Vineet Vinayak
Vineet Vinayak

Reputation: 63

how to match multiple fields inside filter keyword in elastic search query?

I want to add one more field inside match inside function block in my query, but when i am adding, i am getting an error ------ "reason" : "[match] query doesn't support multiple fields, found [gender] and [id]",

How do i do it?

GET exp/_search
{
  "_source": ["score","answer","gender","id"]
  , "query": {
    "function_score": {
      "query": {
        "match": {
          "score": 10
        }
      },
      "functions": [
        {
          "filter": {
            "match":{
              "gender":"male",
              "id":1
            }
          },
          "weight": 2
        }
      ]
    }
  }
}

Upvotes: 0

Views: 531

Answers (1)

Sagar Patel
Sagar Patel

Reputation: 5486

You can create bool query inside filter and it will be resolved your issue. match query does not support providing 2 diffrent field and values. You can use bool query for same purpose.

{
  "_source": [
    "score",
    "answer",
    "gender",
    "id"
  ],
  "query": {
    "function_score": {
      "query": {
        "match": {
          "score": 10
        }
      },
      "functions": [
        {
          "filter": {
            "bool": {
              "must": [
                {
                  "match": {
                    "gender": "male"
                  }
                },
                {
                  "match": {
                    "id": 1
                  }
                }
              ]
            }
          },
          "weight": 2
        }
      ]
    }
  }
}

Also, If you want to apply two different boosting value for gender and id then you can give two filter clause as shown below:

{
  "_source": [
    "score",
    "answer",
    "gender",
    "id"
  ],
  "query": {
    "function_score": {
      "query": {
        "match": {
          "score": 10
        }
      },
      "functions": [
        {
          "filter": {
            "match": {
              "gender": "male"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "match": {
              "id": 1
            }
          },
          "weight": 1
        }
      ]
    }
  }
}

Upvotes: 2

Related Questions