这里我就以Mysql作为对照举例子
1、select 字段
当你只想要某些字段时(不写则默认要全部字段)
GET index_name/_search
{
"_source": ["name","mobile"]
}
当你不想要某些字段时
GET index_name/_search
{
"_source": {
"excludes": ["age","email"]
}
}
当你只想要某些字段,又不想要某些字段时
GET index_name/_search
{
"_source": {
"includes": ["name","mobile"],
"excludes": ["age","email"]
}
}
2、模糊匹配(此功能对text类型才生效,当查询的是非字符串时,为精确匹配)
GET index_name/_search
{
"query": {
"match": {
"name": "小明"
}
}
}
3、精准匹配
GET index_name/_search
{
"query": {
"term": {
"name": "小明"
}
}
}
4、多值精确匹配,类似于SQL中的in功能
GET index_name/_search
{
"query": {
"terms": {
"name": [ "小明", "小红"]
}
}
}
5、范围查询
- gt: greater than 大于
- gte: greater than or equal 大于等于
- lt: less than 小于
- lte: less than or equal 小于等于
GET index_name/_search
{
"query": {
"range": {
"age": {
"gte": 23,
"lte": 30
}
}
}
}
6、where and 功能
GET index_name/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "小明"
}
},
{
"match": {
"age": "18"
}
}
]
}
}
}
7、where or 功能
GET index_name/_search
{
"query": {
"bool": {
//满足任意一个即可
"minimum_should_match": 1,
"should": [
{
"match": {
"name": "小明"
}
},
{
"match": {
"age": "18"
}
}
]
}
}
}
8、where 不匹配 and功能
GET index_name/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "小明"
}
},
{
"match": {
"age": "18"
}
}
]
}
}
}
9、group by 功能
GET index_name/_search
{
"query": {
//查询所有数据
"match_all": {}
},
"aggs": {
//按名称分组
"nameAggs": {
"terms": {
"field": "name"
}
}
}
}
10、order by 功能
GET index_name/_search
{
"query": {
//查询所有数据
"match_all": {}
},
"sort": [
{
"name": "desc"
},
{
"age": "asc"
}
]
}
11、limit功能(es默认条数最多为10000条,通过命令(重启会无效)/配置可以修改,超过es条数会报错),不传默认为10条
GET index_name/_search
{
"query": {
//查询所有数据
"match_all": {}
},
"from": 1,
"size": 10
}
12、count功能
GET index_name/_count
13、must和should共存情况
query = {
"query": {
"bool": {
"must": [
{"term": {"color": "red"}}
],
#当must存在的时候,should中的条件是可有可无的,就是must条件满足就行,should的一个都不用满足也可以
#当must不存在的时候,should中的条件至少要满足一个
"should": {
{"term": {"size": 33}},
{"term": {"size": 55}}
},
#所以当must存在,又想让should的条件至少满足一个地加这个参数
#也可以再must》term统计再加一个bool》must》should
"minimum_should_match":1
}
}
}