elasticsearch 弹性查询和聚合

kcrjzv8t  于 2023-05-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(133)

我是Elastic Search的新手。我想从查询步骤的_source中获取输出,以包含在aggs步骤中。
示例数据:

{
                    "id": "1",
                    "description": "nyHlUN4vLVT6cOjcM1xf",
                    "locationData": [
                        {
                            "locationId": "001-00001",
                            "delivery": true
                        },
                        {
                            "locationId": "001-00002",
                            "delivery": false
                        }
]}

现在我得到基于id & locationId的输出。例如,对于id ='1' & locationId ='001 -00001',输出应为

{
                    "id": "1",
                    "description": "nyHlUN4vLVT6cOjcM1xf",
                    "locationData": [
                        {
                            "locationId": "001-00001",
                            "delivery": true
                        }
]}

它不能包含locationId等于001-00002的数据。
我尝试在这里使用聚合,但在这种方法中出现了另一个问题。我得到的locationData只有locationId等于001-00001,但是iddescription没有包含在locationData中。
下面是我使用的查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "id": {
                            "value": "1"
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "filtered_locations": {
            "nested": {
                "path": "locationData"
            },
            "aggs": {
                "locations": {
                    "filter": {
                        "term": {
                            "locationData.locationId": "001-00001"
                        }
                    },
                    "aggs": {
                        "location_data": {
                            "top_hits": {
                                "_source": [
                                    "id",
                                    "description",
                                    "locationData"
                                ],
                                "size": 1
                            }
                        }
                    }
                }
            }
        }
    }
}

有人能在这方面提供帮助或为这种情况提供文档吗?

9q78igpj

9q78igpj1#

你真的需要聚合吗?您可以通过inner_hits恢复。
查询将是这样的:

GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": {
              "value": "1"
            }
          }
        },
        {
          "nested": {
            "path": "locationData",
            "inner_hits": {}, 
            "query": {
              "term": {
                "locationData.locationId.keyword": "001-00001"
              }
            }
          }
        }
      ]
    }
  }
}

查看inner_hits:

"hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.98082924,
    "hits": [
      {
        "_index": "index",
        "_id": "EIA4JIgBpLXl6DpqrRuF",
        "_score": 0.98082924,
        "_source": {
          "id": "1",
          "description": "nyHlUN4vLVT6cOjcM1xf",
          "locationData": [
            {
              "locationId": "001-00001",
              "delivery": true
            },
            {
              "locationId": "001-00002",
              "delivery": false
            }
          ]
        },
        "inner_hits": { <<--- your hit
          "locationData": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.6931471,
              "hits": [
                {
                  "_index": "index",
                  "_id": "EIA4JIgBpLXl6DpqrRuF",
                  "_nested": {
                    "field": "locationData",
                    "offset": 0
                  },
                  "_score": 0.6931471,
                  "_source": {
                    "delivery": true,
                    "locationId": "001-00001"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

相关问题