gpt4 book ai didi

elasticsearch - 筛选所有必须包含所有指定值的字典数组

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

说我有这个文件:

         {
"_index": "food",
"_type": "recipes",
"_id": "AU2LjsMLOuShTUj_LBrT",
"_score": 1,
"_source": {
"name": "granola bars",
"ingredients": [
{
"name": "butter",
"quantity": 4
},
{
"name": "granola",
"quantity": 6
}
]
}
}

使用以下过滤器可以很好地匹配此文档:
POST /food/recipes/_search
{
"query": {
"filtered": {
"query": {
"match_all": { }
},
"filter": {
"nested": {
"path": "ingredients",
"filter": {
"bool": {
"must": [
{
"terms": {
"ingredients.name": [
"butter",
"granola"
]
}
}
]
}
}
}
}
}
}
}

但是,它也会匹配具有其他成分的文档。
我如何查询以便仅匹配仅包含黄油和格兰诺拉麦片成分的文档?

最佳答案

可以这么说,您需要“双重否定”。您要匹配具有与查询匹配的嵌套文档的父文档,而没有与查询不匹配的嵌套文档。

为了测试,我设置了以下索引:

PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"properties": {
"ingredients": {
"type": "nested",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "long"
}
}
},
"name": {
"type": "string"
}
}
}
}
}

并添加了以下两个文件:
PUT /test_index/doc/1
{
"name": "granola bars",
"ingredients": [
{
"name": "butter",
"quantity": 4
},
{
"name": "granola",
"quantity": 6
}
]
}

PUT /test_index/doc/2
{
"name": "granola cookies",
"ingredients": [
{
"name": "butter",
"quantity": 5
},
{
"name": "granola",
"quantity": 7
},
{
"name": "milk",
"quantity": 2
},
{
"name": "sugar",
"quantity": 7
}
]
}

您的查询将返回两个文档。出于这个问题的目的,为了使其更易于理解,我首先对查询进行了一些简化:
POST /test_index/doc/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "ingredients",
"filter": {
"terms": {
"ingredients.name": [
"butter",
"granola"
]
}
}
}
}
}
}
}

然后,我添加了带有两个 "bool"过滤器的外部 "nested"。一个是您最初在 "must"中具有的过滤器,第二个是您所拥有的过滤器的对面(因此它将匹配不包含这些术语的嵌套文档),位于 "must_not"中:
POST /test_index/doc/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "ingredients",
"filter": {
"terms": {
"ingredients.name": [
"butter",
"granola"
]
}
}
}
}
],
"must_not": [
{
"nested": {
"path": "ingredients",
"filter": {
"not": {
"filter": {
"terms": {
"ingredients.name": [
"butter",
"granola"
]
}
}
}
}
}
}
]
}
}
}
}
}

这仅返回一个文档:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"name": "granola bars",
"ingredients": [
{
"name": "butter",
"quantity": 4
},
{
"name": "granola",
"quantity": 6
}
]
}
}
]
}
}

这是我用于测试的所有代码:

http://sense.qbox.io/gist/e5fd0c35070fb329d40ad342b3198695e6f52d3a

关于elasticsearch - 筛选所有必须包含所有指定值的字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30441557/

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