Elasticsearch:如何使用两个不同的多个匹配字段?

bet*_*eth 5 elasticsearch

我想做一些类似于'和'过滤器示例的操作,除了每个中带有'should'的术语,而不是示例中的字段类型.我想出了以下内容:

    {
  "query": {
    "bool": {
      "must": [
        {
          "ids": {
            "type": "foo",
            "values": [
              "fff",
              "bar",
              "baz",
            ]
          }
        }
      ]
    }
  },
  "filter": {
    "and": {
      "filters": [
        {
          "bool": {
            "should": {
              "term": {
                "fruit": [
                  "orange",
                  "apple",
                  "pear",
                ]
              }
            },
            "minimum_should_match": 1
          }
        },
        {
          "bool": {
            "should": {
              "term": {
                "color": [
                  "red",
                  "yellow",
                  "green"
                ]
              }
            },
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

[bool] filter does not support [minimum_should_match];
Run Code Online (Sandbox Code Playgroud)

还有另一种方法可以解决我正在尝试做的事情,还是我走在正确的轨道上?或者这在弹性搜索中是不可能的?

kie*_*lni 12

每个bool查询子句都可以包含多个子句.术语查询(http://www.elasticsearch.org/guide/reference/query-dsl/terms-query/)是一种指定查询应与任何术语列表匹配的简单方法.这里使用术语查询说水果必须是橙色,苹果,梨和颜色之一必须是红色,黄色,绿色之一,除了你之前的ID查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "ids": {
            "type": "foo",
            "values": [
              "fff",
              "bar",
              "baz"
            ]
          }
        },
        {
          "terms": {
            "fruit": [ "orange", "apple","pear" ],
            "minimum_should_match": 1
          }
        },
        {
          "terms": {
            "color": [ "red", "yellow", "green" ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • OMG这个有效.这个查询是我几周来见过的最美好的事情.我要说出我的长子"stackoverflow". (16认同)