如何在弹性搜索中基于频率进行评分

时间:2017-02-01 14:10:37

标签: elasticsearch

我将elasticsearch与用户数据库结合使用。每个用户都有多个工作,每个工作都有一个职业ID。 我使用以下查询来查找具有特定作业的用户:

"explain": true, 
"query": {
    "bool": {
        "should": [
            {
                "bool": {
                    "must": [
                        {
                            "term": {
                                  "positions.career.id": {
                                  "value": 31
                                }
                            }
                        }
                    ]
                }
            }
        ],
        "minimum_number_should_match": 1
    }
}

然而,这个查询给出了所有结果得分为1,表明他们在至少一个职位上有这个职业。

我需要得分来反映用户所拥有的职位数量。

有没有办法用elasticsearch做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用elasticsearch的高级评分模块来访问索引级别评分属性,以根据术语频率对文档进行评分。

Official documentation

您可以在function score脚本得分查询中使用它来根据elasticsearch中的频率进行评分

对于ES 2.4.1和默认脚本语言groovy

POST index_name1/test_type
{
  "title" : "harley harley harley",
  "nested_one" : [{
    "some_id" : 78
  },
  {
    "some_id" : 80
  },{
    "some_id" : 100
  }],
  "nested_two" : [{
    "some_id" : 79
  },
  {
    "some_id" : 80
  },{
    "some_id" : 101
  }]
}

POST index_name1/test_type
    {
      "title" : "harley harley",
      "nested_one" : [{
        "some_id" : 78
      },
      {
        "some_id" : 80
      },{
        "some_id" : 100
      }],
      "nested_two" : [{
        "some_id" : 79
      },
      {
        "some_id" : 80
      },{
        "some_id" : 101
      }]
    }

POST index_name1/_search
{
  "query": {"function_score": {
    "query": {"match_all": {}},
    "functions": [
      {"script_score": {

        "script": "_index['title']['harley'].tf()"
      }}
    ]
  }}
}

以下是上述查询的回复。

{
  "took": 173,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 3,
    "hits": [
      {
        "_index": "index_name1",
        "_type": "test_type",
        "_id": "AVn9aDOUKkYhaW9Iz5wL",
        "_score": 3,
        "_source": {
          "title": "harley harley harley",
          "nested_one": [
            {
              "some_id": 78
            },
            {
              "some_id": 80
            },
            {
              "some_id": 100
            }
          ],
          "nested_two": [
            {
              "some_id": 79
            },
            {
              "some_id": 80
            },
            {
              "some_id": 101
            }
          ]
        }
      },
      {
        "_index": "index_name1",
        "_type": "test_type",
        "_id": "AVn9aBQGKkYhaW9Iz5wJ",
        "_score": 2,
        "_source": {
          "title": "harley harley",
          "nested_one": [
            {
              "some_id": 78
            },
            {
              "some_id": 80
            },
            {
              "some_id": 100
            }
          ],
          "nested_two": [
            {
              "some_id": 79
            },
            {
              "some_id": 80
            },
            {
              "some_id": 101
            }
          ]
        }
      },
      {
        "_index": "index_name1",
        "_type": "test_type",
        "_id": "AVn9aBhlKkYhaW9Iz5wK",
        "_score": 2,
        "_source": {
          "title": "harley harley",
          "nested_one": [
            {
              "some_id": 78
            },
            {
              "some_id": 80
            },
            {
              "some_id": 100
            }
          ],
          "nested_two": [
            {
              "some_id": 79
            },
            {
              "some_id": 80
            },
            {
              "some_id": 101
            }
          ]
        }
      }
    ]
  }
}

希望这有帮助。