Home > Mobile >  Elasticsearch how to set different value with different scores for the same filed?
Elasticsearch how to set different value with different scores for the same filed?

Time:10-18

I have different type_id in an ES index , and want to give different value type_id different scores to make some type search result rank is higher . My query is

{
    "query":{
        "bool":{
            "must":[
                {"terms":{"type_id":[9,10]}}
            ],
            "should":[
                {"match":{ "display_name":{"query":"keyword","boost":10}}},
                {"match":{ "description":{"query":"keyword","boost":2}}}
            ]
        }
    }
}

I want to make type_id 9 match scores is higher than type_id 10 when display_name and description is same .

Please guide me in this problem. Thanks.

CodePudding user response:

You can group your queries like below and use boost to give more weightage to certain ids.

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "term": {
                  "type_id": {
                    "value": 9,
                    "boost": 2
                  }
                }
              },
              {
                "term": {
                  "type_id": {
                    "value": 10,
                    "boost": 1
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1, 
            "should": [
              {
                "match": {
                  "display_name": {
                    "query": "keyword",
                    "boost": 10
                  }
                }
              },
              {
                "match": {
                  "description": {
                    "query": "keyword",
                    "boost": 2
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Edit: For query in comment , you can use function_score

{
  "query": {
    "bool": {
      "minimum_should_match": 1, 
      "should": [
        {
          "function_score": {
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type_id": {
                        "value": 9
                      }
                    }
                  }
                ],
                "minimum_should_match": 1,
                "should": [
                  {
                    "match": {
                      "display_name": {
                        "query": "keyword"
                      }
                    }
                  },
                  {
                    "match": {
                      "description": {
                        "query": "keyword"
                      }
                    }
                  }
                ]
              }
            },
            "boost": "5"
          }
        },
        {
          "function_score": {
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type_id": {
                        "value": 10
                      }
                    }
                  }
                ],
                "minimum_should_match": 1,
                "should": [
                  {
                    "match": {
                      "display_name": {
                        "query": "keyword"
                      }
                    }
                  },
                  {
                    "match": {
                      "description": {
                        "query": "keyword"
                      }
                    }
                  }
                ]
              }
            },
            "boost": "4"
          }
        }
      ]
    }
  }
}
  • Related