gpt4 book ai didi

elasticsearch - 在Elasticsearch中查询多个嵌套对象在所有嵌套对象中是否存在另一个

转载 作者:行者123 更新时间:2023-12-02 23:30:44 25 4
gpt4 key购买 nike

在Elasticsearch 2.3上运行

我有一个foos索引。每个foo都有0个或多个存储在嵌套对象中的条。每个条具有0个或多个具有名称和值的属性。

https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html使用文档

将查询写入以下内容非常简单:

  • 返回具有至少1个bar.name = "sandwich"
  • 的所有foo
  • 返回所有没有bar.name = "pizza"的foos

  • 我似乎无法弄清楚的是如何编写一个返回所有foo的查询:
  • 至少有1个bar.name = "sandwich"
  • 没有bar.name = "pizza"

  • 我撞到这个头的时间比我想承认的还要长。任何帮助表示赞赏。

    细节:

    foo具有2个属性:
  • foo_id:一个数字
  • bar:0或更多条形的列表,存储为嵌套文档

  • 条具有2个属性:
  • bar_id:一个数字
  • properties:0或更多kv对的列表。

  • 简化了映射。真正的程序与食物无关。
    {
    "some_index" : {
    "mappings" : {
    "foo" : {
    "properties" : {
    "bar" : {
    "type" : "nested",
    "properties" : {
    "bar_id" : {
    "type" : "long"
    },
    "details" : {
    "properties" : {
    "name" : {
    "type" : "string"
    },
    "value" : {
    "type" : "long"
    }
    }
    }
    }
    },
    "foo_id" : {
    "type" : "long",
    "index" : "not_analyzed",
    "store" : true
    }
    }
    }
    }
    }
    }

    没有比萨的典型foo
    # this one does have a bar.name = sandwich
    # this one doesn't have pizza for ANY bars
    # this would match the query
    {
    "foo_id": 186456,
    "bar": [
    {
    "bar_id": 1056791,
    "details": [
    {
    "name": "taco",
    "value": 2
    },
    {
    "name": "sandwich",
    "value": 1
    }
    ]
    },
    {
    "bar_id": 1056800,
    "details": [
    {
    "name": "sandwich",
    "value": 0
    }
    ]
    }
    ]

    }

    典型的foo和一些比萨
    # this one has a bar.name = sandwich
    # this one has pizza for some bars, but not all of them
    # this would NOT match the query
    {
    "foo_id": 187390,
    "bar": [
    {
    "bar_id": 1057455,
    "details": [
    {
    "name": "taco",
    "value": 1
    }
    ]
    },
    {
    "bar_id": 1057457,
    "details": [
    {
    "name": "taco",
    "value": 0
    },
    {
    "name": "sandwich",
    "value": 1
    }
    ]
    },
    {
    "bar_id": 1057458,
    "details": [
    {
    "name": "sandwich",
    "value": 1
    },
    {
    "name": "pizza",
    "value": 0
    }
    ]
    }
    ]

    }

    最佳答案

    barinclude_in_parent字段设为true:

        "bar": {
    "type": "nested",
    "include_in_parent": true,
    "properties": {
    "bar_id": {
    "type": "long"
    },
    "details": {
    "properties": {
    "name": {
    "type": "string"
    },
    "value": {
    "type": "long"
    }
    }
    }
    }
    }

    并使用以下查询:
    {
    "query": {
    "query_string": {
    "query": "bar.details.name:(sandwich NOT pizza)"
    }
    }
    }

    关于elasticsearch - 在Elasticsearch中查询多个嵌套对象在所有嵌套对象中是否存在另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36753435/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com