Elasticsearch中的多重或过滤器 [英] Multiple OR filter in Elasticsearch

查看:76
本文介绍了Elasticsearch中的多重或过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我无法确定Elasticsearch中多个OR的以下查询的正确性.我想选择所有唯一数据(不是计数,而是选择所有行)

Hello I'm having trouble deciding the correctness of the following query for multiple OR in Elasticsearch. I want to select all the unique data (not count, but select all rows)

对此我在弹性查询中的最佳尝试是

My best try for this in elastic query is

GET mystash/_search
{
    "aggs": {
        "uniques":{
             "filter":
            {
                "or":
                        [
                            { "term": { "url.raw" : "/a.json" } },
                            { "term": { "url.raw" : "/b.json" } },
                            { "term": { "url.raw" : "/c.json"} },
                            { "term": { "url.raw" : "/d.json"} }
                        ]
            },
            "aggs": {
                "unique" :{
                    "terms" :{
                        "field" : "id.raw",
                        "size" : 0
                    }
                }
            }
        }
    }
}

等效的SQL将是

SELECT DISTINCT id
FROM json_record
WHERE 
    json_record.url = 'a.json' OR
    json_record.url = 'b.json' OR
    json_record.url = 'c.json' OR
    json_record.url = 'd.json'

我想知道上面的查询是否正确,因为生成报告将需要数据.

I was wondering whether the query above is correct, since the data will be needed for report generations.

推荐答案

一些说明:

  • 您应该使用查询过滤器而不是聚合过滤器.您的查询会加载所有文档.
  • 您可以用单个字词过滤器替换您的or +字词过滤器
  • 您可以在查询的根部使用size = 0来仅获取agg结果而不是搜索结果

示例代码:

{"size":0,
  "query" :{"filtered":{"filter":{"terms":{"url":["a", "b", "c"]}}}}, 
   "aggs" :{"unique":{"term":{"field":"id", "size" :0}}}
} 

这篇关于Elasticsearch中的多重或过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆