This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Home
iamazy edited this page May 7, 2019
·
3 revisions
套用elasticsearch-query-toolkit
## 搜索请求SQL结构
SELECT [fields] FROM [index.type] QUERY [score query condition] WHERE [bool query condition] ORDER BY [sort field] LIMIT [0, 10]
## 聚合查询SQL结构
SELECT [MIN(field),MAX(field),SUM(field),AVG(field)] from [index.type] QUERY [score query condition] WHERE [bool query condition] GROUP BY [agg methods]
以上两种SQL结构分别对应ES的搜索请求和聚合请求,下面大概看看这两种结构,后续会详细介绍。 1、搜索请求SQL
-
SELECT项,可以是通配符* 也可以是具体指定需要返回的字段(针对inner/nested文档直接通过.引用)
-
FROM项,后面跟的是索引名和文档类型如:product.car 表示查询product这个索引下的car类型
-
QUERY项,关键字QUERY和WHERE是同级的,不同之处是QUERY后面跟的查询条件会进行打分
-
WHERE项,和QUERY同级,后面跟的查询条件不会参与打分,只是进行简单的bool匹配
-
ORDER BY项,跟SQL一样后面跟排序条件
-
LIMIT 项,指定分页参数对应ES的from,size参数 2、 聚合请求SQL
-
SELECT/FROM/QUERY/WHERE 项同上
-
GROUP BY项,后面跟的是需要分组查询的条件,如前支持只支持:terms、range
为了能更好的说明SQL语法的使用,我们假设已经存在一个名叫product的索引,索引下有一个apple的类型,具体的mapping像下面这样,其中provider是一个inner doc表示供应商,buyers是一个nested doc表示购买者。
"mappings": {
"apple": {
"properties": {
"productName": {
"type": "string",
"index": "not_analyzed"
},
"productCode": {
"type": "string",
"index": "not_analyzed"
},
"minPrice": {
"type": "double"
},
"advicePrice": {
"type": "double"
},
"provider": {
"type": "object",
"properties": {
"providerName": {
"type": "string",
"index": "not_analyzed"
},
"providerLevel" : {
"type": "integer"
}
}
},
"buyers": {
"type": "nested",
"properties": {
"buyerName": {
"type": "string",
"index": "not_analyzed"
},
"productPrice": {
"type": "double"
}
}
}
}
}
}