繁体   English   中英

Elasticsearch:过滤嵌套文档中的数组元素

[英]Elasticsearch: Filter array elements in nested documents

目前我正在尝试了解 Elasticsearch 文档的结构和这些响应。 我创建了一些索引并添加了一些文档来学习基础知识。 现在我正在为嵌套字典的响应而苦苦挣扎。

最近我在现有索引中添加了 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"}
   ]
  }
}

现在我想使用查询检索所有黄色车辆:

{
   "query": {
     "match": {
         "equipment.color": "yellow“
      }
   }
}

我会得到如下回复:

{
   {"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"}
   ]
  }
}

因为这两个文档都包含一个颜色值为“黄色”的车辆。 实际上,我尝试从响应中过滤出一些数组元素,并希望响应看起来像这样:

{
   {"id": 1,
    "vehicle": "car",
    "equipment": [
       {"v_id": 11, "gps": false, "color": "yellow"}
    ]
   },

  {"id": 2,
   "vehicle": "bus",
   "equipment": [
       {"v_id": 99, "gps": false,"color": "yellow"}
   ]
  }
}

Elasticsearch 或一般字典可以做到这一点吗? 如果答案是肯定的,那么查询主体应该是什么样子?

非常感谢,西科

您需要将嵌套查询inner_hits一起使用

添加具有索引映射、搜索查询和搜索结果的工作示例

索引映射:

PUT myidx
{
  "mappings": {
    "properties": {
      "equipment":{
        "type":"nested"
      }
    }
  }
}

搜索查询:

POST myidx/_search
{
  "query": {
    "nested": {
      "path": "equipment",
      "query": {
        "match": {
          "equipment.color": "yellow"
        }
      },
      "inner_hits": {}
    }
  }
}

搜索结果:

{
  "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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM