elasticsearch常用语法

elasticsearch常用语法

elasticsearch简介及安装

安装参考:https://blog.csdn.net/sinat_28224453/article/details/51134978.
elasticsearch是一个非常强大的分布式搜索引擎,也是一个分布式文档数据库,文档的字段一般需要预定义,否则虽然可以按照字段的值推测字段类型,但很容易出错,导致后期检索,聚合等操作出现错误。每个字段都可以被索引,查询速度接近实时,并且可以横向扩展。

常用语法

  1. 创建索引
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" //数据备份数
  }

}'
  1. 查映射(查询字段类型)
curl -H "Content-Type: application/json"    -XGET '127.0.0.1:9200/index/_mapping?pretty'
  1. 无条件查询,查询全部
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"
				}
            }
        }
    }
}'
  1. 单范围查询
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"}
                }
            }
        }
    }
}'
  1. 多条件匹配
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"}}
			]
		}
  }
}'
  1. 多范围查询
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"
              }
           }
        }
    }
}'
  1. 多条件聚合(按字段)
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"
				}
			}
		}
	}
}
}'
  1. 多条件聚合(按时间)
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
				}
			}
		}
	}
}
}'
  1. 筛选后聚合
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"}}
			   ]
			
        }
    }
}'
  1. 排序
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/index/type/_search?pretty' -d '{
"sort": {
		"key": {
			"order": "desc"
		}
	}
}'
  1. 字段是否存在
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"
                }
            }
        }
    }
}'
  1. 时间筛选加字段不匹配
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"
				}
			}
        }
    }
}'
  1. 为不存在的字段添加默认值
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\""}
}'
  1. 修改值
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\""}
}'
  1. 增加字段(慎用)
curl -H "Content-Type: application/json"  -XPUT 127.0.0.1:9200/index/_mapping/audit -d '{ 
"type": { 
	"properties":{
		"key":{"type":"integer"}
	}
}
}'
  1. 删除全部数据(慎用)
curl -H "Content-Type: application/json" -XPOST '127.0.0.1:9200/index/type/_delete_by_query?pretty' -d '{
"query": {
        "match_all": {}
    }
}'
  1. 列出索引详情
curl '127.0.0.1:9200/_cat/indices?v'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值