12

I have a query (well a part of it - rest is unimportant like pagination):

  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "is_active": true
              }
            }
          ],
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "nested": {
                      "path": "skills",
                      "query": {
                        "bool": {
                          "must": [
                            {
                              "bool": {
                                "must": [
                                  {
                                    "range": {
                                      "skills.value": {
                                        "gte": "2"
                                      }
                                    }
                                  },
                                  {
                                    "term": {
                                      "skills.skill.name": "php"
                                    }
                                  }
                                ]
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                ],
                "boost": 2
              }
            }
          ]
        }
      }
    }
  }

It's for searching profile, which has a skill "PHP" with value 2 or more. User can search for multiple skills=>values pair. It's working fine, but I have one question:

How to make a little boost for matched skills which has higher skills.value, just to make person with PHP value 3 be higher in search results than someone with PHP 2 even if both are correct match.

4

1 回答 1

2

我建议使用function_score来做到这一点,更具体地说是允许在评分计算中使用字段值的field_value_factor函数(带有一个可选的因子来乘以字段值):

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "is_active": true
              }
            }
          ],
          "should": [
            {
              "nested": {
                "path": "skills",
                "query": {
                  "function_score": {
                    "query": {
                      "bool": {
                        "must": [
                          {
                            "range": {
                              "skills.value": {
                                "gte": "2"
                              }
                            }
                          },
                          {
                            "term": {
                              "skills.skill.name": "php"
                            }
                          }
                        ]
                      }
                    },
                    "functions": [
                      {
                        "field_value_factor": {
                          "field": "skills.value",
                          "factor": 2
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
于 2017-09-14T10:52:48.617 回答