Skip to content

Commit

Permalink
es命令修改为curl格式输出
Browse files Browse the repository at this point in the history
  • Loading branch information
yesAnd92 committed Apr 28, 2023
1 parent 90b914e commit 367ff8e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cp <下载的lwe文件路径> /usr/local/bin
> 最好将lwe_Mac重名成lwe,方便使用
>执行cp命令时,通常需要提升权限,可以在`cp`命令前增加`sudo`来解决

#### Win平台添加到环境变量的方法:
可以参照maven的配置方法
Expand Down Expand Up @@ -81,7 +81,7 @@ CREATE TABLE 'student_info' (
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生信息';
```

> 一个SQL文件中支持多个创建语句;
> 一个SQL文件中支持多个创建语句批量生成目标文件;
> 另外,实际使用中最好使用比如Navicat等工具导出的建表语句,识别率会更高。自己手写的可能由于语法或者拼写错误导致错误识别!
Expand Down Expand Up @@ -152,8 +152,7 @@ type StudentInfo struct {
lwe es [可选参数] <SQL语句>
```

如果经常使用ElasticSearch的dsl在本地做一些查询并且又不想记忆繁琐的语法,这个命令就派上用场了,毕竟大家对SQL语句还是信手拈来的。

这个命令可以帮我们从繁琐的ES查询语法中解脱出来,它可以将sql语句转换成响应的DSL,并且以curl命令的形式输出,这样服务器上也可以方便的使用。
当前版本支持的SQL操作

✅select
Expand Down Expand Up @@ -188,9 +187,8 @@ lwe es -p 'select * from user where age >18 order by create_time desc limit 10

生成的结果如下:

```json
// POST /user/_search
{
```bssh
curl -XPOST -H "Content-Type: application/json" -u {username}:{password} {ip:port}/user/_search?pretty -d '{
"from": 10,
"query": {
"bool": {
Expand All @@ -211,8 +209,10 @@ lwe es -p 'select * from user where age >18 order by create_time desc limit 10
"create_time": "desc"
}
]
}
}'
```


<h3 id="3">3、获取给定值的md5值</h3>
这个命令非常的简单,返回给定值的md5值,如果未给定值则随机返回一个md5值

Expand All @@ -235,7 +235,7 @@ lwe md5 yesAnd
## RoadMap
- fmt 根据需求支持更多类型的转换
- es 按需增加对insert、update、delete
......
......

## 开源协议

Expand Down
12 changes: 6 additions & 6 deletions cmd/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"lwe/handler/es"
)

const (
CURL_TPL = `curl -XPOST -H "Content-Type: application/json" -u {username}:{password} {ip:port}/%s/_search?pretty -d '%s' `
)

/**
* es命令
* 将sql语句转译成dsl语句
Expand All @@ -29,11 +33,10 @@ var (
return
}

var dsl, esType, urlMethod string
var dsl, esType string
switch stmt.(type) {
case *sqlparser.Select:
dsl, esType, err = es.HandleSelect(stmt.(*sqlparser.Select))
urlMethod = fmt.Sprintf("POST /%s/_search", esType)
case *sqlparser.Delete:
fmt.Println("Delete syntax is not supported this version!")
return
Expand All @@ -49,8 +52,6 @@ var (
fmt.Println(err)
return
}
//输出请求路径,比如 POST /index_type/_search
fmt.Println(urlMethod)

if fmtPretty {
//需要美化
Expand All @@ -59,8 +60,7 @@ var (
pr, _ := json.MarshalIndent(re, "", " ")
dsl = string(pr)
}

fmt.Println(dsl)
fmt.Printf(CURL_TPL, esType, dsl)
},
}
)
Expand Down

0 comments on commit 367ff8e

Please sign in to comment.