具有功能评分的Elasticsearch DSL提升

时间:2018-07-30 10:36:48

标签: elasticsearch

如何增强我未明确查询的字段?

在以下查询中,我正在搜索特定公司组内部的员工Brenda。现在,多个公司可以属于一个公司组。

但是我想给我登录的公司打分,比其他公司更高。 因此,如果我以公司sky blue的身份登录并且从公司sky greensky red聘请了员工,我希望他们的分数稍慢一些。

查询

GET employee-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "brenda",
            "fields": [
              "firstName"
            ]
          }
        }
      ],
      "filter": {
        "term": {
          "companyGroupId": 1595
        }
      }
    }
  },
  "aggs": {
    "comapany_names": {
      "terms": {
        "field": "companyName.keyword",
        "size": 5
      }
    }
  },
  "highlight": {
    "order": "score",
    "pre_tags": [
      "<tag1>"
    ],
    "post_tags": [
      "</tag1>"
    ],
    "fields": {
      "firstName": {},
      "number": {}
    }
  }
}

结果

"hits": [
      {
        "_index": "employee-index",
        "_type": "_doc",
        "_id": "107223",
        "_score": 7.490565,
        "_source": {
          "employeeId": 123,
          "companyGroupId": 1595,
          "companyId": 99,",
          "preferredName": "",
          "firstName": "Brenda",
          "middleName": "",
          "lastName": "Bently",
          "fullName": "Brenda Bently",
          "companyName": "Green Ice",
        },
        "highlight": {
          "firstName": [
            "<tag1>Brenda</tag1>"
          ]
        }
      },
      {
        "_index": "employee-index",
        "_type": "_doc",
        "_id": "7061",
        "_score": 7.490565, // <-- I would like this document to score higher because i am logged in as 'sky blue'
        "_source": {
          "employeeId": 1234,
          "companyGroupId": 1595,
          "companyId": 100,
          "preferredName": "Brenda",
          "firstName": "Brenda",
          "middleName": "Kate",
          "lastName": "Eaton",
          "fullName": "Brenda Eaton",
          "companyName": "Sky Blue",
        },
        "highlight": {
          "firstName": [
            "<tag1>Brenda</tag1>"
          ]
        }
      }

1 个答案:

答案 0 :(得分:0)

因此,事实证明,这比我要容易得多,您只需要添加function_score

GET employee-index/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "brenda",
                "fields": [
                  "firstName"
                ]
              }
            }
          ],
          "filter": {
            "term": {
              "companyGroupId": 1595
            }
          }
        }
      },
      "functions": [ <--- This is the magic right here
        {
          "filter": {
            "match": {
              "companyId": 158
            }
          },
          "weight": 2
        }
      ]
    }
  },
   "aggs": {
    "comapany_names": {
      "terms": {
        "field": "companyName.keyword",
        "size": 5
      }
    }
  },
  "highlight": {
    "order": "score",
    "pre_tags": [
      "<tag1>"
    ],
    "post_tags": [
      "</tag1>"
    ],
    "fields": {
      "firstName": {},
      "number": {}
    }
  }
}