如何在ElasticSearch查询中匹配Filter关键字中的多个字段?

twh00eeo  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(241)

我想在我的查询的函数块内的Match内再添加一个字段,但添加时出现错误-“原因”:“[Match]查询不支持多个字段,Found[Gender]和[id]”,

我该怎么做呢?

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

ff29svar1#

您可以在filter中创建bool查询,它将解决您的问题。match查询不支持提供%2个不同的字段和值。您可以使用bool查询来实现相同的目的。

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

此外,如果要为genderid应用两个不同升压值,则可以提供两个Filter子句,如下所示:

{
  "_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
        }
      ]
    }
  }
}

相关问题