如何搜索所有领域? [英] How to search across all the fields?
问题描述
在 Lucene 中,我们可以使用 TermQuery 来搜索带有字段的文本.我想知道如何在一堆字段或所有可搜索字段中搜索关键字?
In Lucene, we can use TermQuery to search a text with a field. I am wondering how to search a keyword across a bunch of fields or all the searchable fields?
推荐答案
另一种方法是使用 MultiFieldQueryParser
.
Another approach, which doesn't require to index anything more than what you already have, nor to combine different queries, is using the MultiFieldQueryParser
.
您可以提供要搜索的字段列表和查询,仅此而已.
You can provide a list of fields where you want to search on and your query, that's all.
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 中使用.
This is how I would do it with the original lucene library written in Java. I'm not sure whether the MultiFieldQueryParser
is available in lucene.net too.
这篇关于如何搜索所有领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!