在 Lucene 中,我们可以使用 TermQuery 来搜索带有字段的文本。我想知道如何在一堆字段或所有可搜索字段中搜索关键字?
问问题
10099 次
3 回答
27
另一种方法是使用MultiFieldQueryParser
.
您可以提供要搜索的字段列表和查询,仅此而已。
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
Version.LUCENE_41,
new String[]{"title", "content", "description"},
new StandardAnalyzer(Version.LUCENE_41));
Query query = queryParser.parse("here goes your query");
这就是我使用 Java 编写的原始 lucene 库的方式。我不确定MultiFieldQueryParser
lucene.net 中是否也可以使用。
于 2013-03-02T11:47:54.820 回答
11
两种方法
1) 索引时间方法:使用包罗万象的字段。这只不过是附加所有字段中的所有文本(输入文档中的总文本)并将产生的巨大文本放在单个字段中。您必须在索引时添加一个额外的字段以充当一个包罗万象的字段。
2) 搜索时方法:使用BooleanQuery组合多个查询,例如 TermQuery 实例。可以形成这些多个查询以覆盖所有目标字段。
文章末尾的示例检查。
如果您在运行时知道目标字段列表,请使用方法 2。否则,您必须使用第一种方法。
于 2013-03-02T03:49:06.570 回答
5
使用“ MultifieldQueryParser ”搜索所有字段的另一种简单方法是在查询中使用IndexReader.FieldOption.ALL。
这是 c# 中的示例。
Directory directory = FSDirectory.Open(new DirectoryInfo(HostingEnvironment.MapPath(VirtualIndexPath)));
//get analyzer
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
//get index reader and searcher
IndexReader indexReader__1 = IndexReader.Open(directory, true);
Searcher indexSearch = new IndexSearcher(indexReader__1);
//add all possible fileds in multifieldqueryparser using indexreader getFieldNames method
var queryParser = new MultiFieldQueryParser(Version.LUCENE_29, indexReader__1.GetFieldNames(IndexReader.FieldOption.ALL).ToArray(), analyzer);
var query = queryParser.Parse(Criteria);
TopDocs resultDocs = null;
//perform search
resultDocs = indexSearch.Search(query, indexReader__1.MaxDoc());
var hits = resultDocs.scoreDocs;
单击此处查看我在 vb.net 中对同一问题的先前回答
于 2015-07-31T23:20:21.163 回答