如何仅根据特定条件过滤内部对象。例如,我将产品存储在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"}]}}]}}
答案 0 :(得分:0)
如果要过滤“描述”,则需要将映射从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": {}
}
}
]
}
}
}