elasticsql
1.0.1
_____ _ _ ____ _____ ___ ____ ____ ___ _
| ____| | / / ___|_ _|_ _|/ ___|/ ___| / _ | |
| _| | | / _ ___ | | | || | ___ | | | || |
| |___| |___ / ___ ___) || | | || |___ ___) | |_| || |___
|_____|_____|/_/ _|____/ |_| |___|____||____/ ___|_____|
該工具將sql轉換為elasticsearch dsl
目前支援:
去獲取-u github.com/cch123/elasticsql
演示:
package main
import (
"fmt"
"github.com/cch123/elasticsql"
)
var sql = `
select * from aaa
where a=1 and x = '三个男人'
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
`
func main () {
dsl , esType , _ := elasticsql . Convert ( sql )
fmt . Println ( dsl )
fmt . Println ( esType )
}
將產生:
{
"query" : {
"bool" : {
"must" : [
{
"match" : {
"a" : {
"query" : " 1 " ,
"type" : " phrase "
}
}
},
{
"match" : {
"x" : {
"query" : "三个男人" ,
"type" : " phrase "
}
}
},
{
"range" : {
"create_time" : {
"from" : " 2015-01-01T00:00:00+0800 " ,
"to" : " 2016-01-01T00:00:00+0800 "
}
}
},
{
"range" : {
"process_id" : {
"gt" : " 1 "
}
}
}
]
}
},
"from" : 100 ,
"size" : 10 ,
"sort" : [
{
"id" : " desc "
}
]
}
aaa
如果你的sql包含一些關鍵字,例如。順序、時間戳,不要忘記轉義這些字段,如下所示:
select * from `order` where `timestamp` = 1 and `desc`.id > 0
要使用這個工具,你需要了解elasticsearch的術語查詢和符合短語查詢。
將欄位設為已分析或未分析將得到不同的結果。
更多轉換細節請參考wiki
在編寫這個工具時,我試圖避免已棄用的 dsl 過濾器和聚合,因此它與大多數版本的elasticsearch相容
如果您有任何建議或想法,歡迎提交問題或 Pull Request!
麻省理工學院