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

时间:2013-09-24 19:35:50

标签: elasticsearch

我想做的事情很像'and' filter example,除了每个术语中带有'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
          }
        }
      ]
    }
  }
}

然而,我收到此错误:

[bool] filter does not support [minimum_should_match];

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

2 个答案:

答案 0 :(得分:11)

每个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
          }
        }
      ]
    }
  }
}

答案 1 :(得分:0)

我认为您无需指定bool filter。如果我理解您要完成的工作,terms filterand 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.