弹性搜索:如何使用两个不同的多个匹配字段? [英] Elasticsearch: How to use two different multiple matching fields?

查看:113
本文介绍了弹性搜索:如何使用两个不同的多个匹配字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些类似于和过滤示例,除了每个字段中都有应该的术语,而不是示例中的字段类型。我想出了以下内容:

I want to do something much like the 'and' filter example except with terms with a 'should' in each one, instead of the field types in the example. I came up with the following:

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

但是,我得到这个错误:

However, I get this error:

[bool] filter does not support [minimum_should_match];

还有另一种方式来谈论我正在尝试做什么,还是我在右边跟踪?或者这在弹性搜索中是不可能的?

Is there another way to go about what I'm attempting to do, or am I on the right track? Or is this just not possible in elasticsearch?

推荐答案

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

Each bool query clause can contain multiple clauses. A terms query (http://www.elasticsearch.org/guide/reference/query-dsl/terms-query/) is an easy way to specify that the query should match any of a list of terms. Here's that uses terms queries to say fruit must be one of orange, apple, pear and color must be one of red, yellow, green, in addition to the ids query you had before:

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

这篇关于弹性搜索:如何使用两个不同的多个匹配字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆