【问题标题】:Elasticsearch query and aggregation on indices索引上的 Elasticsearch 查询和聚合
【发布时间】:2019-03-06 09:16:28
【问题描述】:

我有多个弹性搜索索引,我想获得doc_count 有多少文档与每个索引的查询匹配(即使没有文档与索引匹配)。我试过这个(使用 elasticsearch.js):

{
  index: 'one,or,many,indices,here',
  query: {
    query_string: {
      query: 'hello',
    },
  },
  aggs: {
    group_by_index: {
      terms: {
        field: '_index',
        min_doc_count: 0,
      },
    },
  },
  from: 0,
  size: 20,
};

这仅在我指定 index 键上的所有索引时才有效。但是,我并不总是希望在我的搜索结果中匹配所有索引中的文档。

所以我想出了:

{
  index: 'all,indices,here',
  query: {
    bool: {
      must: [
        {
          query_string: {
            query: 'hello',
          },
        },
        {
          terms: {
            _index: 'only,search,hits,indices,here',
          },
        },
      ],
    },
  },
  aggs: {
    group_by_index: {
      terms: {
        field: '_index',
        min_doc_count: 0,
      },
    },
  },
  from: 0,
  size: 20,
};

然后我得到doc_count: 0 用于匹配的索引,因为该索引不在 bool 查询中。

如何获取所有索引的 doc_count,但不能从不需要的索引中获取搜索命中?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    您需要将索引约束移至post_filter

    {
      index: 'all,indices,here',
      query: {
        bool: {
          must: [
            {
              query_string: {
                query: 'hello',
              },
            },
          ],
        },
      },
      aggs: {
        group_by_index: {
          terms: {
            field: '_index',
            min_doc_count: 0,
          },
        },
      },
      post_filter: {
        terms: {
          _index: 'only,search,hits,indices,here',
        },
      },
      from: 0,
      size: 20,
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 2019-12-06
      • 2022-01-18
      • 2014-10-22
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多