【问题标题】:Lucene: the same query parsed from String and build via Query API doesn't yield same resultsLucene:从 String 解析并通过 Query API 构建的相同查询不会产生相同的结果
【发布时间】:2013-02-25 14:55:05
【问题描述】:

我有以下代码:

public static void main(String[] args) throws Throwable {
    String[] texts = new String[]{
            "starts_with k mer",
            "starts_with mer",
            "starts_with bleue est mer",
            "starts_with mer est bleue",
            "starts_with mer bla1 bla2 bla3 bla4 bla5",
            "starts_with bleue est la mer",
            "starts_with la mer est bleue",
            "starts_with la mer"
    };


    //write:
    Set<String> stopWords = new HashSet<String>();
    StandardAnalyzer stdAn = new StandardAnalyzer(Version.LUCENE_36, stopWords);
    Directory fsDir = FSDirectory.open(INDEX_DIR);
    IndexWriterConfig iwConf  = new IndexWriterConfig(Version.LUCENE_36,stdAn);
    iwConf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    IndexWriter indexWriter = new IndexWriter(fsDir,iwConf);
    for(String text:texts) {
         Document document = new Document();
         document.add(new Field("title",text,Store.YES,Index.ANALYZED));
         indexWriter.addDocument(document);
    }
    indexWriter.commit();

    //read
    IndexReader indexReader = IndexReader.open(fsDir);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);

    //get query:
    //Query query = getQueryFromString("mer");
    Query query = getQueryFromAPI("mer");

    //explain
    System.out.println("======== Query: "+query+"\n");
    TopDocs hits = indexSearcher.search(query, 10);
    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = indexSearcher.doc(scoreDoc.doc);
        System.out.println(">>> "+doc.get("title"));
        System.out.println("Explain:");
        System.out.println(indexSearcher.explain(query, scoreDoc.doc));
    }
}

private static Query getQueryFromString(String searchString) throws Throwable {
    Set<String> stopWords = new HashSet<String>();
    Query query = new QueryParser(Version.LUCENE_36, "title",new StandardAnalyzer(Version.LUCENE_36, stopWords)).parse("("+searchString+") \"STARTS_WITH "+searchString+"\"");
    return query;
}

private static Query getQueryFromAPI(String searchString) throws Throwable {
    Set<String> stopWords = new HashSet<String>();
    Query searchStringTermsMatchTitle = new QueryParser(Version.LUCENE_36, "title", new StandardAnalyzer(Version.LUCENE_36, stopWords)).parse(searchString);

    PhraseQuery titleStartsWithSearchString = new PhraseQuery();
    titleStartsWithSearchString.add(new Term("title","STARTS_WITH".toLowerCase()+" "+searchString));
    BooleanQuery query = new BooleanQuery(true);

    BooleanClause matchClause = new BooleanClause(searchStringTermsMatchTitle, Occur.SHOULD);
    query.add(matchClause);     
    BooleanClause startsWithClause = new BooleanClause(titleStartsWithSearchString, Occur.SHOULD);
    query.add(startsWithClause);

    return query;
}

基本上我正在索引一些字符串,然后我有两种方法可以从用户输入创建 Lucene 查询,一种只是“手动”(通过字符串连接)构建相应的 Lucene 查询字符串,另一种使用 Lucene 的 API构建查询。他们似乎在构建相同的查询,因为查询的调试输出显示完全相同的查询字符串,但搜索结果不一样:

  • 运行通过字符串连接生成的查询(对于参数“mer”):

    title:mer title:"starts_with mer"

在这种情况下,当我使用它进行搜索时,我首先会得到与title:"starts_with mer" 部分匹配的文档。这是第一个结果中的explain

>>> starts_with mer
Explain:
1.2329358 = (MATCH) sum of:
  0.24658716 = (MATCH) weight(title:mer in 1), product of:
    0.4472136 = queryWeight(title:mer), product of:
      0.882217 = idf(docFreq=8, maxDocs=8)
      0.50692016 = queryNorm
    0.55138564 = (MATCH) fieldWeight(title:mer in 1), product of:
      1.0 = tf(termFreq(title:mer)=1)
      0.882217 = idf(docFreq=8, maxDocs=8)
      0.625 = fieldNorm(field=title, doc=1)
  0.9863486 = (MATCH) weight(title:"starts_with mer" in 1), product of:
    0.8944272 = queryWeight(title:"starts_with mer"), product of:
      1.764434 = idf(title: starts_with=8 mer=8)
      0.50692016 = queryNorm
    1.1027713 = fieldWeight(title:"starts_with mer" in 1), product of:
      1.0 = tf(phraseFreq=1.0)
      1.764434 = idf(title: starts_with=8 mer=8)
      0.625 = fieldNorm(field=title, doc=1)
  • 运行通过 Lucene 查询帮助工具构建的查询会产生一个明显相同的查询:

    title:mer title:"starts_with mer"

但这次结果不一样了,因为实际上title:"starts_with mer" 部分是不匹配的。这是第一个结果的explain

>>> starts_with mer
Explain:
0.15185544 = (MATCH) sum of:
  0.15185544 = (MATCH) weight(title:mer in 1), product of:
    0.27540696 = queryWeight(title:mer), product of:
      0.882217 = idf(docFreq=8, maxDocs=8)
      0.312176 = queryNorm
    0.55138564 = (MATCH) fieldWeight(title:mer in 1), product of:
      1.0 = tf(termFreq(title:mer)=1)
      0.882217 = idf(docFreq=8, maxDocs=8)
      0.625 = fieldNorm(field=title, doc=1)

我的问题是:为什么我没有得到相同的结果?我真的很希望能够在这里使用查询帮助工具,特别是因为我想使用BooleanQuery(disableCoord) 选项,但我真的不知道如何直接将其表达为 Lucene 查询字符串。 (是的,我的示例在那里传递了“true”,我也尝试过使用“false”,结果相同)。

===更新

femtoRgon 的回答很好:问题是我将整个搜索字符串添加为一个词,而不是先将其拆分为词,然后将每个词添加到查询中。

如果输入字符串由一个词组成,femtoRgon 给出的答案可以正常工作:在这种情况下,单独添加“STARTS_WITH”文本作为一个词,然后将搜索字符串作为第二个词添加。

但是,如果用户输入的内容会被多个术语标记,您必须首先将其拆分为术语(最好使用您在索引时使用的相同分析器和/或标记器 - 以获得一致的结果)然后将每个术语添加到查询中。

我最终做的是使用与索引相同的分析器创建一个将查询字符串拆分为术语的函数:

private static List<String> getTerms(String text) throws Throwable {
    Analyzer analyzer = getAnalyzer();      
    StringReader textReader = new StringReader(text);
    TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME_TITLE, textReader);
    tokenStream.reset();        
    List<String> terms = new ArrayList<String>();
    CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
    while (tokenStream.incrementToken()) {
        String term = charTermAttribute.toString();
        terms.add(term);
    }
    textReader.close();
    tokenStream.close();
    analyzer.close();       
    return terms;
}

然后我首先将“STARTS_WITH”作为一个术语添加,然后将列表中的每个元素作为一个单独的术语添加:

PhraseQuery titleStartsWithSearchString = new PhraseQuery();
titleStartsWithSearchString.add(new Term("title","STARTS_WITH".toLowerCase()));
for(String term:getTerms(searchString)) {
    titleStartsWithSearchString.add(new Term("title",term));
}

【问题讨论】:

    标签: lucene


    【解决方案1】:

    我相信您遇到的问题是您将整个短语作为一个术语添加到您的 PhraseQuery 中。在索引中,以及由 QueryParser 解析的查询中,这将被拆分为术语 "starts_with""mer",它们必须连续找到。但是,在您构建的查询中,您的 PhraseQuery 中只有一个术语,即术语 "starts_with mer",它在索引中不作为单个术语存在。

    您应该能够将构建 PhraseQuery 的位更改为:

    PhraseQuery titleStartsWithSearchString = new PhraseQuery();
    titleStartsWithSearchString.add(new Term("title","STARTS_WITH".toLowerCase())
    titleStartsWithSearchString.add(new Term("title",searchString));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      相关资源
      最近更新 更多