java 是否有可能在数组中找到一个对象,该对象只有那些在请求中指定的元素?ElasticSearch

yx2lnoni  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(101)

假设我有3个具有下一个结构的对象:

{
        "name": "Test_1",
        "attributes":[
            {
                "name": "attribute_1"
            },
            {
                "name": "attribute_2"
            }
        ]
    },
    {
        "name": "Test_2",
        "attributes":[
            {
                "name": "attribute_1"
            },
            {
                "name": "attribute_2"
            },
    {
                "name": "attribute_3"
            }
        ]
    }
    {
        "name": "Test_3",
        "attributes":[
            {
                "name": "attribute_2"
            },
            {
                "name": "attribute_3"
            }
        ]
    }

存储在ElasticSearch中,我想查找只包含这些值的对象attributes.name = attribute_1,attribute_2
所以,作为结果,我想得到第一个对象,因为这个对象只包含那些指定的值,不多也不少

e0bqpujr

e0bqpujr1#

如果您的字段是嵌套类型,我相信这个查询应该会对您有所帮助。

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "attributes",
            "query": {
              "bool": {
                "must_not": [
                  {
                    "bool": {
                      "should": [
                        {
                          "match": {
                            "attributes.name": "attribute_1"
                          }
                        },
                        {
                          "match": {
                            "attributes.name": "attribute_2"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

相关问题