Jeewan
Jeewan

Reputation: 47

How can I search on list of values using Lucene Query interface

Simplistic Problem description:

Lucene index has two fields per document: ID and NAME.

I want to make a query using the Lucene Query interface such that I can find all the documents where ID is 1 OR 2 OR 3 OR so on. The IDs to be searched will be in a list and can potentially have upto 30 elements.

If I was using the query parser I would have done something like

ID:(1 OR 2 OR 3)

But the application is already heavily committed to the Query interface and I want to follow the current pattern. Only way I can think of doing this with Query interface is create n term queries and group them using the Boolean query as below

BooleanQuery booleanQuery = new BooleanQuery();
(String searchId : lstIds)
{
    booleanQuery.add(new TermQuery(new Term("ID", searchId)), BooleanClause.Occur.SHOULD);
}

But is there a better/more efficient way of doing this?

Upvotes: 4

Views: 19333

Answers (2)

Root
Root

Reputation: 995

BooleanQuery is the solution for handling OR operator as you have shown in the code but if you want simple alternative of the it you could also use simple Query and pass the IDs as "1 OR 2 OR 3".

Here is the code snippet lucene 7.

Query query = new QueryParser("ID", analyzer).parse("1 OR 2 OR 3");
TopDocs topDocs = searcher.search(query, 10);

OR if you have all the OR you could also use QueryParser default Operator.

Here is the code snippet for lucene 7.

QueryParser queryParser = new QueryParser("ID", analyzer);
queryParser.setDefaultOperator(QueryParser.Operator.OR);
Query query = queryParser.parse("1 2 3");
TopDocs topDocs = searcher.search(query, 10);

I hope that work for you.

Upvotes: 2

femtoRgon
femtoRgon

Reputation: 33341

Combining queries togetheer with a BooleanQuery is the correct way to reproduce a query like ID:(1 OR 2 OR 3). The query parser will generate a BooleanQuery similar to what you provided for that syntax, so you are absolutely doing the right thing here.

You might be able to make use of PrefixQuery, NumericRangeQuery or TermRangeQuery to simplify matters, if they actually suit your needs in practice, but there is nothing wrong with what you are doing already.

Upvotes: 9

Related Questions