[英]Elasticsearch: Filter array elements in nested documents
Currently I'm trying to understand the structure of Elasticsearch documents and those responses.目前我正在尝试了解 Elasticsearch 文档的结构和这些响应。 I created some indices and added a few documents to learn the basics.我创建了一些索引并添加了一些文档来学习基础知识。 Now I am struggling with the responses of nested dicts.现在我正在为嵌套字典的响应而苦苦挣扎。
Recently I added 2 documents to my existing index:最近我在现有索引中添加了 2 个文档:
{
{"id": 1,
"vehicle": "car",
"equipment": [
{"v_id": 24, "gps": true,"color": "black"}
{"v_id": 11, "gps": false, "color": "yellow"}
]
},
{"id": 2,
"vehicle": "bus",
"equipment": [
{"v_id": 34, "gps": false,"color": "red"}
{"v_id": 99, "gps": false,"color": "yellow"}
{"v_id": 10, "gps": true,"color": "red"}
]
}
}
Now I want to retrieve all yellow vehicles using the query:现在我想使用查询检索所有黄色车辆:
{
"query": {
"match": {
"equipment.color": "yellow“
}
}
}
I will get a response that looks like:我会得到如下回复:
{
{"id": 1,
"vehicle": "car",
"equipment": [
{"v_id": 24, "gps": true,"color": "black"}
{"v_id": 11, "gps": false, "color": "yellow"}
]
},
{"id": 2,
"vehicle": "bus",
"equipment": [
{"v_id": 34, "gps": false,"color": "red"}
{"v_id": 99, "gps": false,"color": "yellow"}
{"v_id": 10, "gps": true,"color": "red"}
]
}
}
because both documents contain a vehicle that has "yellow" as color value.因为这两个文档都包含一个颜色值为“黄色”的车辆。 Actually I try to filter some array-elements out from the response and want that the response looks like this:实际上,我尝试从响应中过滤出一些数组元素,并希望响应看起来像这样:
{
{"id": 1,
"vehicle": "car",
"equipment": [
{"v_id": 11, "gps": false, "color": "yellow"}
]
},
{"id": 2,
"vehicle": "bus",
"equipment": [
{"v_id": 99, "gps": false,"color": "yellow"}
]
}
}
Is that possible with Elasticsearch or generally with dictionaries? Elasticsearch 或一般字典可以做到这一点吗? If the answer is yes, how should the query-body look like?如果答案是肯定的,那么查询主体应该是什么样子?
Thanks a lot, cico非常感谢,西科
You need to use nested query along with inner_hits您需要将嵌套查询与inner_hits一起使用
Adding a working example with index mapping, search query, and search result添加具有索引映射、搜索查询和搜索结果的工作示例
Index Mapping:索引映射:
PUT myidx
{
"mappings": {
"properties": {
"equipment":{
"type":"nested"
}
}
}
}
Search Query:搜索查询:
POST myidx/_search
{
"query": {
"nested": {
"path": "equipment",
"query": {
"match": {
"equipment.color": "yellow"
}
},
"inner_hits": {}
}
}
}
Search Result:搜索结果:
{
"took" : 432,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "myidx",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.87546873,
"_source" : {
"id" : 1,
"vehicle" : "car",
"equipment" : [
{
"v_id" : 24,
"gps" : true,
"color" : "black"
},
{
"v_id" : 11,
"gps" : false,
"color" : "yellow"
}
]
},
"inner_hits" : {
"equipment" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "myidx",
"_type" : "_doc",
"_id" : "1",
"_nested" : {
"field" : "equipment",
"offset" : 1
},
"_score" : 0.87546873,
"_source" : {
"v_id" : 11,
"color" : "yellow",
"gps" : false // note this
}
}
]
}
}
}
},
{
"_index" : "myidx",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.87546873,
"_source" : {
"id" : 2,
"vehicle" : "bus",
"equipment" : [
{
"v_id" : 34,
"gps" : false,
"color" : "red"
},
{
"v_id" : 99,
"gps" : false,
"color" : "yellow"
},
{
"v_id" : 10,
"gps" : true,
"color" : "red"
}
]
},
"inner_hits" : {
"equipment" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "myidx",
"_type" : "_doc",
"_id" : "2",
"_nested" : {
"field" : "equipment",
"offset" : 1
},
"_score" : 0.87546873,
"_source" : {
"v_id" : 99,
"color" : "yellow", // note this
"gps" : false
}
}
]
}
}
}
}
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.