gpt4 book ai didi

elasticsearch - 在Elasticsearch中,如何基于不影响实际查询命中的条件过滤特定内部对象

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

如何根据特定条件仅过滤内部对象。例如,我将产品存储在es中。基于某些字段,我想查询产品,例如型号和其他条件。但是我只想通过传递的语言值列出查询结果的名称和描述。

架构如下

PUT /product/
{"mappings":{"productTypeA":{"dynamic":"strict","properties":{"id":{"type":"keyword"},"model":{"type":"keyword"},"version":{"type":"keyword"},"launchDate":{"type":"date","format":"yyyyMMdd'T'HHmmss.SSSZ","fields":{"keyword":{"type":"keyword"}}},"names":{"type":"object","properties":{"locale":{"type":"keyword"},"value":{"type":"keyword"}}},"descriptions":{"type":"object","properties":{"locale":{"type":"keyword"},"value":{"type":"keyword"}}}}}}}

我的产品1
POST /product/productTypeA/1
{"id":"1","model":"modelA","version":"1.0","launchDate":"20190924T175212.057+0000","names":[{"locale":"en","value":"product one"},{"locale":"fr","value":"produit un"},{"locale":"de","value":"Produkt eins"}],"descriptions":[{"locale":"en","value":"product one description"},{"locale":"fr","value":"description du produit un"},{"locale":"de","value":"Produkt eins Beschreibung"}]}

我的产品2
POST /product/productTypeA/2
{"id":"2","model":"modelA","version":"1.0","launchDate":"20190924T175212.057+0000","names":[{"locale":"en","value":"product two"},{"locale":"fr","value":"produit deux"},{"locale":"de","value":"Produkt zwei"}],"descriptions":[{"locale":"en","value":"product two description"},{"locale":"fr","value":"description du produit deux"},{"locale":"de","value":"Produkt zwei Beschreibung"}]}

我在以下提到的产品数据上按如下方式尝试了查询的post_filter
GET /product/productTypeA/_search
{"query":{"bool":{"must":[{"term":{"model":"modelA"}}]}},"post_filter":{"bool":{"should":{"term":{"names.locale":"fr"}}}}}

但是我的结果在所有语言中都有名称和描述,正如我期望的那样,每个查询结果仅以法语显示
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":0.2876821,"hits":[{"_index":"product","_type":"productTypeA","_id":"2","_score":0.2876821,"_source":{"id":"2","model":"modelA","version":"1.0","launchDate":"20190924T175212.057+0000","names":[{"locale":"en","value":"product two"},{"locale":"fr","value":"produit deux"},{"locale":"de","value":"Produkt zwei"}],"descriptions":[{"locale":"en","value":"product two description"},{"locale":"fr","value":"description du produit deux"},{"locale":"de","value":"Produkt zwei Beschreibung"}]}},{"_index":"product","_type":"productTypeA","_id":"1","_score":0.2876821,"_source":{"id":"1","model":"modelA","version":"1.0","launchDate":"20190924T175212.057+0000","names":[{"locale":"en","value":"product one"},{"locale":"fr","value":"produit un"},{"locale":"de","value":"Produkt eins"}],"descriptions":[{"locale":"en","value":"product one description"},{"locale":"fr","value":"description du produit un"},{"locale":"de","value":"Produkt eins Beschreibung"}]}}]}}

最佳答案

如果要过滤“描述”,则需要将映射从object更改为nested type
对象类型存储为平面字段,因此无法对其进行过滤
例如当description是类型时,其下的对象字段将存储为description.locale等。

下面的查询将返回所有具有法语描述的modelA,并且法语描述将出现在inner_hits

GET /product/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"model": "modelA"
}
},
{
"nested": {
"path": "descriptions",
"query": {
"match": {
"descriptions.locale": "fr"
}
},
"inner_hits": {}
}
}
]
}
}
}

编辑1:
GET /product/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"model": "modelA"
}
}
],
"should": [
{
"nested": {
"path": "descriptions",
"query": {
"match": {
"descriptions.locale": "fr"
}
},
"inner_hits": {}
}
}
]
}
}
}

关于elasticsearch - 在Elasticsearch中,如何基于不影响实际查询命中的条件过滤特定内部对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350701/

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