0
votes

I have indexed documents in Lucene based on three fields: title, address, city. Now I want to build my query say, C A B so that I can retrieve the documents as follows:

C must be present in the title field of the documents and either A or B must be present in either of address and city fields of the matched documents. The documents that have A present in either of those fields should get higher score or higher boost. Here A, B, C may be single terms or phrases.

I am new to Lucene. I do not have any experience of framing such complex queries. In this context I have read the post Boost factor in MultiFieldQueryParser But this post does not answer my question. So if anyone please help me to solve this I will be really grateful.

2
Is what you have in mind something like: +title: C +(address: A B city: A B)^2 where 2 is the boost level? - rae1

2 Answers

0
votes

title:C AND (address:A^2 OR city:A^2 OR address:B OR city:B)

0
votes

Don't get caught up on reading about MultiFieldQueryParser, that isn't really what you need for this. Standard QueryParser syntax will serve your purposes you fine.

See the Lucene QueryParser syntax documentation

A query like:

+title:C +((address:A city:A)^2 address:B city:B)

Should do nicely.

To explain a bit:

+title:C - require a match on title:C. No results will be returned that don't match this condition.

+(....) - require a match on the subquery contained inside. As long as a match is found on any one of the optional queries contained within the parentheses is matched, this will be satisfied.

(address:A city:A)^2 - You prefer a match on A, these two queries are boosted more heavily with ^2.