2017-07-10 50 views
2

我使用的與Boost FieldsElastic Search 1.7。它工作正常,但在某些情況下,我沒有得到預期的結果。
查詢:彈性搜索中帶有提升字段的查詢字符串

query 
{ 
    "from": 0, 
    "size": 10, 
    "explain": true, 
    "query": { 
     "function_score": { 
     "query": { 
      "query_string": { 
       "query": "account and data", 
       "fields": [ 
        "title^5" 
        "authors^4", 
        "year^5", 
        "topic^6" 
       ], 
       "default_operator": "and", 
       "analyze_wildcard": true 
      } 
     }, 
     "score_mode": "sum", 
     "boost_mode": "sum", 
     "max_boost": 100 
     } 
    } 
} 

示例數據:

{ 
    "took": 50, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 4, 
     "max_score": 12.833213, 
     "hits": [ 
     { 
      "_id": "19850", 
      "_score": 12.833213, 
      "_source": { 
       "ID": "19850", 
       "Year": "2010", 
       "Title": "account and data :..." 
      } 
     }, 
     { 
      "_id": "16896", 
      "_score": 11.867042, 
      "_source": { 
       "ID": "16896", 
       "Year": "2014", 
       "Title": "effectivness of data..." 
      } 
     }, 
     { 
      "_id": "59862", 
      "_score": 9.706333, 
      "_source": { 
       "ID": "59862", 
       "Year": "2007", 
       "Title": "best system to..." 
      } 
     }, 
     { 
      "_id": "18501", 
      "_score": 9.685843, 
      "_source": { 
       "ID": "18501", 
       "Year": "2010", 
       "Title": "management of..." 
      } 
     } 
     ] 
} 

我通過使用查詢得到上述樣品的數據,這是按照期望。但是現在,如果我將的year增加到100,那麼我期望第四名成績在第三名,第三名成績在第四名。我嘗試了很多東西,但我不知道我做錯了什麼。

回答

4

僅當查詢與您正在提升的字段相匹配並且它將得分彈性搜索計算與您定義的提升相乘時,纔會使用提升。在您的查詢中,您正在尋找"account and data",並且這與任何一年都不匹配,因此不會使用今年的提振。

您是否想要訂購年份?如果是這樣的話,你可以嘗試添加field_value_factor到您的查詢是這樣的:

"query" : { 
    "function_score": { 
     "query": { <your query goes here> }, 
     "field_value_factor": { 
      "field": "year" 
     } 
    } 
} 

這將乘以一年比分彈性搜索計算因此將需要一年到帳戶,而不在今年必要訂貨。你可以在這裏閱讀更多關於https://www.elastic.co/guide/en/elasticsearch/guide/current/boosting-by-popularity.html

您可以隨時使用說明工具來弄清楚彈性搜索是如何產生分數的,從而以該順序返回結果。 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html

+0

我是否需要添加所有字段,無論我想要考慮訂購? –

+0

您需要在訂購時添加您想要考慮的字段。但是'field_value_factor'' field'屬性只需要一個字段。如果你想使它成爲兩個字段的組合並添加自定義邏輯,你應該使用'script_score'來代替這裏提到的https://www.elastic.co/guide/en/elasticsearch/guide/current/function-score -query.html –

+0

如果這解決了您的問題,請記住將答案標記爲已接受。 –