5

我想做一些很像“和”过滤器示例的事情,除了每个术语都带有“应该”,而不是示例中的字段类型。我想出了以下内容:

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

但是,我收到此错误:

[bool] filter does not support [minimum_should_match];

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

4

2 回答 2

12

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

{
  "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
          }
        }
      ]
    }
  }
}
于 2013-09-24T21:12:12.303 回答
0

我认为您不需要指定bool filter。如果我正确理解您要完成的工作,则term filter with和 filter就足够了。所以是这样的:

# Delete index
#
curl -s -X DELETE 'http://localhost:9200/bool-filter-test' ; echo

# Create index
#
curl -s -XPUT 'http://localhost:9200/bool-filter-test/' -d '{
  "mappings": {
    "document": {
      "properties": {
        "color": {
          "type": "string",
          "index": "not_analyzed"
        },
        "fruit": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}' ; echo

# Index some documents
#
curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/1?pretty=true' -d '{
  "fruit" : "apple",
  "color" : "red"
}' ; echo

curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/2?pretty=true' -d '{
  "fruit" : "apple",
  "color" : "yellow"
}' ; echo

curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/3?pretty=true' -d '{
  "fruit" : "apple",
  "color" : "green"
}' ; echo

curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/4?pretty=true' -d '{
  "fruit" : "banana",
  "color" : "green"
}' ; echo

curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/5?pretty=true' -d '{
  "fruit" : "banana",
  "color" : "yellow"
}' ; echo

curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/6?pretty=true' -d '{
  "fruit" : "pear",
  "color" : "green"
}' ; echo

curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/7?pretty=true' -d '{
  "fruit" : "pear",
  "color" : "yellow"
}' ; echo

curl -s -XPUT 'http://localhost:9200/bool-filter-test/document/7?pretty=true' -d '{
  "fruit" : "pear",
  "color" : "red"
}' ; echo


# Refresh index
#
curl -s -XPOST 'http://localhost:9200/bool-filter-test/_refresh'; echo


# This query should return only red apples and pears
#
curl -s -X POST 'http://localhost:9200/bool-filter-test/_search?pretty' -d '{
  "query" : {
    "match_all" : { }
  },
  "filter" : {
    "and" : [
      {
        "terms" : { "fruit" : ["apple", "pear"] }
      },
      {
        "terms" : { "color" : ["red"] }
      }
    ]
  }
}'

您甚至可以指定(execution根据bool文档)Generates a term filter (which is cached) for each term, and wraps those in a bool filter. The bool filter itself is not cached as it can operate very quickly on the cached term filters.

于 2013-09-24T21:14:03.577 回答