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!
麻省理工学院