2017-09-03 129 views
0

我使用Elasticsearch v5.3.2Elasticsearch查詢返回奇怪的排序(根據分數)導致

我有以下映射:

{ 
    "mappings":{ 
     "info":{ 
     "_all":{ 
      "enabled": false 
     }, 
     "properties":{ 
      "info":{ 
       "properties":{ 
        "email":{ 
        "doc_values":"false", 
        "fields":{ 
         "ngram":{ 
          "analyzer":"custom_nGram_analyzer", 
          "type":"text" 
         } 
        }, 
        "type":"keyword" 
        } 
       } 
      } 
     } 
    } 
    }, 
    "settings":{ 
     "analysis":{ 
     "analyzer":{ 
      "custom_nGram_analyzer":{ 
       "filter":[ 
        "lowercase", 
        "asciifolding", 
        "custom_nGram_filter" 
       ], 
       "tokenizer":"whitespace", 
       "type":"custom" 
      } 
     }, 
     "filter":{ 
      "custom_nGram_filter":{ 
       "max_gram":16, 
       "min_gram":3, 
       "type":"ngram" 
      } 
     } 
     } 
    } 
} 

我看到的文檔分值方面非常奇怪的結果,當我執行以下查詢:

GET /info_2017_08/info/_search 
{ 
    "query": { 
     "multi_match": { 
      "query": "hotmail", 
      "fields": [ 
       "info.email.ngram" 
      ] 
     } 
    } 
} 

它帶來以下結果:

"hits": { 
    "total": 3, 
    "max_score": 1.3834574, 
    "hits": [ 
     { 
     "_index": "info_2017_08", 
     "_type": "info", 
     "_id": "AV4uQnCjzNcTF2GMY730", 
     "_score": 1.3834574, 
     "_source": { 
      "info": { 
       "email": "[email protected]" 
      } 
     } 
    }, 
    { 
     "_index": "info_2017_08", 
     "_type": "info", 
     "_id": "AV4uQm93zNcTF2GMY73x", 
     "_score": 0.3967861, 
     "_source": { 
      "info": {    
       "email": "[email protected]" 
      } 
     } 
    }, 
    { 
     "_index": "info_2017_08", 
     "_type": "info", 
     "_id": "AV4uQmYbzNcTF2GMY73P", 
     "_score": 0.36409757, 
     "_source": { 
      "info": { 
       "email": "[email protected]" 
      } 
     } 
    } 
    ] 
} 

現在注意分數。如果第一個結果比第二個結果更高,第一個結果是... @ gmail.com,第二個結果是... @ hotmail.com,如果我搜索了術語「hotmail」?

第二個應該使用ngrams「mail」和「hotmail」來匹配查詢,而第一個應該只通過ngram「mail」匹配查詢,那麼結果是什麼原因呢?

在此先感謝。

回答

1

Elasticsearch使用TF/IDF統計信息獨立計算每個碎片上文檔的分數。正因爲如此,如果你有兩個碎片與下一個內容:

  1. 「info.email」: 「[email protected]
  2. 「info.email」:「[email protected]。 「info.email」:「[email protected]

然後,對於您的具體查詢,來自第一個分片的單個文檔將比第二個分片中的任何文檔具有更高的分數。

您可以檢查使用下一個API調用每個碎片的內容:GET index/_search?preference=_shards:0

+0

是啊,你說得對,我已經發現了這一點。當我使用查詢參數'search_type = dfs_query_then_fetch'進行同樣的查詢時,我得到'hotmail'得分高於任何其他'gmail'。現在我必須找出搜索類型在查詢性能方面的差異。如果你有這方面的信息,我想知道。謝謝。 –

+2

它會影響性能,因爲它需要在分片之間進行額外往返 - 詳細信息請參見[https://elastic.co/blog/understanding-query-then-fetch-vs-dfs-query-then-取)。在這個[ES-guide](https://elastic.co/guide/en/elasticsearch/guide/current/relevance-is-broken.html)中,你甚至可以找到一句話:*不要在生產中使用dfs_query_then_fetch。 .. *作爲替代方案,您可以使用[Routing](https://elastic.co/guide/en/elasticsearch/reference/5.5/mapping-routing-field.html)進行實驗,以確保您的所有相關文檔都已發佈到同一個分片,或使用單個分片。 – Joanna