Reputation: 2285
I have this search query in Lucene
String searchVal = "+(narr:foo narr:bar)" + " +(narr:foo2 narr:bar2)" + " -(narr:foo3 narr:bar3)";
IndexReader reader = luceneUtils.getIndexReader();
IndexSearcher searcher = luceneUtils.getIndexSearcher();
Query query = new QueryParser("narr", new EnglishAnalyzer()).parse(searchVal);
TopDocs topDocs = searcher.search(query, reader.numDocs());
In the sample code above, topDocs
variable holds the result set of Lucene query.
How can I create another Lucene query that searches only in documents retrieved from the previous query. i.e. How can I execute query on topDocs
instead of executing it on all indexed documents?
Upvotes: 1
Views: 453
Reputation: 2408
A very simple way of doing this is to wrap old and new queries inside a BooleanQuery
and use Occur.FILTER
for the original one.
Like this:
BooleanQuery.Builder qb = new BooleanQuery.Builder();
qb.add(query, Occur.MUST);
qb.add(new QueryParser("narr", new EnglishAnalyzer()).parse("new-query"), Occur.FILTER);
Query newQuery = qb.build();
Things will get a bit more complicated if you need to perform this "query refinement" over and over. However, I don't think there's a way of searching inside a previous resultset, you need to construct a new query AFAIK.
Upvotes: 2