elasticsearch常用语法
elasticsearch简介及安装
安装参考:https://blog.csdn.net/sinat_28224453/article/details/51134978.
elasticsearch是一个非常强大的分布式搜索引擎,也是一个分布式文档数据库,文档的字段一般需要预定义,否则虽然可以按照字段的值推测字段类型,但很容易出错,导致后期检索,聚合等操作出现错误。每个字段都可以被索引,查询速度接近实时,并且可以横向扩展。
常用语法
- 创建索引
curl -H "Content-Type: application/json" -XPUT '127.0.0.1:9200/index
{
"mappings": {
"type" : {
"properties" : {
"startTime" : { //时间类型
"type" : "date",
"format" : "strict_date_optional_time||epoch_millis||dd/MMM/yyyy:HH:mm:ss Z||yyyy-MM-dd HH:mm:ss"
},
"totalCount" : {
"type" : "long" //long
},
"partitionName" : {
"type" : "keyword" //关键字,类似于string
},
}
}
},
"settings" : {
"number_of_shards" : "72", //数据分片数
"number_of_replicas" : "2" //数据备份数
}
}'
- 查映射(查询字段类型)
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/_mapping?pretty'
- 无条件查询,查询全部
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty'
4.单条件查询
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"query" : {
"bool": {
"must": {
"match":{
"key":"value"
}
}
}
}
}'
- 单范围查询
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"query" : {
"bool": {
"filter": {
"range" : {
"time" : {"gte":"2019-08-01 00:00:00","lte":"2019-08-02 00:00:00"}
}
}
}
}
}'
- 多条件匹配
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"query": {
"bool": {
"must": [
{ "match": { "key1":"value1" }},
{ "match": { "key2":"value2"}}
]
}
}
}'
- 多范围查询
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"query" : {
"bool": {
"filter": {
"range" : {
"startTime" : {"gte":"2019-03-18 00:00:00"}
}
},
"filter": {
"range" : {
"totalCount" : { "gte" :"100" }
}
}
}
}
}'
8.按条件聚合(求和)
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"size":0,
"aggs":{
"sum_totalCount":{
"sum": {
"field": "totalCount"
}
}
},
"query" : {
"bool": {
"must":{
"match":{
"key1" : "value1"
}
}
}
}
}'
- 多条件聚合(按字段)
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"size":0,
"aggs":{
"group_by_key1":{
"terms": {
"field": "key1"
},
"aggs":{
"group_by_key2":{
"terms": {
"field": "key2"
}
}
}
}
}
}'
- 多条件聚合(按时间)
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty&size=30&from=0' -d '{
"size":0,
"aggs":{
"group_by_day":{
"date_histogram":{
"field":"time",
"interval":"day",
"format":"yyyy-MM-dd",
"min_doc_count": 0
},
"aggs":{
"group_by_key1":{
"terms":{
"field":"key1",
"min_doc_count": 0
}
}
}
}
}
}'
- 筛选后聚合
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"size":0,
"aggs":{
"sum_by_key":{
"sum": {
"field": "key"
}
}
},
"query" : {
"bool": {
"must": [
{ "match": { "key1":"盘value1市" }},
{ "match": { "key2" :"value2"}}
]
}
}
}'
- 排序
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"sort": {
"key": {
"order": "desc"
}
}
}'
- 字段是否存在
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"query" : {
"bool": {
"must": {
"exists" : {
"field":"key1",
"field":"key2"
}
}
}
}
}'
- 时间筛选加字段不匹配
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"query" : {
"bool": {
"filter": {
"range" : {
"time" : {"gte":"yyyy-mm-dd hh:mm:ss","lte":"yyyy-mm-dd hh:mm:ss"}
}
},
"must_not":{
"match":{
"key":"value"
}
}
}
}
}'
- 为不存在的字段添加默认值
curl -H "Content-Type: application/json" -XPOST '127.0.0.1:9200/index/type/_update_by_query?pretty' -d '{
"query":{
"bool":{
"must":[
{"range":
{
"time" : {"gte":"yyyy-mm-dd hh:mm:ss","lte":"yyyy-mm-dd hh:mm:ss"}
}
}
],
"must_not":[
{"exists":{"field":"key"}}
]}
},"script":{"source":"ctx._source.key=\"value\""}
}'
- 修改值
curl -H "Content-Type: application/json" -XPOST '127.0.0.1:9200/index/type/_update_by_query?pretty' -d '{
"query":{
"bool":{
"must":[
{"range":
{
"time" : {"gte":"yyyy-mm-dd hh:mm:ss","lte":"yyyy-mm-dd hh:mm:ss"}
}
},
{"match":
{
"key":"old_value"
}
}
]
}
},"script":{"source":"ctx._source.key=\"new_value\""}
}'
- 增加字段(慎用)
curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/index/_mapping/audit -d '{
"type": {
"properties":{
"key":{"type":"integer"}
}
}
}'
- 删除全部数据(慎用)
curl -H "Content-Type: application/json" -XPOST '127.0.0.1:9200/index/type/_delete_by_query?pretty' -d '{
"query": {
"match_all": {}
}
}'
- 列出索引详情
curl '127.0.0.1:9200/_cat/indices?v'