Elasticsearch中的过滤/查询支持热门点击聚合

时间:2015-12-16 18:20:09

标签: elasticsearch

Elasticsearch文档指出The top_hits aggregation returns regular search hits, because of this many per hit features can be supported最重要的是,该列表包含Named filters and queries

但尝试添加任何过滤器或查询会引发SearchParseException: Unknown key for a START_OBJECT

用例:我有包含嵌套注释列表的项目

项{id} - >评论{date,rating}

我希望在上周获得每个项目的评分最高。

{
 "query": {
   "match_all": {}
  },
  "aggs": {
    "items": {
      "terms": {
        "field": "id",
        "size": 10
      },
      "aggs": {
        "comment": {
          "nested": {
            "path": "comments"
          },
          "aggs": {
            "top_comment": {
              "top_hits": {
                "size": 1,
                //need filter  here to select only comments of last week
                "sort": {
                  "comments.rating": {
                    "order": "desc"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

文档错误,或者有没有办法添加过滤器?

https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-aggregations-metrics-top-hits-aggregation.html

1 个答案:

答案 0 :(得分:-1)

您确定已将它们映射为Nested吗?我只是尝试对我的数据执行此类查询,但它确实运行良好。

如果是这样,你可以简单地在嵌套聚合之后添加一个过滤器聚合(希望我没有弄乱大括号):

POST data/_search
{
  "size": 0,
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "path": "comments",
          "query": {
            "range": {
              "comments.date": {
                "gte": "now-1w",
                "lte": "now"
              }
            }
          }
        }
      }
    }
  },
  "aggs": {
    "items": {
      "terms": {
        "field": "id",
        "size": 10
      },
      "aggs": {
        "nested": {
          "nested": {
            "path": "comments"
          },
          "aggs": {
            "filterComments": {
              "filter": {
                "range": {
                  "comments.date": {
                    "gte": "now-1w",
                    "lte": "now"
                  }
                }
              },
              "aggs": {
                "topComments": {
                  "top_hits": {
                    "size": 1,
                    "sort": {
                      "comments.rating": "desc"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

P.S。始终包含嵌套对象的FULL路径。

所以这个查询将:

  1. 过滤评论小于一周的文档,以缩小文档以进行汇总,并查找实际拥有此类评论的文档(已过滤的查询)
  2. 根据id字段进行术语聚合
  3. 打开嵌套的子文档(评论)
  4. 按日期过滤
  5. 返回最糟糕的一个(评分最高)