2017-05-01 8 views
0

私はlucene 6.5.xからelasticsearch 5.3.xに移植したい簡単なコードに従わなければなりません。luceneからelasticsearchへの移植コード

しかし、スコアは異なり、私はルーキーのように同じスコアの結果を得たいと思います。

例として、IDF:

Lucenes docFreqは3である(3つのドキュメントは用語 "D" を含む)とDOCCOUNT 4(このフィールドを含むドキュメント)です。弾性抽出物は、1 docFreqおよび2 docCount(または1および1)を有する。私は得点

他異なるがavgFieldLengthです...これらの値はelasticsearchで互いにどのように関係するかを確認していない:

のLuceneは、右の4分の14 = 3.5です。 Elasticsearchはスコア結果ごとに異なりますが、これはすべての文書で同じでなければなりません...

私がelasticsearchで欠けていた設定/マッピングを教えてもらえますか?

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); 
     } 
    } 
} 

Elasticsearch:

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" 
     } 
    } 
} 

答えて

0

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" 
       } 
     } 
} 
関連する問題