0
votes

I'm very new to Elasticsearch. I'm using it to filtering and also boosting some fields at query time. This is the code part for boosting and filtering:

"query": {
"bool": {
  "must": [
    {
      "bool": {
        "should": [
          {
            "multi_match": {
              "type": "best_fields",
              "query": "exampleKeyword",
              "fields": [
                "exampleField1^0",
                "exampleField2^50",
                "exampleField3^10",
                "exampleField4^10",
                "exampleField5^5"                  
              ],
              "boost": 50
            }
          }]
      }
    }
  ],
  "filter": [
    {
      "bool": {
        "must": [
          {
            "bool": {
              "must": [
                {
                  "term": {
                    "bla": {
                      "value": ""
                    }
                  }
                }
              ]
            }
          }, {
            "term": {
              "active": {
                "value": "true"
              }
            }
          },
          {
            "range": {
              "closingDate": {
                "gte": "201710310000",
                "lte": "999912312359"
              }
            }
          },

Now I want to boost some specific documents. I'll give an array of integers for example Field6 and if my search results contain the elements of the array, these documents should get boosted with, I dont know, 100 to my scale.

How can I do this? Finally I dont want to expand the result set. Just want to boost more the desired ids if results contain these ids.

1
elastic.co/guide/en/elasticsearch/reference/current/… for that with a filter and different weight for each filter. And the filter itself should probably be an ids filter.Andrei Stefan

1 Answers

1
votes

Using function_score you can do something around these lines:

{
  "query": {
    "bool": {
      "must": [
        {
          "function_score": {
            "query": {
              "bool": {
                "should": [
                  {
                    "multi_match": {
                      "type": "best_fields",
                      "query": "bla",
                      "fields": [
                        "exampleField1^0",
                        "exampleField2^50",
                        "exampleField3^10",
                        "exampleField4^10",
                        "exampleField5^5"
                      ],
                      "boost": 50
                    }
                  }
                ]
              }
            },
            "functions": [
              {
                "filter": {
                  "ids": {
                    "values": [
                      1,
                      5
                    ]
                  }
                },
                "weight": 10
              }
            ],
            "score_mode": "max",
            "boost_mode": "multiply"
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "bla": {
                          "value": ""
                        }
                      }
                    }
                  ]
                }
              },
              {
                "term": {
                  "active": {
                    "value": "true"
                  }
                }
              },
              {
                "range": {
                  "closingDate": {
                    "gte": "201710310000",
                    "lte": "999912312359"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}