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

时间:2016-03-23 18:42:04

标签: elasticsearch

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


    {
        "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 个答案:

答案 0 :(得分: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
          }
       }
     }
  }

parent_document获取_source数据的数据和从inner_hits获取实际数据。

希望有所帮助