【问题标题】:Elastic Search Alternative of SQL Query?SQL 查询的弹性搜索替代方案?
【发布时间】:2014-07-12 10:16:43
【问题描述】:

我怎样才能写一个Elastic Search Alternative下面的SQL查询,

select
    column1,
    column2,
    column3,
    count(column3)
from
    table_name
where
    some conditions
group by
    column1,
    column2,
    column3
order by
    column 3 desc;

我尝试了多种方式,包括方面、总和聚合等。无法组合查询。 我认为使用脚本也可以做到这一点,但我不知道该怎么做?

  1. 能否请您指导我如何编写上述 SQL 替代方案?
  2. 是否可以通过脚本实现,如果可以,如何实现?

提前致谢。

【问题讨论】:

    标签: sql elasticsearch


    【解决方案1】:

    我终于找到了一种方法,但只能逐列一组。这是查询

    {
      "index": "index_value",
      "type": "type_value",
      "searchQuery": {
        "query": {
          "match_all": {}
        },
        "filter": {
          "range": {
            "activity_date": {
              "from": "2013-11-01",
              "to": "2014-11-02"
            }
          }
        },
        "facets": {
          "sample_facet": {
            "terms_stats": {
              "key_field": "offering_sku",
              "value_field": "net_ordered_units",
              "value_field": "gross_ordered_units",
              "order": "max"
            }
          }
        }
      }
    }
    

    在过滤器中指定 where 条件,并且 facet 由 elasticsearch 替代分组,其中key_field 指的是列名,value_field 指的是执行聚合的字段,如 sum、max 等。最大值的顺序是指按照最大值降序排列。

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多