1

给定一个文档 {'foo', 'bar', 'baz'},我想使用 SpanNearQuery 与令牌 {'baz', 'extra'} 进行匹配

但这失败了。

我该如何解决这个问题?

样本测试(使用 lucene 2.9.1),结果如下:

  • givenSingleMatch - PASS
  • givenTwoMatches - 通过
  • givenThreeMatches - 通过
  • givenSingleMatch_andExtraTerm - 失败

...

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class SpanNearQueryTest {

    private RAMDirectory directory = null;

    private static final String BAZ = "baz";
    private static final String BAR = "bar";
    private static final String FOO = "foo";
    private static final String TERM_FIELD = "text";

    @Before
    public void given() throws IOException {
        directory = new RAMDirectory();
        IndexWriter writer = new IndexWriter(
                directory,
                new StandardAnalyzer(Version.LUCENE_29),
                IndexWriter.MaxFieldLength.UNLIMITED);

        Document doc = new Document();
        doc.add(new Field(TERM_FIELD, FOO, Field.Store.NO, Field.Index.ANALYZED));
        doc.add(new Field(TERM_FIELD, BAR, Field.Store.NO, Field.Index.ANALYZED));
        doc.add(new Field(TERM_FIELD, BAZ, Field.Store.NO, Field.Index.ANALYZED));

        writer.addDocument(doc);
        writer.commit();
        writer.optimize();
        writer.close();
    }

    @After
    public void cleanup() {
        directory.close();
    }

    @Test
    public void givenSingleMatch() throws IOException {

        SpanNearQuery spanNearQuery = new SpanNearQuery(
                new SpanQuery[] {
                        new SpanTermQuery(new Term(TERM_FIELD, FOO))
                }, Integer.MAX_VALUE, false);

        TopDocs topDocs = new IndexSearcher(IndexReader.open(directory)).search(spanNearQuery, 100);

        Assert.assertEquals("Should have made a match.", 1, topDocs.scoreDocs.length);
    }

    @Test
    public void givenTwoMatches() throws IOException {

        SpanNearQuery spanNearQuery = new SpanNearQuery(
                new SpanQuery[] {
                        new SpanTermQuery(new Term(TERM_FIELD, FOO)),
                        new SpanTermQuery(new Term(TERM_FIELD, BAR))
                }, Integer.MAX_VALUE, false);

        TopDocs topDocs = new IndexSearcher(IndexReader.open(directory)).search(spanNearQuery, 100);

        Assert.assertEquals("Should have made a match.", 1, topDocs.scoreDocs.length);
    }

    @Test
    public void givenThreeMatches() throws IOException {

        SpanNearQuery spanNearQuery = new SpanNearQuery(
                new SpanQuery[] {
                        new SpanTermQuery(new Term(TERM_FIELD, FOO)),
                        new SpanTermQuery(new Term(TERM_FIELD, BAR)),
                        new SpanTermQuery(new Term(TERM_FIELD, BAZ))
                }, Integer.MAX_VALUE, false);

        TopDocs topDocs = new IndexSearcher(IndexReader.open(directory)).search(spanNearQuery, 100);

        Assert.assertEquals("Should have made a match.", 1, topDocs.scoreDocs.length);
    }

    @Test
    public void givenSingleMatch_andExtraTerm() throws IOException {

        SpanNearQuery spanNearQuery = new SpanNearQuery(
                new SpanQuery[] {
                        new SpanTermQuery(new Term(TERM_FIELD, BAZ)),
                        new SpanTermQuery(new Term(TERM_FIELD, "EXTRA"))
                },
                Integer.MAX_VALUE, false);

        TopDocs topDocs = new IndexSearcher(IndexReader.open(directory)).search(spanNearQuery, 100);

        Assert.assertEquals("Should have made a match.", 1, topDocs.scoreDocs.length);
    }
}
4

1 回答 1

6

SpanNearQuery 可让您查找彼此相距一定距离内的术语。

示例(来自http://www.lucidimagination.com/blog/2009/07/18/the-spanquery/):

假设我们想在 doug 的 5 个位置内找到 lucene,doug 跟随 lucene(顺序很重要)——您可以使用以下 SpanQuery:

new SpanNearQuery(new SpanQuery[] {
  new SpanTermQuery(new Term(FIELD, "lucene")),
  new SpanTermQuery(new Term(FIELD, "doug"))},
  5,
  true);

替代文字
(来源:lucidimagination.com

在这个示例文本中,Lucene 在 Doug 的 3 以内

但是对于您的示例,我能看到的唯一匹配是您的查询和目标文档都有“cd”(我假设所有这些术语都在一个字段中)。在这种情况下,您不需要使用任何特殊的查询类型。使用标准机制,您将获得一些非零权重,因为它们在同一字段中都包含相同的术语。

编辑 3 - 回应最新评论,答案是除了它的用途之外,您不能SpanNearQuery做任何事情,即找出文档中的多个术语是否出现在彼此的一定数量的地方。我无法说出您的具体用例/预期结果是什么(随时发布),但在最后一种情况下,如果您只想找出 ("BAZ", "EXTRA") 中的一个或多个是否在该文件, aBooleanQuery将工作得很好。

编辑 4 - 现在您已经发布了您的用例,我明白您想要做什么。你可以这样做:使用BooleanQuery上面提到的 a 来组合你想要的单个术语以及SpanNearQuery,并在SpanNearQuery.

因此,文本形式的查询将如下所示:

BAZ OR EXTRA OR "BAZ EXTRA"~100^5

(例如 - 这将匹配包含“BAZ”或“EXTRA”的所有文档,但将更高的分数分配给术语“BAZ”和“EXTRA”出现在彼此相距 100 个位置内的文档;将位置和提升调整为你喜欢。这个示例来自 Solr 食谱,因此它可能无法在 Lucene 中解析,或者可能会产生不理想的结果。没关系,因为在下一节中我将向您展示如何使用 API 构建它)。

以编程方式,您将按如下方式构造它:

Query top = new BooleanQuery();

// Construct the terms since they will be used more than once
Term bazTerm = new Term("Field", "BAZ");
Term extraTerm = new Term("Field", "EXTRA");

// Add each term as "should" since we want a partial match
top.add(new TermQuery(bazTerm), BooleanClause.Occur.SHOULD);
top.add(new TermQuery(extraTerm), BooleanClause.Occur.SHOULD);

// Construct the SpanNearQuery, with slop 100 - a document will get a boost only
// if BAZ and EXTRA occur within 100 places of each other.  The final parameter means
// that BAZ must occur before EXTRA.
SpanNearQuery spanQuery = new SpanNearQuery(
                              new SpanQuery[] { new SpanTermQuery(bazTerm), 
                                                new SpanTermQuery(extraTerm) }, 
                              100, true);

// Give it a boost of 5 since it is more important that the words are together
spanQuery.setBoost(5f);

// Add it as "should" since we want a match even when we don't have proximity
top.add(spanQuery, BooleanClause.Occur.SHOULD);

希望有帮助!将来,试着从发布你所期望的结果开始——即使对你来说很明显,对读者来说可能不是,而且明确可以避免来回多次。

于 2010-01-07T16:33:35.000 回答