Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create nested boolean query with lucene API (a AND (b OR c))?

Tags:

java

lucene

api

I have an indexed object with three fields (userId, title, description). I want to find all objects of a specific user where the title OR the description contains a given keyword.

I have something like this (but that's obviously wrong):

WildcardQuery nameQuery = new WildcardQuery(new Term("name", filter.getSearch()));
WildcardQuery descQuery = new WildcardQuery(new Term("description", filter.getSearch()));

TermQuery userQuery = new TermQuery(new Term("user_id", u.getId()+""));

BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(new BooleanClause(name_query, Occur.SHOULD));
booleanQuery.add(new BooleanClause(desc_query, Occur.SHOULD));
booleanQuery.add(new BooleanClause(user_query, Occur.MUST));

How wo modify the code to get all objects with the correct ID and the search phrase in title or description?

like image 652
Arne Deutsch Avatar asked Jun 28 '10 08:06

Arne Deutsch


1 Answers

I think that it will be something like this:

TermQuery userQuery = new TermQuery(new Term("user_id", u.getId()+""));

BooleanQuery orQuery = new BooleanQuery();
orQuery.add(new BooleanClause(name_query, Occur.SHOULD));
orQuery.add(new BooleanClause(desc_query, Occur.SHOULD));

BooleanQuery andQuery = new BooleanQuery();
andQuery.add(new BooleanClause(userQuery , Occur.MUST));
andQuery.add(new BooleanClause(orQuery, Occur.MUST));
like image 58
Bartek Jablonski Avatar answered Nov 02 '22 13:11

Bartek Jablonski