【问题标题】:Porting code from lucene to elasticsearch将代码从 lucene 移植到 elasticsearch
【发布时间】:2017-05-02 17:16:47
【问题描述】:

我必须遵循我想从 lucene 6.5.x 移植到 elasticsearch 5.3.x 的简单代码。

但是,分数不同,我希望获得与 lucene 中相同的分数结果。

例如,idf:

Lucenes docFreq 为 3(3 个文档包含术语“d”),docCount 为 4(具有此字段的文档)。 Elasticsearch 有 1 个 docFreq 和 2 个 docCount(或 1 和 1)。我不确定这些值在 elasticsearch 中是如何相互关联的......

评分的另一个不同之处是 avgFieldLength:

Lucene 的 14 / 4 = 3.5 是正确的。每个分数结果的 Elasticsearch 都不同 - 但所有文档都应该相同...

您能否告诉我,我在 elasticsearch 中错过了哪些设置/映射以使其像 lucene 一样工作?

IndexingExample.java:

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.document.Field;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class IndexingExample {
    private static final String INDEX_DIR = "/tmp/lucene6idx";

    private IndexWriter createWriter() throws IOException {
        FSDirectory dir = FSDirectory.open(Paths.get(INDEX_DIR));
        IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
        return new IndexWriter(dir, config);
    }

    private List<Document> createDocs() {
        List<Document> docs = new ArrayList<>();
        FieldType summaryType = new FieldType();
        summaryType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
        summaryType.setStored(true);
        summaryType.setTokenized(true);

        Document doc1 = new Document();
        doc1.add(new Field("title", "b c d d d", summaryType));
        docs.add(doc1);
        Document doc2 = new Document();
        doc2.add(new Field("title", "b c d d", summaryType));
        docs.add(doc2);
        Document doc3 = new Document();
        doc3.add(new Field("title", "b c d", summaryType));
        docs.add(doc3);
        Document doc4 = new Document();
        doc4.add(new Field("title", "b c", summaryType));
        docs.add(doc4);

        return docs;
    }

    private IndexSearcher createSearcher() throws IOException {
        Directory dir = FSDirectory.open(Paths.get(INDEX_DIR));
        IndexReader reader = DirectoryReader.open(dir);
        return new IndexSearcher(reader);
    }

    public static void main(String[] args) throws IOException, ParseException {
        // indexing
        IndexingExample app = new IndexingExample();
        IndexWriter writer = app.createWriter();
        writer.deleteAll();
        List<Document> docs = app.createDocs();
        writer.addDocuments(docs);
        writer.commit();
        writer.close();

        // search
        IndexSearcher searcher = app.createSearcher();
        Query q1 = new TermQuery(new Term("title", "d"));
        TopDocs hits = searcher.search(q1, 20);
        System.out.println(hits.totalHits + " docs found for the query \"" + q1.toString() + "\"");
        int num = 0;
        for (ScoreDoc sd : hits.scoreDocs) {
            Explanation expl = searcher.explain(q1, sd.doc);
            System.out.println(expl);
        }
    }
}

弹性搜索:

DELETE twitter

PUT twitter/tweet/1
{
    "title" : "b c d d d"
}

PUT twitter/tweet/2
{
    "title" : "b c d d"
}

PUT twitter/tweet/3
{
    "title" : "b c d"
}

PUT twitter/tweet/4
{
    "title" : "b c"
}

POST /twitter/tweet/_search
{
    "explain": true, 
    "query": {
        "term" : {
            "title" : "d"
        }
    }
}

【问题讨论】:

    标签: java elasticsearch lucene


    【解决方案1】:

    jimczy 的帮助下解决了问题:

    不要忘记 ES 默认创建一个包含 5 个分片的索引,并且 docFreq 和 docCount 是按分片计算的。您可以创建一个 具有 1 个分片的索引或使用 dfs 模式来计算分布式统计信息: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#dfs-query-then-fetch

    此搜索查询 (dfs_query_then_fetch) 按预期工作:

    POST /twitter/tweet/_search?search_type=dfs_query_then_fetch
    {
            "explain": true, 
            "query": {
                    "term" : {
                            "title" : "d"
                    }
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多