简体   繁体   English

如何在 Lucene 中进行多字段 - 短语搜索?

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

Title asks it all... I want to do a multi field - phrase search in Lucene.. How to do it ?标题要求一切... 我想在 Lucene 中进行多字段短语搜索.. 怎么做?

for example : I have fields as String s[] = {"title","author","content"};例如:我的字段为String s[] = {"title","author","content"};
I want to search harry potter across all fields.. How do I do it ?我想在所有领域搜索harry potter .. 我该怎么做?

Can someone please provide an example snippet ?有人可以提供一个示例片段吗?

  1. Use MultiFieldQueryParser , its a QueryParser which constructs queries to search multiple fields.使用MultiFieldQueryParser ,它是一个 QueryParser ,它构建查询以搜索多个字段。 . .

  2. Other way is to use Create a BooleanQuery consisting of TermQurey (in your case phrase query).另一种方法是使用 Create a BooleanQuery 由 TermQurey 组成(在您的情况下短语查询)。

  3. Third way is to include the content of other fields into your default content field.第三种方法是将其他字段的内容包含到您的default内容字段中。


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) MultiFieldQueryParser将以这种方式解析查询:(参见 javadoc)

Parses a query which searches on the fields specified.解析在指定字段上搜索的查询。 If x fields are specified, this effectively constructs:如果指定了 x 字段,这将有效地构造:

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

Hope this helps.希望这可以帮助。

intensified googling revealed this :加强谷歌搜索揭示了这一点:
http://lucene.472066.n3.nabble.com/Phrase-query-on-multiple-fields-td2292312.html . 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...由于它是最新和最好的,我想我会采用他的方法......不过,它可能会帮助那些正在寻找像我这样的人......

You need to use MultiFieldQueryParser with escaped string.您需要将MultiFieldQueryParser与转义字符串一起使用。 I have tested it with Lucene 8.8.1 and it's working like magic.我已经使用Lucene 8.8.1对其进行了测试,并且它的工作Lucene 8.8.1很神奇。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM