基于索引名称的有条件聚合

发布于 2025-01-30 07:19:12 字数 1570 浏览 4 评论 0原文

我需要在现有项目中添加功能。 到目前为止

return this.client.search({
    index,
    size: pageSize,
    from: currentPage <= 1 ? 0 : (currentPage - 1) * pageSize,
    body: {
        query: {
            query_string: queryObject
        }
    }
});

​代码> index ='机会>',或者我们同时用所有这些,index = ['quotes','机会',...] 现在,我被要求添加此搜索以下内容:

  1. 如果仅在索引QUOTES中完成搜索,则将逻辑添加到仅搜索具有最高版本的引号字段>字段在所有引号中的值相同master_id字段值
  2. quotes index index

如果我们在多个索引中搜索,则仅通过master_id应用此分组,而通过max版本进行过滤到quot > 到目前为止,我已经添加了汇总,其中包含引号的结果,由master_id分组,只有最高版本的汇总添加到集合内部的结果:

return this.client.search({
    index,
    size: pageSize,
    from: currentPage <= 1 ? 0 : (currentPage - 1) * pageSize,
    body: {
        aggs: {
            groupByMasterId: {
                terms: {
                    field: 'master_id'
                }
            },
            aggs: {
                top_hits: {
                    size: 1,
                    sort: [
                        {
                            version: 'desc'
                        }
                    ]
                }
            }
        },
        query: {
            query_string: queryObject
        }
    }
});

我需要帮助:

  1. 是否有一个返回hits hits对象而不是聚合对象的聚合结果的方法?还是我还必须添加单独的逻辑才能提取这些QUOTES 聚合 Elasticsearch响应的对象结果?
  2. 如果在单个查询中搜索了多个索引,我可以将此聚合仅应用于特定索引吗?还是在所有索引的所有结果上完成聚合?

谢谢

I need to add functionality to an existing project.
Until now our fulltext serach only sent index list and query string to elasticsearch(from our js backend):

return this.client.search({
    index,
    size: pageSize,
    from: currentPage <= 1 ? 0 : (currentPage - 1) * pageSize,
    body: {
        query: {
            query_string: queryObject
        }
    }
});

Until now and still currently we either search by on of the specific indexes, index = 'quotes' or index = 'opportunities', or we serach by all of them at once, index = ['quotes', 'opportunities', ...]
Now, I was asked to add to this search the following:

  1. If the search is done only in index quotes, then add logic to only search for quotes which have highest version field value inside a group of all quotes with same master_id field value
  2. If we search in multiple indexes, then only apply this grouping by master_id and filtering by max version to the quotes index

What I have so far is I've added aggregation which contains results of quotes, grouped by master_id and only the one with highest version is added to the result inside aggregation:

return this.client.search({
    index,
    size: pageSize,
    from: currentPage <= 1 ? 0 : (currentPage - 1) * pageSize,
    body: {
        aggs: {
            groupByMasterId: {
                terms: {
                    field: 'master_id'
                }
            },
            aggs: {
                top_hits: {
                    size: 1,
                    sort: [
                        {
                            version: 'desc'
                        }
                    ]
                }
            }
        },
        query: {
            query_string: queryObject
        }
    }
});

I need help with this:

  1. is there a way to return the result of aggregation inside hits object instead of aggregations object ? Or do I also have to add separate logic to extract these quotes results from aggregations object of Elasticsearch response ?
  2. If the multiple indexes are being searched in single query, can I apply this aggregation only to a specific index ? Or are aggregations done on all results from all the indexes ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文