elasticsearch-sql icon indicating copy to clipboard operation
elasticsearch-sql copied to clipboard

这样的子查询 es语句能转吗?

Open dingzhjie opened this issue 4 years ago • 6 comments

select count(DISTINCT deviceId) from uv where deviceId in (select deviceId from uv where userId = 'unknown') and uv.userId != 'unknown';

dingzhjie avatar Jan 04 '21 15:01 dingzhjie

可以

select count(DISTINCT deviceId) from uv where deviceId in (select deviceId from uv where userId = 'unknown') and uv.userId <> 'unknown'

shi-yuan avatar Jan 05 '21 13:01 shi-yuan

能帮忙转一下吗 我们服务依赖阿里云的 只能装阿里云的插件 他们的插件我试了一下不能转

dingzhjie avatar Jan 08 '21 09:01 dingzhjie

这个转换,不是直接转出来的,里面有个子查询,转换的时候,需要执行子查询,拿到结果,然后再转出最终的结果

shi-yuan avatar Jan 11 '21 09:01 shi-yuan

本地子查询 select deviceId from uv where userId = 'unknown' 只有一条数据:device1

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "must": [
                    {
                      "bool": {
                        "should": [
                          {
                            "match_phrase": {
                              "deviceId": {
                                "query": "device1",
                                "slop": 0,
                                "zero_terms_query": "NONE",
                                "boost": 1
                              }
                            }
                          }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "match_phrase": {
                              "uv.userId": {
                                "query": "unknown",
                                "slop": 0,
                                "zero_terms_query": "NONE",
                                "boost": 1
                              }
                            }
                          }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "_source": {
    "includes": [
      "COUNT"
    ],
    "excludes": []
  },
  "aggregations": {
    "COUNT(DISTINCT deviceId)": {
      "cardinality": {
        "field": "deviceId"
      }
    }
  }
}

shi-yuan avatar Jan 11 '21 13:01 shi-yuan

sorry 过年了 一直没关注 这个如果分成了两步的话 子查询的集合会非常大 外面的in也会非常大 有啥办法能不取出来 一句话查出来结果吗

dingzhjie avatar Feb 18 '21 02:02 dingzhjie

我的想法是,要么就现有模型拿到所有的deviceid之后批量去取,要么考虑调整下mapping来满足这种query

shi-yuan avatar Feb 18 '21 03:02 shi-yuan