Lucene - Equivalent SQL Keyword "IN"

Please excuse my newbie question. I tried to find the answers, but finding these kinds of things is quite difficult given the keywords ...

I use Lucene 5.2.x to index a set of documents, and each document has two fields: idand description.

I get a set idsfrom a previous request in the system. Now I would like to get Lucene's text search results on description, but only from the documents in the set ids. If I did this (naively) in MySQL, I could do something like:

SELECT * FROM mytable 
    WHERE description LIKE 'blah%' 
          AND 
          id IN (6345, 5759, 333, ...)

A set idsmaybe tens of thousands. What is the best approach to this with Lucene? Can I build a Lucene query to effectively solve this problem, or search through the entire document and then perform a set of intersections? Something else?

Thank!

+4
source share
1 answer

I would like to get Lucene text search results in the description, but only from the documents in the set ids.

You need to use BooleanQuery.

If you create a query using QueryParser, use:

+(id:6345 id:5759 id:333 ...) +(description:"blah*")

If you create Queryprogrammatically, then the code will look something like this:

BooleanQuery ids = new BooleanQuery();
ids.add(new TermQuery(new Term("id", "6345")), SHOULD);
ids.add(new TermQuery(new Term("id", "5759")), SHOULD);
ids.add(new TermQuery(new Term("id", "333")), SHOULD);

BooleanQuery resultQuery = new BooleanQuery();
resultQuery.add(new PrefixQuery(new Term("description", "blah")), MUST);
resultQuery.add(ids, MUST);

A set idsmaybe tens of thousands.

BooleanQuery (. org.apache.lucene.search.BooleanQuery#maxClauseCount). , BooleanQuery.setMaxClauseCount(). .

Lucene , , ? - ?

, - , . , ( ).

, , Lucene ( ). Lucene . , , Lucene, " " Lucene.

Lucene , . , , .

+1

All Articles