在弹性搜索中使用提升字段查询字符串

时间:2017-07-10 13:51:46

标签: elasticsearch elasticsearch-query solr-boost

我在Query String中使用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..."
            }
         }
      ]
}

我通过使用查询得到了上面的示例数据,这是按照期望的。但是现在,如果我将weight的{​​{1}}增加到100,那么我预计第3个位置的第4个结果和第4个位置的第3个结果。我尝试了很多东西,但我不知道我做错了什么。

1 个答案:

答案 0 :(得分: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