ElasticSearch:如何查询精确的嵌套数组 [英] ElasticSearch: How to query exact nested array

查看:66
本文介绍了ElasticSearch:如何查询精确的嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询索引中的某种类型的文档.

I am trying to query a certain type of documents in my index.

让我们看看以下文档:

{
  "id": 1,
  "title": "My first Collection",
  "items": [
    {
      "code": "SB",
      "order": 1,
      "random": "something random"
    },
    {
      "code": "BB",
      "order": 2,
      "random": "something random"
    },
    {
      "code": "FO",
      "order": 3,
      "random": "something random"
    },
    {
      "code": "RA",
      "order": 4,
      "random": "something random"
    },
    {
      "code": "FO",
      "order": 5,
      "random": "something random"
    }
  ]
}

项目 字段是 嵌套 字段.

The items field is a nested field.

我想查询所有与确切模式匹配的文章:

I would like to query all the articles that matches the exact pattern:

{
  "items": [
    {
      "code": "SB",
      "order": 1
    },
    {
      "code": "BB",
      "order": 2
    },
    {
      "code": "FO",
      "order": 3
    },
    {
      "code": "RA",
      "order": 4
    }
  ]
}

查询的文档将具有这4个字段的所有正确组合.但是他们也可以拥有更多.上面描述的文档将与查询匹配.

The queried documents would have all those 4 items with this exact combination of fields. But they could have more too. The described document above would match the query.

我搜索了整个文档,特别是在嵌套查询部分,但没有找到任何使它起作用的方法.

I searched through the entire documentation, particularly in the nested queries part and I did not find any way to get it working.

是否有可能或者我应该实现自己的算法/脚本?

我尝试了以下操作但未成功:

I tried the following without success:

{
  "query": {
    "nested": {
      "path": "items",
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "items.code:SB AND items.order:1"
              }
            },
            {
              "query_string": {
                "query": "items.code:BB AND items.order:2"
              }
            },
            {
              "query_string": {
                "query": "items.code:FO AND items.order:3"
              }
            }
          ]
        }
      }
    }
  }
}

推荐答案

此查询有效:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "items",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "items.code": "SB"
                    }
                  },
                  {
                    "term": {
                      "items.order": "1"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "items",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "items.code": "BB"
                    }
                  },
                  {
                    "term": {
                      "items.order": "2"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "items",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "items.code": "FO"
                    }
                  },
                  {
                    "term": {
                      "items.order": "3"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

请注意,基本上就是您所使用的那个,但要为每个子查询指定嵌套,而不是整体.

Notice that is basically the one you used, BUT specifying the nested for each sub-query and not overall.

当您使用 nested 关键字时,您是在对ElasticSearch说以尝试在嵌套文档中进行匹配,该文档与主文档分开.当然,以您的示例为例,一个商品不能具有所有的代码顺序对"SB" -1 "BB" -2 "FO"-3 同时.但是您可以将它们与3个嵌套查询分开要求.

When you use the nested keyword, you are saying to ElasticSearch to try to match inside a nested document, which is separated from the main document. Of course, given your example, an item can't have all the code-order couples "SB"-1, "BB"-2 and "FO"-3 at the same time. But you can requiring them separately with the 3 nested queries.

这篇关于ElasticSearch:如何查询精确的嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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