1.查询所有索引
GET _cat/indices
GET _all/_settings
2.打开索引
kibana开启索引
POST http://localhost:9200/索引名/_open
POST http://localhost:9200/.kibana/_open
3.关闭索引
POST http://localhost:9200/索引名/_close
4.删除索引
DELETE 索引名
5.创建索引
PUT 索引名
***创建索引加字段***
PUT 索引
{
"mappings": {
"类型": {
"properties": {
"字段一": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"字段二": { //嵌套类 数组[]
"type": "nested",
"properties": {
"字段二(1)": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"字段二(2)": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"字段二(3)": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"字段二(4)": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"字段二(5)": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"字段三": {
"type": "long"
}
}
}
}
}
6.term 的使用(删除字段名称值为 *** 的所有记录)
GET 索引名/_delete_by_query
{
"query":{
"term":{
"字段名称":{
"value":"***"
}
}
}
}
7.sort 的使用
GET 索引名/_search
{
"query":{
"match_all":{}
},
"sort":[
{
"time":{
"order":"desc"
}
}
],
"form":0,
"size":100
}
8.range 的使用(age为例)
GET 索引名/_search
{
"query":{
"range":{
"age":{
"gte":10, ## 大于10
"lte":20 ## 小于20
]
}
}
}
9.Ids 的使用
GET 索引名/_search
{
"query":{
"ids":{
"values":[1,2,3]
}
}
}
10.fuzzy 的使用(模糊查询 name为例)
GET 索引名/_search
{
"query":{
"fuzzy":{
"name":{"value":"王"}
}
}
}
11.Boolean 的使用
must 查询结果必须符合该查询条件
should 类似于or的查询条件
must_not 查询结果必须不符合查询条件
GET 索引名/_search (must)
{ ## 查询age必须在10-20之间
"query":{
"bool":{
"must":[
{
"range":{
"age":{
"gte":10,
"lte":20
}
}
}
]
}
}
}
GET 索引名/_search (must_not)
{ ## 查询age不在10-20之间
"query":{
"bool":{
"must_not":[
{
"range":{
"age":{
"gte":10,
"lte":20
}
}
}
]
}
}
}
过滤器的用法
GET 索引名/_search
{ ## 过滤查询age大于10的所有数据
"query":{
"bool":{
"must":[{"match_all":{}}],
"filter":{
"range":{
"age":{
"gte":10
}
}
}
}
}
}
## 过滤查询age不为null的所有数据
{
"query":{
"bool":{
"must":[{"match_all":{}}],
"filter":{
"exists":{
"field":"age"
}
}
}
}
}
## 聚合 age的 min max avg sum
{
"aggs":{
"age":{
"stats":{
"field":"age"
}
}
}
}
索引备份
POST _reindex
{
"source": {
"index": "源索引"
},
"dest": {
"index": "目标索引"
}
}
ES中新增字段
PUT index/_mapping/type
{
"video": {
"properties": {
"字段": {
"type": "类型",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
字段聚合
GET 索引/_search
{
"size": 0,
"aggs": {
"字段名称": {
"terms":{
"field": "字段名称"
}
}
}
}
注释:
1.当聚合字段的属性为text时,必须给字段加上fielddate=true属性才可以聚合。
2.当聚合字段的属性为keyword(Long)时可以直接聚合
ES中聚合时的字段介绍
1.offset 时间偏移量("-8h")
2.interval 时间间隔(聚合的时间单位)年,月,日,时,分,秒
3.extended_bounds: { ## 聚合的范围
"min": 开始时间
"max": 结束时间
}
4.min_doc_count:0 (设置此属性,会返回聚合范围内的所有数据,包括为空的)
注释:
1.只有在桶聚合时存在时间偏移问题,一般查询过滤不会出现
ES should用法(or)
"query": {
"bool": {
"should": [
{"term": {"uuid.keyword": #[path]}},
{"term": {"checkJpg.keyword": #[path]}}
],
"minimum_should_match":1
}
}
注释:minimum_should_match表示最少返回的字段数 1 表示最少返回一个
ES 动态添加索引查询的最大条数
PUT 索引/_settings
{
"max_result_window":100000000
}
ES设置fielddata=true
聚合字段时 long字段可直接聚合
text:
1.加fielddata=true可聚合
2.字段加keyword也可聚合
PUT index/_mapping/type
{
"properties": {
"字段": {
"type": "text",
"fielddata": true
}
}
}
聚合:字段 lists 属性为 text
1. GET 索引/_search
{
"aggs": {
"lists": {
"terms": {
"field": "lists.keyword" //聚合时字段加keyword
}
}
},
"size": 0
}
2.加fielddata=true后聚合
GET 索引/_search
{
"aggs": {
"lists": {
"terms": {
"field": "lists" //直接聚合
}
}
},
"size": 0
}
ES索引备份
1.创建索引库
POST _snapshot/oneevent_center_index_backup
{
"type": "fs",
"settings": {
"location": "/home/upload/oneevent_center_index_20",
"max_snapshot_bytes_per_sec": "20mb",
"max_restore_bytes_per_sec": "20mb",
"compress": true
}
}
注释:
a: oneevent_center_index_backup索引库名(随意取)
b: oneevent_center_index_20 给索引起别名
2.往索引库中备份索引
PUT _snapshot/oneevent_center_index_backup/oneevent_center_index_20
{
"ignore_unavailable": true,
"indices": "audit_log_index", //选择要备份的索引
"include_global_state": true
}
3.恢复索引
POST _snapshot/oneevent_center_index_backup/oneevent_center_index_20/_restore
注释:
同服务器备份步骤 1-2-3
不同服务器:
源服务器 1-2
目标服务器 1-3
ES数据修改
1.单条数据修改(字符串)
POST 索引/类型/_id/_update
{
"doc":{
"修改字段":"修改内容"
}
}
2.单条数据修改(数组)
POST 索引/类型/_id/_update
{
"doc":{
"修改字段":["修改内容"]
}
}
3.批量修改(字符串)
POST 索引/类型/_update_by_query
{
"query": {
"match": {
"字段": "源内容"
}
},
"script": {
"lang": "painless",
"inline": "ctx._source[\"字段\"] =\"修改内容\""
}
}
4.批量修改(数组)
POST 索引/类型/_update_by_query
{
"query": {
"match": {
"字段": "源内容"
}
},
"script": {
"lang": "painless",
"inline": "ctx._source[\"字段\"] =[\"label57\"]"//数组中的字符串需要转义
}
}
根据curl删除数据
1.根据指定条件删除
curl -X POST http://ip:9200/索引/_delete_by_query?pretty -H 'Content-Type: application/json' -d '{"query": {"match": {"_id": "WTHgQX4BlJ5LWNRAJVV3"}}}'