How to do a Multi field - Phrase search in Lucene?

前端 未结 2 648
刺人心
刺人心 2021-01-14 04:27

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\"

相关标签:
2条回答
  • 2021-01-14 05:18
    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.

    0 讨论(0)
  • 2021-01-14 05:20

    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...

    0 讨论(0)
提交回复
热议问题