首页 文章

如何在嵌套的top_hits聚合中获取父文档?

提问于
浏览
2

这是我的文档/映射与嵌套价格数组:

{
        "name": "Foobar",
        "type": 1,
        "prices": [
            {
                "date": "2016-03-22",
                "price": 100.41
            },
            {
                "date": "2016-03-23",
                "price": 200.41
            }
        ]
    }

Mapping:

    {
        "properties": {
          "name": {
            "index": "not_analyzed",
            "type": "string"
          },
          "type": {
            "type": "byte"
          },
          "prices": {
            "type": "nested",
            "properties": {
              "date": {
                "format": "dateOptionalTime",
                "type": "date"
              },
              "price": {
                "type": "double"
              }
            }
          }
        }
    }

我使用top_hits聚合来获取嵌套价格数组的最低价格 . 我还必须按日期过滤价格 . 这是查询和响应:

POST /index/type/_search

    {
      "size": 0,
      "query": {
        "match_all": {}
      },
      "aggs": {
        "prices": {
          "nested": {
            "path": "prices"
          },
          "aggs": {
            "date_filter": {
              "filter": {
                "range": {
                  "prices.date": {
                    "gte": "2016-03-21"
                  }
                }
              },
              "aggs": {
                "min": {
                  "top_hits": {
                    "sort": {
                      "prices.price": {
                        "order": "asc"
                      }
                    },
                    "size": 1
                  }
                }
              }
            }
          }
        }
      }
    }

Response:

    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 0,
        "hits": [
        ]
      },
      "aggregations": {
        "prices": {
          "doc_count": 4,
          "date_filter": {
            "doc_count": 4,
            "min": {
              "hits": {
                "total": 4,
                "max_score": null,
                "hits": [
                  {
                    "_index": "index",
                    "_type": "type",
                    "_id": "4225796ALL2016061541031",
                    "_nested": {
                      "field": "prices",
                      "offset": 0
                    },
                    "_score": null,
                    "_source": {
                      "date": "2016-03-22",
                      "price": 100.41
                    },
                    "sort": [
                      100.41
                    ]
                  }
                ]
              }
            }
          }
        }
      }
    }

有没有办法在响应中使用 _id="4225796ALL2016061541031" 获取父源文档(或其中的某些字段)(例如 name )?第二个查询不是一个选项 .

1 回答

  • 3

    而不是应用 aggregations 使用 queryinner_hits 如:

    {
    "query": {
        "nested": {
           "path": "prices",
           "query": {
              "range": {
                  "prices.date": {
                    "gte": "2016-03-21"
                  }
                }
           },
            "inner_hits": {
               "sort": {
                      "prices.price": {
                        "order": "asc"
                      }
                    },
                    "size": 1
              }
           }
         }
      }
    

    _source 获取 parent_document 数据的数据,从 inner_hits 获取实际数据 .

    希望能帮助到你

相关问题