How to search across all the fields?

a 夏天 提交于 2019-12-09 02:23:13

问题


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?


回答1:


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

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.




回答2:


Two approaches

1) Index-time approach: Use a catch-all field. This is nothing but appending all the text from all the fields (total text from your input doc) and place that resulting huge text in a single field. You've to add an additional field while indexing to act as a catch-all field.

2) Search-time approach: Use a BooleanQuery to combine multiple queries, for example TermQuery instances. Those multiple queries can be formed to cover all the target fields.

Example check at the end of the article.

Use approach 2 if you know the target-field list at runtime. Otherwise, you've got to use the 1st approach.




回答3:


Another easy approach to search across all fields using "MultifieldQueryParser" is use IndexReader.FieldOption.ALL in your query.

Here is example in 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;

click here to check out my pervious answer to same quesiton in vb.net



来源:https://stackoverflow.com/questions/15170097/how-to-search-across-all-the-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!