Elasticsearch是否支持AND查询多个值? [英] Does Elasticsearch support AND query for multiple values?

查看:995
本文介绍了Elasticsearch是否支持AND查询多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ElasticSearch v:5.3

ElasticSearch v: 5.3

我在弹性搜索中存储了一个项数组.

I've one items array store in elastic search.

userId: "123"
items: [
  {
    "range": {
      "from": 10,
      "to": 30
    },
    "id": "1"
  },
  {
    "range": {
      "from": 20,
      "to": 100
    },
    "id": "2"
  },
  {
    "range": {
      "from": 5,
      "to": 90
    },
    "id": "3"
  },
]

这仅是一条记录.将此视为包含项目键的多个记录. 现在,我想执行具有ID的搜索查询,并且它是受尊重的范围.所以我目前的查询是这样的

This is only single record. Consider this as multiple records which contains items key. Now i want to perform search query with id and it's respected range. So my current query is like this

GET product/item/_search{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "items.id": "1"
          }
        },
        {
          "range": {
            "items.range.from": {
              "gte": 20,
              "lte": 30
            }
          }
        },
        {
          "range": {
            "items.range.to": {
              "gte": 20,
              "lte": 30
            }
          }
        },

      ]
    }
  }
}

当前,这将执行一个搜索查询,该查询的id = 1,范围值从> = 20到< =30.因此它将返回所有包含id = 2且范围在20到30之间的记录.因此,即使在其他记录中,如果没有id = 1,它也会搜索范围值,如果范围匹配,它将返回记录.

Currently this will perform a search query which has id=1 and range values from >= 20 and to <= 30. So it will return all the records containing id=2 and also which has range between 20 to 30. So even if in other records if id=1 is not present it will search for range values an will return records if range matches.

我想搜索的不是id = 1的记录,而id的值应该在20到30之间. 所以我的问题是弹性搜索是否支持AND之间的这种查询?

Instead of this i want to search for records which has id=1 and that id's value should between 20 to 30. So My question is does elastic search supports this kind of in between AND query?

推荐答案

您需要将items字段设置为

You need your items field to be nested in your mapping, like this:

PUT product
{
  "mappings": {
    "item": {
      "properties": {
        "itemId": {
          "type": "long"
        },
        "items": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "long"
            },
            "range": {
              "properties": {
                "from": {
                  "type": "long"
                },
                "to": {
                  "type": "long"
                }
              }
            }
          }
        }
      }
    }
  }
}

然后,您将可以像这样查询:

Then you'll be able to query like this:

GET product/item/_search
{
  "query": {
    "nested": {
      "path": "items",
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "items.id": "1"
              }
            },
            {
              "range": {
                "items.range.from": {
                  "gte": 20,
                  "lte": 30
                }
              }
            },
            {
              "range": {
                "items.range.to": {
                  "gte": 20,
                  "lte": 30
                }
              }
            }
          ]
        }
      }
    }
  }
}

PS:建议您查看 数据类型,而不是您的range.from/to对象.只是我的两分钱

PS: I suggest that you look at the integer_range data type instead of your range.from/to object. Just my two cents

这篇关于Elasticsearch是否支持AND查询多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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