• Stars
    star
    1,091
  • Rank 40,837 (Top 0.9 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

convert sql to elasticsearch DSL in golang(go)
 _____ _         _     ____ _____ ___  ____  ____   ___   _
| ____| |       / \   / ___|_   _|_ _|/ ___|/ ___| / _ \ | |
|  _| | |      / _ \  \___ \ | |  | || |    \___ \| | | || |
| |___| |___  / ___ \  ___) || |  | || |___  ___) | |_| || |___
|_____|_____|/_/   \_\|____/ |_| |___|\____||____/ \__\_\|_____|

Overview

Build Status Go Documentation Coverage Status Go Report Card

This tool converts sql to elasticsearch 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

Usage

go get -u github.com/cch123/elasticsql

Demo :

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)
}

will produce :

{
    "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

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

Warning

To use this tool, you need to understand the term query and match phrase query of elasticsearch.

Setting a field to analyzed or not analyzed will get different results.

Details

For more details of convertion, please refer to the wiki

Other info

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!

License

MIT

More Repositories

1

golang-notes

Go source code analysis(zh-cn)
HTML
3,761
star
2

asmshare

some ideas about asm && plan9 asm video: https://www.bilibili.com/video/BV1Xb411J7Yk
349
star
3

asm-cli-rust

interative assembly shell written in rust
Rust
255
star
4

asm-cli

Interactive shell of assembly language(X86/X64) based on unicorn and keystone
Go
250
star
5

supermonkey

Patch all Go functions for testing
Go
244
star
6

gogctuner

auto adjust your GOGC value
Go
173
star
7

leetcode-go

leetcode in golang
Go
81
star
8

awesome-gc

A curated list of awesome garbage collection resources
67
star
9

llp-trans

Low Level Progarmming Chinese translation(WIP)
65
star
10

goroutineid

extract goroutine id to the out world although it's dangerous and useless.
Go
56
star
11

leetcode-rust

leetcode in rust
Rust
51
star
12

gomod-conflict-detect

detect dependencies which may conflict
Go
49
star
13

elastic-rs

comvert bool expression to elasticsearch DSL in rust
Rust
43
star
14

eco2020

for English learning, 看不到图片(github usercontent 部分被 block)请梯子,有需要可以 fork,哪天被 DMCA 了也不奇怪
35
star
15

go-ddd

Go
22
star
16

how-ai-will-change-our-work

18
star
17

iapp

《程序员的算法趣题》Go 解法(WIP)
Go
18
star
18

whisper-tts

Go
17
star
19

parser_example

various parser demos
Rust
15
star
20

test

learn and test examples
HTML
15
star
21

perf_workshop_2021

Go
13
star
22

bottlenecks-go

Cases for high concurrency go programs
12
star
23

asciidoc-rs

asciidoc parser in pest(WIP)
Rust
10
star
24

knowledge-index

knowledge index for my career
10
star
25

cch123.github.io

personal share
HTML
8
star
26

blog_comment

comments of xargin.com
8
star
27

rust-book-code-collect

collect code of various rust books
Rust
8
star
28

cch123

6
star
29

collins-wordlist

collins dict word list
5
star
30

threadkill

Go
2
star
31

stackdiff

2
star
32

nginx-annotated

C
2
star
33

cmake-learn

CMake
2
star
34

structs

Some useful structs implemented by go including skiplist...
Go
1
star
35

monkey

hand write parser test
Go
1
star
36

gomodexp

go mod experiments
Go
1
star
37

mosn_easy_configure

Vue
1
star
38

woboq-lua-5.0

lua code online preview
HTML
1
star
39

wrkrs

modern wrk
Rust
1
star
40

schema-parser

convert between pb, thrift, avro or yaml schema
1
star
41

voice-clone-demo

Python
1
star
42

woboq-script

CMake
1
star
43

crawl_log

Go
1
star
44

woboq-nginx-1.13

nginx 1.13 code online preview
HTML
1
star
45

algos-svg

record the process of writing algorithms
1
star
46

hazama

personal web framework
1
star
47

pdf-url-remover

remove link/url in pdf files
Go
1
star
48

paper-translation-project

1
star
49

woboq-redis-3.0

redis 3.0 code online preview
HTML
1
star
50

pest_gen

generate basic layout of pest parsers(WIP)
Rust
1
star