user142866
user142866

Reputation:

Lucene "Or Queries"

I am new in Lucene, I am trying to make a search something like this

 content="some thext" and (id ="A" or id="B" or id="c")

I am really lost with that, could you help me

Thank you.

Upvotes: 15

Views: 23998

Answers (3)

Reeebuuk
Reeebuuk

Reputation: 1381

BooleanQuery is deprecated now.

New syntax looks like this. (should == OR, must == AND)

val searchManager = Search.getSearchManager(cache)
val queryBuilder = searchManager.buildQueryBuilderForClass(classTag[T].runtimeClass).get()
val luceneQuery = queryBuilder.bool()
luceneQuery.should(new TermQuery(new Term("type", "lala"))
luceneQuery.createQuery()

Upvotes: 0

Alexander  Petryakov
Alexander Petryakov

Reputation: 730

BooleanQuery mainQuery = new BooleanQuery();

TermQuery contentFilter = new TermQuery(new Term("content", "some text"));
mainQuery.add(contentFilter, BooleanClause.Occur.MUST);

BooleanQuery idFilter = new BooleanQuery();
idFilter.setMinimumNumberShouldMatch(1);
idFilter.add(new TermQuery(new Term("id", A)), BooleanClause.Occur.SHOULD);
idFilter.add(new TermQuery(new Term("id", B)), BooleanClause.Occur.SHOULD);
idFilter.add(new TermQuery(new Term("id", C)), BooleanClause.Occur.SHOULD);
mainQuery.Add(idFilter, BooleanClause.Occur.MUST);

Upvotes: 27

Adam Paynter
Adam Paynter

Reputation: 46898

I believe the "Grouping" section in the Query Parser Syntax documentation provides the answer:

(jakarta OR apache) AND website

I suspect you should make your operators (and, or) upper case. As well, I don't think you can use the equals operator (use a colon instead).

content:"some thext" AND (id:"A" OR id:"B" OR id:"c")

Upvotes: 21

Related Questions