ElasticSearch嵌套查询无法按预期工作

问题描述 投票:0回答:1

我需要一个快速的帮助,我只需要在同一个数组中满足我的某些条件时才能获取整个文档。

在一个阵列块中的条件满足所有这三个条件。即

  • “profile.bud.buddies.code”:“1”
  • “profile.bud.buddies.moredata.key”: “一”
  • “profile.bud.buddies.moredata.val”:“0”

不幸的是,现在它正在遍历整个文档并尝试匹配每个数组中的值,因此可以使得code = 1在一个数组中匹配,key = 1在其他数组中匹配,val = 0在第三个数组中阵列。在这种情况下会发生什么,它会返回整个文档,而实际上这并没有单独在一个数组中完成,所以不应该将文档返回给我。

我把moredata作为嵌套类型,但仍然无法通过。请帮忙。

查询我正在使用

"query": {
    "bool": {
      "should": [
        {
          "match": {
            "profile.bud.buddies.code": "1"
          }
        }
      ]
    },
    "nested": {
    "path": "profile.bud.buddies.moredata",
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "profile.bud.buddies.moredata.key": "one"
            }
          },
          {
            "match": {
              "profile.bud.buddies.moredata.val": "0"
            }
          }
        ]
      }
    }
  }
}

文件结构

"profile": {
  "x":{},
  "y":{},
  "a":{},
  "b":{},
  "bud":{
    "buddies": [
    {
        "code":"1",
        "moredata": [
            {
                "key": "one",
                "val": 0,
                "setup": "2323",
                "data": "myid"
            },
            {
                "key": "two",
                "val": 1,
                "setup": "23",
                "data": "id"
            }]
    },
    {
        "code":"2",
        "moredata": [
            {
                "key": "two",
                "val": 0,
                "setup": "2323",
                "data": "myid"
            },
            {
                "key": "three",
                "val": 1,
                "setup": "23",
                "data": "id"
            }]
    }]
}

这就是我标记映射的方式;

"profile": {
"bug": {
                                "properties": {
                                    "buddies": {
                                        "properties": {
                                            "moredata": {
                                                "type": "nested",
                                                "properties": {
                            "key": {"type": "string"},
                            "val": {"type": "string"}

elasticsearch nested nest elasticsearch-plugin elasticsearch-5
1个回答
0
投票

您的查询结构不正确,它应该是类似的

"query": {
  "bool": {
    "must": [{
        "match": {
          "profile.bud.buddies.code": "1"
        }
      },
      {
        "nested": {
          "path": "profile.bud.buddies.moredata",
          "query": {
            "bool": {
              "must": [{
                  "match": {
                    "profile.bud.buddies.moredata.key": "one"
                  }
                },
                {
                  "match": {
                    "profile.bud.buddies.moredata.val": "0"
                  }
                }
              ]
            }
          }
        }
        ]
    }
  }
}

其中nested查询位于外部must查询的bool子句数组中。请注意,profile.bud.buddies.moredata必须映射为nested数据类型。

© www.soinside.com 2019 - 2024. All rights reserved.