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

标签 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数据?

最佳答案

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

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

这也将更有效率。

更新:

继续使用terms/top_hits的另一种方法是利用response filtering并仅返回您需要的内容。例如,将其附加到您的网址将确保您不会在聚合中找到任何内部匹配
?filter_path=hits.hits,aggregations.**.key,aggregations.**.doc_count,aggregations.**.hits.hits.hits._source

关于elasticsearch - 使用来源过滤器从热门匹配中排除内部匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57479970/

相关文章:

elasticsearch - Elasticsearch 集群

django - Elasticsearch 聚合[python]

elasticsearch - 如何对Elasticsearch建议的结果进行排序?

elasticsearch - Elasticsearch字段名称的最大长度

python - 在 Elastic Search 中为批量加载设置请求超时

python - haystack ElasticsearchDocumentStore 无法连接 Elasticsearch

elasticsearch - 使用 puppet 运行 elasticsearch 时出错

mysql - Nutch 抓取文档的 Elasticsearch 映射面临问题

django - 将 Django 应用程序部署到 Amazon AWS Elastic Beanstalk 时出现问题

mysql - elastic search + couchdb 或 sphinx + mysql .... 用于文档审查 SaaS