user660024
user660024

Reputation: 241

Count and sub count in lucene

My fields in lucene are product_name, type and sub_types.

I am querying on type with abc, this results me in products whose type is abc. This abc type products have sub_types as pqr and xyz.

I can get total count of the xyz type using TopScoreDocCollector.getTotalHits().

But I want to get the count of sub_types. ie. pqr and xyz.

How can I get it?

Any reply would be of great help for me.

Thanks in advance.

Upvotes: 0

Views: 226

Answers (1)

Gene Golovchinsky
Gene Golovchinsky

Reputation: 6131

One way to do this is to create a filter based on your abc query, and then use that filter to constrain results for the sub-type queries.

IndexSearcher searcher = // searcher to use
int nDocs = 100; // number of docs to retrieve
QueryParser parser = // query parser to use

Query typeQuery = parser.parse("type:abc");
Filter f = CachingWrapperFilter(new QueryWrapperFilter(typeQuery));
Query subtypeQuery = parser.parse("sub_type:xyz");
TopDocs results = searcher.search(subtypeQuery, f, nDocs);

Another thought: if you know up-front which sub-type you're interested in, you can simply add both a type and a sub-type to the query: +type:abc +sub_type:xyz.

Finally, you might consider using Solr to index your data if you have these kinds of queries.

Upvotes: 1

Related Questions