如何在Lucene中进行“多字段-词组搜索"? [英] How to do a Multi field - Phrase search in Lucene?
问题描述
标题全部问完了...我想在Lucene中进行多字段词组搜索.
Title asks it all... I want to do a multi field - phrase search in Lucene.. How to do it ?
例如:
我的字段为String s[] = {"title","author","content"};
我想在所有字段中搜索harry potter
.我该怎么做?
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 ?
推荐答案
-
使用
MultiFieldQueryParser
,它是一个QueryParser,它构造查询以搜索多个字段..
其他方法是使用创建一个由TermQurey组成的布尔查询(在您的情况下为短语查询).
Other way is to use Create a BooleanQuery consisting of TermQurey (in your case phrase query).
第三种方法是将其他字段的内容包含到您的default
内容字段中.
Third way is to include the content of other fields into your default
content field.
添加
通常来说,对多个字段进行查询并不是用户输入查询的最佳做法.更常见的是,要组合所有字段,将要搜索的所有单词编入内容或关键字字段.
更新
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
用法:
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);
MultiFieldQueryParser
将以这种方式解析查询:(请参阅javadoc)
The MultiFieldQueryParser
will resolve the query in this way: (See javadoc)
解析一个查询,该查询在 指定的字段.如果x字段是 指定,这有效 构造:
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)
(field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
希望这会有所帮助.
这篇关于如何在Lucene中进行“多字段-词组搜索"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!