3

Title asks it all... I want to do a multi field - phrase search in Lucene.. How to do it ?

for example : I have fields as String s[] = {"title","author","content"};
I want to search harry potter across all fields.. How do I do it ?

Can someone please provide an example snippet ?

Shrinath
  • 7,888
  • 13
  • 48
  • 85

3 Answers3

5
  1. Use MultiFieldQueryParser, its a QueryParser which constructs queries to search multiple fields..

  2. Other way is to use Create a BooleanQuery consisting of TermQurey (in your case phrase query).

  3. Third way is to include the content of other fields into your default content field.


Add

Generally speaking, querying on multiple fields isn’t the best practice for user-entered queries. More commonly, all words you want searched are indexed into a contents or keywords field by combining various fields.


Update

Usage:

Query query = MultiFieldQueryParser.parse(Version.LUCENE_30, new String[] {"harry potter","harry potter","harry potter"},   new String[] {"title","author","content"},new SimpleAnalyzer());
IndexSearcher searcher = new IndexSearcher(...);
Hits hits = searcher.search(query);

The MultiFieldQueryParser will resolve the query in this way: (See javadoc)

Parses a query which searches on the fields specified. If x fields are specified, this effectively constructs:

(field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)

Hope this helps.

Favonius
  • 13,959
  • 3
  • 55
  • 95
  • 3rd option isn't even an option to me, the example I've in the question is dummy... due to the constraints of my app, I require the fields not to be combined... – Shrinath Feb 24 '11 at 05:32
  • 2nd option is what it is... a SECOND option!!! I am interested in MultiFieldQueryParser, I am doing that, but getting errors in doing it.. I guess that'll be the way to go... – Shrinath Feb 24 '11 at 05:33
  • about ADD, I'll keep that in mind as a good advice.. thank you :) – Shrinath Feb 24 '11 at 05:40
  • @Favonius : do you mind instructing how MultiFieldQueryParser works with phrases ? because parsing with it returns a `Query` object and not the phraseQuery object... – Shrinath Feb 24 '11 at 05:41
  • @Shrinath: Yes it will return the `Query` object. Under the hood it extends the default `QueryParser` and it parses a query expression using QueryParser’s static parse method for each field as the default field and combines them into a BooleanQuery. The default operator **OR** is used in the simplest parse method when adding the clauses to the `BooleanQuery`. Which again is the **Number 2** option I mentioned. – Favonius Feb 24 '11 at 06:13
0

intensified googling revealed this :
http://lucene.472066.n3.nabble.com/Phrase-query-on-multiple-fields-td2292312.html.
Since it is latest and best, I'll go with his approach I guess.. Nevertheless, it might help someone who is looking for something like I am...

Shrinath
  • 7,888
  • 13
  • 48
  • 85
0

You need to use MultiFieldQueryParser with escaped string. I have tested it with Lucene 8.8.1 and it's working like magic.

String queryStr = "harry potter";
queryStr = "\"" + queryStr.trim() + "\"";
Query query = new MultiFieldQueryParser(new String[]{"title","author","content"}, new StandardAnalyzer()).parse(queryStr);
System.out.println(query);

It will print.

(title:"harry potter") (author:"harry potter") (content:"harry potter")
Ankur Mahajan
  • 3,396
  • 3
  • 31
  • 42