Lucene:带有权重的文档字段

时间:2014-10-21 15:42:09

标签: java boost lucene

我想使用Lucene将文档与大量加权标记一起索引(权重为真实的概率)。这些字段都将被称为“标记”,以允许搜索以这些标记为目标,并返回具有匹配标记但概率最高的文档。

以下代码仅显示了我想要做的更清晰的事情。

但是,Lucene中的字段提升意味着应用于索引字段和字段类型,而不是应用于添加到文档中的实例。这意味着,下面的解决方案不起作用,我需要使用具有唯一名称的字段,以便对它们应用提升。

我也知道这是一个非常糟糕的解决方案,我想知道这里有人知道更好的方法。我显然需要a)存储概率,b)有办法在检索过程中使用它们。

private void indexDocuments(IndexWriter writer) throws IOException {

    Document docA = new Document();
    Field pathFieldA = new StringField("path", "dog.jpg", Field.Store.YES);
    docA.add(pathFieldA);
    // add all tags to the index
    StringField c1 = new StringField("tag", "dog", Field.Store.YES);
    c1.setBoost(0.8f);
    docA.add(c1);
    StringField c2 = new StringField("tag", "cat", Field.Store.YES);
    c2.setBoost(0.2f);
    docA.add(c2);

    Document docB = new Document();
    Field pathFieldB = new StringField("path", "cat.jpg", Field.Store.YES);
    docB.add(pathFieldB);
    // add all tags to the index
    StringField tagB1 = new StringField("tag", "dog", Field.Store.YES);
    tagB1.setBoost(0.2f);
    docB.add(tagB1);
    StringField tagB2 = new StringField("tag", "cat", Field.Store.YES);
    tagB2.setBoost(0.8f);
    docB.add(tagB2);

    writer.addDocument(docB);
    writer.addDocument(docB);
}

0 个答案:

没有答案
相关问题