elasticsearch - 类型的 Elasticsearch 限制结果

标签 elasticsearch

我有以下查询

$queryDefinition = [
        'query' => [
            'bool' => [
                'must' => [
                    [
                        'query_string' => [
                            'default_field' => '_all',
                            'query' => $term
                        ]
                    ]
                ],
                'must_not' => [],
                'should' => []
            ],
        ],
        //'filter' => [
        //    'limit' => ['value' => 3],
        //],
        'from' => 0,
        'size' => 50,
        'sort' => [],
        'facets' => [
            'types' => [
                    'terms' => ['field' => '_type']
                ]
            ]
    ];

在索引中我们有 5 种类型,我只想显示每种类型的 3 个结果,用于自动完成。当我设置过滤器限制时,只有第一种类型的结果会被过滤,对于其他类型,我会得到所有结果。

我该怎么做?

谢谢

最佳答案

{
  "aggregations": {
    "types": {
      "terms": {
        "field": "_type"
      },
      "hits": {
        "top_hits": {
          "size": 3
        }
      }
    }
  }
}

如果您使用的是 ElasticSearch 1.4.0,则可以使用 terms aggregation在“_type”字段上并使用 top hits aggregation作为子聚合并将其大小设置为 3(在您的情况下)。

希望对您有所帮助。

关于elasticsearch - 类型的 Elasticsearch 限制结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13818892/

相关文章:

couchbase - 删除执行XDCR时Couchbase添加的默认字段 “doc”

filter - 我没有从Elasticsearch查询中得到任何文档。有人可以指出我的错误吗?

c# - NEST 2.3.1 (Elastic Search) 创建索引出错

elasticsearch - 安装 ElasticSearch APM

使用 And 和 Or 条件的 Elasticsearch 查询

elasticsearch - 如何检测何时将新的唯一术语插入到 Elasticsearch 中特定索引中特定字段的索引中?

elasticsearch - Elasticsearch Nest:是否可以在自动映射的属性属性上定义标记生成器?

filter - Elasticsearch 同义词分析器不工作

node.js - NodeJS 使用 Elasticsearch/Mongoosastic 不返回任何结果

elasticsearch - ElasticSearch聚合+在非数值字段5.3上排序