How to incorporate multiple fields in QueryParser?

后端 未结 4 967
无人共我
无人共我 2020-12-12 13:41
Dim qp1 As New QueryParser(\"filename\", New StandardAnalyzer())
Dim qp2 As New QueryParser(\"filetext\", New StandardAnalyzer())
.
.

I am using th

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 14:25

    There are 3 ways to do this.

    The first way is to construct a query manually, this is what QueryParser is doing internally. This is the most powerful way to do it, and means that you don't have to parse the user input if you want to prevent access to some of the more exotic features of QueryParser:

    IndexReader reader = IndexReader.Open("");
    Searcher searcher = new IndexSearcher(reader);
    
    BooleanQuery booleanQuery = new BooleanQuery();
    Query query1 = new TermQuery(new Term("filename", ""));
    Query query2 = new TermQuery(new Term("filetext", ""));
    booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
    booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
    // Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD
    // for AND queries
    Hits hits = searcher.Search(booleanQuery);
    

    The second way is to use MultiFieldQueryParser, this behaves like QueryParser, allowing access to all the power that it has, except that it will search over multiple fields.

    IndexReader reader = IndexReader.Open("");
    Searcher searcher = new IndexSearcher(reader);
    
    Analyzer analyzer = new StandardAnalyzer();
    MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                                            new string[] {"filename", "filetext"},
                                            analyzer);
    
    Hits hits = searcher.Search(queryParser.parse(""));
    

    The final way is to use the special syntax of QueryParser see here.

    IndexReader reader = IndexReader.Open("");
    Searcher searcher = new IndexSearcher(reader);    
    
    Analyzer analyzer = new StandardAnalyzer();
    QueryParser queryParser = new QueryParser("", analyzer);
    //  is the field that QueryParser will search if you don't 
    // prefix it with a field.
    string special = "filename:" + text + " OR filetext:" + text;
    
    Hits hits = searcher.Search(queryParser.parse(special));
    

    Your other option is to create new field when you index your content called filenameandtext, into which you can place the contents of both filename and filetext, then you only have to search one field.

提交回复
热议问题