0x00 SQL 转 Query-DSL 插件,还用毛学DSL
法器在此: https://github.com/NLPchina/elasticsearch-sql/
0x01 基本语法
基本语法是 [GET|POST] http://domain.com/you_index_name/type1,type2/_search{?search_type=count|scan|…}
注意,随着ES版本变化,搜索语法也有小调整。本文以1.7为准。
- 全基于rest式http调用。 其中GET方法支持在body传参数。
_search是关键字,以此结束表示搜索行为,可以同时搜索多个index与type。search_type值为count时,任何时候都不返回hits部分,减少返回内容;更多选项请参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html- body部分必需是
json;同时支持 URL 中使用query_string传参; - 搜索请求会以
query_string参数优先,且在 URL 中的参数有可能使用简写,如q==query。 - 可以同时索引多个index或type,逗号隔开,或直接使用
通配符。 - 更丰富官方索引文档在此 https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
0x02 例子:Group By – 单条件
curl -XPOST 'http://localhost:9200/index-x/type-x/_search?search_type=count' -d'{ "aggs": { "group_by_xxoo": { "terms": { "field": "stype" } } }, "query": { "term": { <1> "ftype": "file" } }, "from": 0, "size": 100 }'
<1>处的query中,使用match或term效果是一样的,表示包含此分词。term性能稍胜10%。
相当于SQL:
SELECT COUNT(*) AS
group_by_xxooFROMindex-1.type-xWHEREftype=’file’ GROUP BYstypeLIMIT 0, 100;
0x03 多条件查询bool:must、must_not、should
官方例子是:Query-DSL-bool-query(注:例子中少了query这个key。)
{ "query": { "bool": { <0> "must": { <1> "term": { "user": "kimchy" } }, "must_not": { "range": { "age": { "from": 10, "to": 20 } } }, "should": [ { "term": { "tag": "wow" } }, { "term": { "tag": "elasticsearch" } } ], "minimum_should_match": 1, <2> "boost": 1 <3> }, "from": 0, "size": 100 } }
<0> bool称为query的过虑器,还有很多过虑器,如:and,or,not,limit
<1> must、must_not,should为过虑条件,如果多个子条件,使用[],如should有两个子条件。
<2> minimum_should_match 用于限制should的子条件匹配个数。
<3> boost表示此过虑器的权重,默认值1.0。
- 过虑器支持嵌套 – 呆 -_=!,可以写一个很复杂的
Query-DSL. - 由于
Query-DSL查询语言过于复杂无比,关键字非常多。本人编不下去了。用到再记下来。
0x04 Query-DSL 常用关键字:
query,search_type
term,terms,match,match_phrase,multi_match,fuzzy
bool,and,or,not,limit,must,must_not,should
range,size,from,to,gt,gte,lt,lte
field,fields
aggs,count,sum,min,max,avg
-_=! 太多关键字,都可以出本字典了。本人又编不下去了。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211895.html原文链接:https://javaforall.net
