Multiple Field Query handling in Lucene

前端 未结 1 997
天涯浪人
天涯浪人 2021-01-02 22:27

I have written an index searcher in Lucene that will search multiple fields in the indexed database.

Actually it takes query as two strings one is say title

相关标签:
1条回答
  • 2021-01-02 23:24

    We use MultiFieldQueryParser only when we want to search the same keyword(s) in multiple fields.

    To handle your use case, it is simpler that you already have references to city-keyword and title-keyword separately. Try using following code.

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
    // city query
    QueryParser cityQP = new QueryParser(Version.LUCENE_CURRENT, "city", analyzer);
    Query cityQuery = cityQP.parse(myCity);
    
    // title query
    QueryParser titleQP = new QueryParser(Version.LUCENE_CURRENT, "title", analyzer);
    Query titleQuery = titleQP.parse(myQuery);
    
    // final query
    BooleanQuery finalQuery = new BooleanQuery();
    finalQuery.add(cityQuery, Occur.MUST); // MUST implies that the keyword must occur.
    finalQuery.add(titleQuery, Occur.MUST); // Using all "MUST" occurs is equivalent to "AND" operator.
    
    0 讨论(0)
提交回复
热议问题