gpt4 book ai didi

elasticsearch - 我认为elasticsearch嵌套查询

转载 作者:行者123 更新时间:2023-12-03 02:01:20 25 4
gpt4 key购买 nike

我来自相关的数据库背景,在那里这样的事情很简单,但是我无法弄清楚。我已经尝试学习Elasticsearch了大约一个星期,并且试图找出我认为是嵌套查询的内容。以下是一些示例数据:

PUT /myindex/pets/_mapping
{
"pets": {
"properties": {
"name": {
"type": "string"
},
"pet": {
"type": "nested",
"properties": {
"name": {"type": "string"}
}
}
}
}
}

POST /myindex/pets/
{"pet": {"name": "rosco"}, "name": "sam smith"}

POST /myindex/pets/
{"pet": {"name": "patches"}, "name": "sam smith"}

POST /myindex/pets
{"pet": {"name": "rosco"}, "name": "jack mitchell"}

查询看起来像什么,只返回匹配的文档:
  • 所有者名称是“sam smith”
  • 宠物名是“rosco”

  • 我已经尝试过 boolmatchnestedfiltered/filter类型查询的混合匹配,但是我一直在出错。这样的东西在错误中脱颖而出:
    nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"nested\"];

    这是查询:
    GET /myindex/pets/_search
    {
    "query": {
    "match": {
    "name": "sam smith"
    },
    "nested": {
    "path": "pet",
    "query": {
    "match": {
    "pet.name": "rosco"
    }
    }
    }
    }
    }

    我开始认为,由于Elasticsearch的相关性质,我无法针对这一特定目标。

    有任何想法吗?

    最佳答案

    伙计,这些查询有时很棘手...这似乎起作用:

    GET /myindex/pets/_search
    {
    "query": {
    "filtered": {
    "query": {
    "match": {
    "name": "sam smith"
    }
    },
    "filter": {
    "nested": {
    "path": "pet",
    "query": {
    "match": {
    "pet.name": "rosco"
    }
    }
    }
    }
    }
    }
    }

    关于elasticsearch - 我认为elasticsearch嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182927/

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