java - 如何在 Lucene 7.x 中使用 CustomScoreQuery

标签 java lucene

我对 Lucene 比较陌生,并且想要实现我自己的 CustomScoreQuery,因为我的大学需要它。

我使用 Lucene 演示作为起点来索引文件夹中的所有文档,并希望使用我自己的算法对它们进行评分。

这里是演示源代码的链接。

https://lucene.apache.org/core/7_1_0/demo/src-html/org/apache/lucene/demo/IndexFiles.html https://lucene.apache.org/core/7_1_0/demo/src-html/org/apache/lucene/demo/SearchFiles.html

我正在与 Luke: Lucene Toolbox Project 检查以查看我的索引是否符合预期。我在访问它时出现问题。

package CustomModul;
import java.io.IOException;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Terms;
import org.apache.lucene.queries.CustomScoreProvider;
import org.apache.lucene.queries.CustomScoreQuery;
import org.apache.lucene.search.Query;

public class CountingQuery extends CustomScoreQuery {

public CountingQuery(Query subQuery) {
    super(subQuery);
}


public class CountingQueryScoreProvider extends CustomScoreProvider {

    String _field;

    public CountingQueryScoreProvider(String field, LeafReaderContext context) {
        super(context);
        _field = field;
    }

    public float customScore(int doc, float subQueryScore, float valSrcScores[]) throws IOException {           
        IndexReader r = context.reader();

        //getTermVector returns Null
        Terms vec = r.getTermVector(doc, _field);

        //*TO-DO* Algorithm

        return (float)(1.0f);       
    }   
}

protected CustomScoreProvider getCustomScoreProvider(
        LeafReaderContext context) throws IOException {
    return new CountingQueryScoreProvider("contents", context);
}

}

在我的 customScore 函数中,我像大多数教程中描述的那样访问索引。我应该使用 getTermVector 访问索引,但它返回 NULL。 在其他帖子中,我读到这可能是由于 Lucene 演示索引文件中声明的 TextField 内容引起的。

在尝试了很多不同的方法之后,我得出的结论是我需要帮助,而我就在这里。

我现在的问题是我是否需要调整索引过程(如何调整?)或者除了 getTermVector 之外还有其他方法可以访问 ScoreProvider 中的索引?

最佳答案

我能够自己解决这个问题,并且如果有人发现这个问题正在寻找答案,我想分享我的解决方案。

问题确实是由于内容是 TextField 引起的 https://lucene.apache.org/core/7_1_0/demo/src-html/org/apache/lucene/demo/IndexFiles.html

为了解决这个问题,我们必须构建自己的字段,我用它替换了 IndexFile 中的第 193 行

FieldType myFieldType = new FieldType(TextField.TYPE_STORED);
myFieldType.setOmitNorms(true);
myFieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
myFieldType.setStored(false);
myFieldType.setStoreTermVectors(true);  
myFieldType.setTokenized(true);
myFieldType.freeze();
Field myField = new Field("contents",
                new BufferedReader(new InputStreamReader(stream, 
                StandardCharsets.UTF_8)),
                myFieldType);
doc.add(myField);

这允许在 customScore 函数中使用 getTermVector。希望这对将来的人有所帮助。

关于java - 如何在 Lucene 7.x 中使用 CustomScoreQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48383780/

相关文章:

c# - Lucene 和正斜杠

lucene 和本体论

java - 搜索结果归因于 Hibernate Search 中的字段

java - Lucene Proximity 搜索包含两个以上单词的短语

Java正则表达式替换问题

java - 在保持LogicalTypes的同时将Parquet/Avro GenericRecord写入JSON

java - Hibernate 抛出 MultipleBagFetchException - 不能同时获取多个包

java - 如何显示表的列名

java - Maven 连接到 IDE

hibernate - 由 : java. lang.IllegalArgumentException 引起 : java. lang.Object 不是索引实体或索引实体的子类