2017-07-10 43 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

如果这解决了您的问题,请记住将答案标记为已接受。 –