ElasticSQL package converts SQL to Elasticsearch DSL

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

ElasticSQL
-----------

 ElasticSQL package converts SQL to Elasticsearch DSL

 SQL Features Support:

- [x] SQL Select
- [x] SQL Where
- [x] SQL Order BySQL
- [x] SQL Group By
- [x] SQL AND & OR
- [x] SQL Like & NOT Like
- [x] SQL COUNT distinct
- [x] SQL In & Not In
- [x] SQL Between
- [x] SQL avg()、count(*), count(field), min(field), max(field)

Beyond SQL Features Support:
- [x] ES TopHits
- [x] ES date_histogram
- [x] ES STATS
- [x] ES RANGE
- [x] ES DATE_RANGE

Improvement : now the query DSL is much more flat


SQL Usage


Query

select * from test where a=1 and b="c" and create_time between '2015-01-01T00:00:00+0800' and '2016-01-01T00:00:00+0800' and process_id > 1 order by id desc limit 100,10


Aggregation

select avg(age),min(age),max(age),count(student),count(distinct student) from test group by grade,class limit 10


Beyond SQL

  •   range age group 20-25,25-30,30-35,35-40
SELECT COUNT(age) FROM bank GROUP BY range(age, 20,25,30,35,40)

 

  •   range date group by your config
SELECT online FROM online GROUP BY date_range(field="insert_time",format="yyyy-MM-dd" ,"2014-08-18","2014-08-17","now-8d","now-7d","now-6d","now")

 

  •   range date group by day
select * from test group by date_histogram(field="changeTime",interval="1h",format="yyyy-MM-dd HH:mm:ss")

 

  •   stats
SELECT online FROM online group by stats(field="grade")

 

  •   topHits
select top_hits(field="class", hitssort="age:desc", taglimit = "10", hitslimit = "1", _source="name,age,class,gender") from school


PKG Usage
-------------

 go get github.com/farmerx/elasticsql

Demo :

package mainimport (    "fmt"    "github.com/farmerx/elasticsql")var sql = `select * from test where a=1 and b="c" and create_time between '2015-01-01T00:00:00+0800' and '2016-01-01T00:00:00+0800' and process_id > 1 order by id desc limit 100,10`var sql2= `  select avg(age),min(age),max(age),count(student) from test group by class limit 10`var sql3= `  select * from test group by class,student limit 10`var sql4 = `  select * from test group by date_histogram(field="changeTime",interval="1h",format="yyyy-MM-dd HH:mm:ss")`func main() {    esql := elasticsql.NewElasticSQL(eslasticsql.InitOptions{})    table, dsl, err := esql.SQLConvert(sql)    fmt.Println(table, dsl, err)}

 

項目地址

elasticSQL github 地址

 

416 次點擊  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.