搜索语法:GET /_index/type/_id
/_search
在所有的索引中搜索所有的类型
/gb/_search
在 gb 索引中搜索所有的类型
/gb,us/_search
在 gb 和 us 索引中搜索所有的文档
/g*,u*/_search
在任何以 g 或者 u 开头的索引中搜索所有的类型
/gb/user/_search
在 gb 索引中搜索 user 类型
/gb,us/user,tweet/_search
在 gb 和 us 索引中搜索 user 和 tweet 类型
/_all/user,tweet/_search
在所有的索引中搜索 user 和 tweet 类型
*************es6.0新增sql语法************************
参考文档:https://www.cnblogs.com/xiaobaozi-95/p/9295194.html
POST /_xpack/sql?format=txt
{
"query": "SELECT SCORE(), * FROM twitter WHERE match(twitter, 'sql is') ORDER BY id DESC"
}
sql语句查询,format的选项有txt(表),json(默认),yaml,smile,cbor,csv,tsv
POST _xpack/sql/translate
{
"query":"select * from guide_37 order by roleId",
"fetch_size":3
}
sql translate api接受json文档中的sql并将其转换为es查询。
//聚合:多个aggs是嵌套关系
"aggs": {
"NAME": {
"AGG_TYPE": {
"field":""
},
"aggs": {
"histogram":{
"field": "price",
"interval": 20000
}
}
}
}
NAME:自定义名称
field:作用的项
AGG_TYPE:可选参数有avg(求平均值),min,max,sum,terms(group by),cardinality(distinct),histogram(直方图,需要两个参数:field,interval指定间隔)
****************************************************************
histogram:date_histogram 按时间(一个月)排序
"aggs": {
"sales": {
"date_histogram": {
"field": "time",
"interval": "1d",
"format": "yyyy-MM-dd" ,
"order": {
"_key": "asc"
}
}
}
}
***清空索引里面的所有数据******* POST indexName/_delete_by_query { "query": { "match_all": { } } } *****清空索引*********** DELETE /indexName
注释 : size:指定每页显示个数,from规定从哪开始--->手动分页
下面语句等价于:select * from getoneserver-* order by uid desc limit 0,20
GET /getoneserver-*/doc/_search
{
"from": 0,
"size":20,
"query": {
"match_all": {}
},
"sort": [
{
"uid": {
"order": "desc"
}
}
]
}
注释: bool >> must(and)/must_not(not)/should(or) >> match/match_phrase(长语句中间有空格不会被认为是or)/query_string(like)/range(指定范围)
下面语句等价于:select count() from getoneserver- where version='2.0.0' and platfrom like '%nd%' and uid = '6165210242' group by @timestamp (进行了时间转换)
GET /getoneserver-*/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{"match": {
"version": "2.0.0"
}},
{"query_string": {
"default_field": "platform",
"query": "*nd*"
}},
{"match_phrase": {
"uid": "6165210242"
}}
]
}
}
, "aggs": {
"NAME": {
"date_histogram": {
"field": "@timestamp",
"interval": "1d",
"format": "yyyy-MM-dd"
}
}
}
}
注释: 在2的基础上distinct(uid) ---> cardinality
GET /getoneserver-*/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{"match": {
"version": "2.0.0"
}},
{"query_string": {
"default_field": "platform",
"query": "*n*"
}},
{"match_phrase": {
"uid": "6165210242"
}}
]
}
}
, "aggs": {
"NAME": {
"date_histogram": {
"field": "@timestamp",
"interval": "1d",
"format": "yyyy-MM-dd"
},
"aggs": {
"distinct_uid": {
"cardinality": {
"field": "uid"
}
}
}
}
}
}
注释: [使查询结果高亮]: highlight
查找最近十分钟登录的人数(group by uid)
GET /getoneserver-*/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"cid": "1171400791"
}
},
{
"range": {
"@timestamp": {
"gt": "now-10m"
}
}
},
{
"match_phrase": {
"channel": {"query": "Hyz"}
}
}
]
}
},
"highlight": {
"fields": {
"@timestamp":{}
}
},
"aggs": {
"group_uid": {
"terms": {
"field": "uid"
}
}
}
}
查询具体的字段:“_source{"includes":["字段1","字段2"]}” //查询字段1,字段2
“_source{"excludes":["字段1","字段2"]}” //排除字段1,字段2
POST /login-g01-201/_search
{
"size": 300,
"query": {
"bool":{
"must": [
{
"match": {
"serviceName.keyword": "s1"
}
},
{
"match": {
"platform.keyword": "android"
}
},
{
"range": {
"@timestamp": {
"gte": "now-10d"
}
}
}
]
}
},
"_source":{
"includes": ["rid","logintime"]
}
,"aggs": {
"distinct_rid": {
"cardinality": {
"field": "rid"
}
}
}
}
❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤es查询的几个坑❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
①通过每天时间聚合查询时:
"aggs": {
"group_date": {
"date_histogram": {
"field": "@timestamp",
"interval": "1d",
"format": "yyyy-MM-dd",
"time_zone": "Asia/Shanghai",
"min_doc_count": 1
}
}
一定要加上time_zone,不然时区不同,会截止到早上八点而不是凌晨0点,另把min_doc_count加上,查询有效的数据
②聚合查询只想查找聚合的数据不想查找结果
"size": 0,
据说 size:-1,查询所有结果,但是经试验无效
③聚合查询时,千万不要忘了如果聚合对象结果大于10,一定要加上size
"aggs": {
"lossnode": {
"terms": {
"size": 11,
"field": "Node.keyword"
}
}
}
④elastic删除批量数据
GET red_231,item_231/_delete_by_query?waitForCompletion=false
{
"query":
{"bool": {
"must": [{"terms": {
"roleName": [
"名字1",
"名字2"
]
}}]
}
}
}
注:waitForCompletion=false 立即返回删除是否成功,会返回个taskId
GET /_tasks/taskId 来查询是否成功