使用来源过滤器从热门匹配中排除内部匹配

时间:2019-08-13 14:32:50

标签: elasticsearch

在查询中,我使用inner_hits返回与查询匹配的嵌套对象列表。

然后我为我的文档的categoryId添加一个聚合,然后是一个热门匹配,以获取该类别的显示名称。

"aggs": {
  "category": {
    "terms": {
      "field": "categoryId",
      "size": 100
    },
    "aggs": {
      "category_value": {
        "top_hits": {
          "size": 1,
          "_source": {
            "includes": "categoryName"
          }
        }
      }
    }
  }
}

现在,当我查看聚合存储桶时,我确实获得了一个仅具有_source属性的categoryName文档,但是我还获得了整个inner_hits集合:

{
    ...
    "_source": {
        "categoryName": "Armchairs"
    },
    "inner_hits": {
        "my_inner_hits": {
            "hits": {
                "total": 260,
                "max_score": null,
                "hits": [{
                        ...
                        "_source": {
                            //nested document here
                        }
                    }
                ]
            }
        }
    }
}

是否有一种方法不将inner_hits数据包括在top_hits聚合中?

1 个答案:

答案 0 :(得分:1)

由于只需要一个字段,所以我建议您摆脱top_hits聚合,并使用另一个terms聚合作为名称:

{
  ...
  "aggs": {
    "category": {
      "terms": {
        "field": "categoryId",
        "size": 100
      },
      "aggs": {
        "category_value": {
          "terms": {
            "field": "categoryName",
            "size": 1
          }
        }
      }
    }
  }
}

这也将更有效率。

更新

继续使用terms/top_hits的另一种方法是利用response filtering并只返回您需要的内容。例如,将其附加到您的URL可以确保您不会在聚合中找到任何内部匹配

?filter_path=hits.hits,aggregations.**.key,aggregations.**.doc_count,aggregations.**.hits.hits.hits._source
相关问题