如何搜索所有字段?

Ada*_*Lee 15 lucene lucene.net

在Lucene中,我们可以使用TermQuery来搜索带有字段的文本.我想知道如何在一堆字段或所有可搜索的字段中搜索关键字?

jav*_*nna 24

另一种方法,不需要索引任何比你已经拥有的更多的东西,也不需要组合不同的查询,正在使用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");
Run Code Online (Sandbox Code Playgroud)

这就是我用Java编写的原始lucene库的方法.我不确定MultiFieldQueryParserlucene.net 是否也有.


pha*_*ani 10

两种方法

1)索引时间方法:使用catch-all字段.这只是附加所有字段中的所有文本(输入文档中的总文本),并将生成的大文本放在单个字段中.您需要在索引时添加其他字段以充当全能字段.

2)搜索时方法:使用BooleanQuery组合多个查询,例如TermQuery实例.可以形成那些多个查询以覆盖所有目标字段.

文章末尾的示例检查.

如果您在运行时知道目标字段列表,请使用方法2.否则,你必须使用第一种方法.


vij*_*ani 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;
Run Code Online (Sandbox Code Playgroud)

单击此处查看我以前对 vb.net 中相同问题的回答