This repository is a fork of cch123/elasticsql. I'm moving from Elasticsearch to OpenSearch, so I decided to fork this project to attend my needs. That's why I changed the name of the project to SQL2DSL. You should use the original one.
This tool converts sql to dsl
Currently support:
- sql and expression
- sql or expression
- equal(=) support
- not equal(!=) support
- gt(>) support
- gte(>=) support
- lt(<) support
- lte(<=) support
- sql in (eg. id in (1,2,3) ) expression
- sql not in (eg. id not in (1,2,3) ) expression
- paren bool support (eg. where (a=1 or b=1) and (c=1 or d=1))
- sql like expression (currently use match phrase, perhaps will change to wildcard in the future)
- sql order by support
- sql limit support
- sql not like expression
- field missing check
- support aggregation like count(*), count(field), min(field), max(field), avg(field)
- support aggregation like stats(field), extended_stats(field), percentiles(field) which are not standard sql function
- null check expression(is null/is not null)
- join expression
- having support
go get -u github.com/seriallink/sql2dsl
Demo :
package main
import (
"fmt"
"github.com/seriallink/sql2dsl"
)
func main() {
sql := `select *
from document
where code = '1234'
and tags like '%test%'`
dsl, _, err := sql2dsl.Convert(sql)
if err != nil {
panic(err)
}
fmt.Print(dsl)
}will produce :
{
"query": {
"bool": {
"must": [{
"match_phrase": {
"code": {
"query": "1234"
}
}
}, {
"wildcard": {
"tags": {
"value": "*test*"
}
}
}]
}
},
"from": 0,
"size": 1
}If your sql contains some keywords, eg. order, timestamp, don't forget to escape these fields as follows:
select * from `order` where `timestamp` = 1 and `desc`.id > 0
To use this tool, you need to understand the term query and match phrase query.
Setting a field to analyzed or not analyzed will get different results.
For more details of convertion, please refer to the wiki
When writing this tool, I tried to avoid the deprecated dsl filters and aggregations, so it is compatible with most versions of the elasticsearch
If you have any advices or ideas, welcome to submit an issue or Pull Request!
MIT