0
votes

Not sure how to format the query in Lucene. The scenario is that the search term must be present in one of the two columns (either one is fine).

boolQuery.Add(query1, Occur.MUST)  'this one is fine
boolQuery.Add(query2, Occur.SHOULD)
boolQuery.Add(query3, Occur.SHOULD)

Brings up results even when the search term is not present at all in column 2 and column 3.

boolQuery.Add(query2, Occur.MUST)
boolQuery.Add(query3, Occur.SHOULD)

Does not bring up results when the search term is present in column 3 but not in column 2.

How do I format the query so that I get equivalent of this:

where  column 1= val1 and (column 2 = val2 or column 3 = val2)
2

2 Answers

0
votes

MUST, as the name suggests, makes the occurrence mandatory. SHOULD means optional. The first boolean query will basically match only documents hit by the first clause, but if any of them can be hit by the second or third clause, they will score higher. To get the results to match your desired linq (i assume that's what it is) statement, this should work (using java).

BooleanQuery q = new BooleanQuery();

BooleanQuery subQuery = new BooleanQuery();
subQuery.addClause(new BooleanClause(q2,Occur.SHOULD));
subQuery.addClause(new BooleanClause(q3,Occur.SHOULD));

q.addClause(new BooleanClause(q1, Occur.MUST));
q.addClause(new BooleanClause(subQuery,Occur.MUST));

Your confusion probably stems from the fact that the query API implements must and should as unary operators, while in the traditional programming languages AND and OR are binary operators

0
votes

i solved a similar issue using query syntax:

+(col1:{query} OR col2:{query})

this will return the documents having the value {query} in at least one of the fields.

(note: i am using the classes Query and MultiFieldQueryParser)