Elasticsearch - boost nested query with higher value
Asked Answered
P

1

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.

Polyphonic answered 28/9, 2015 at 15:15 Comment(0)
D
2

I suggest using function_score to do this and more specifically the field_value_factor function which allows to use a field value in the scoring computation (with an optional factor by which to multiply the field value):

{
  "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
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
Dibs answered 14/9, 2017 at 10:52 Comment(1)
getting issue :: "TransportError: TransportError(500, u'search_phase_execution_exception', u'org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData')"Connors

© 2022 - 2024 — McMap. All rights reserved.