ES的基本操作

定位查询语句语法问题的API

GET _validate/query?explain

查看热点线程的命令

curl -X GET "localhost:9200/_nodes/hot_threads"

https://www.elastic.co/guide/en/elasticsearch/reference/5.5/cluster-nodes-hot-threads.html

开启日志记录传入传出的请求

PUT _cluster/settings
{
   "transient" : {
      "logger.org.elasticsearch.transport.TransportService.tracer" : "TRACE"
   }
}

只记录走9300的tcp请求

如果要关闭的话,将日志级别改成INFO,如下:

PUT _cluster/settings
{
   "transient" : {
      "logger.org.elasticsearch.transport.TransportService.tracer" : "INFO"
   }
}

官方手册:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-transport.html

怎么查看等待执行的集群任务

通过以下命令查看等待执行的集群任务总数

/_cluster/health

返回中的number_of_pending_tasks字段就是任务总数

通过以下命令查看等待执行的具体任务是什么

/_cluster/pending_tasks 或  /_cat/pending_tasks

如何关闭es mapping自动推导数据类型

PUT /_settings 
{
  "index.mapper.dynamic":false
}

设置聚合查询bucket 限制

注意:es6之后才支持
动态修改:
PUT _cluster/settings { “persistent”: { “search.max_buckets”: 20000 } }
经常修改:
编辑elasticsearch.yml
添加search.max_buckets: 20000

安全相关

安装searchGuard后如何通过curl访问

curl -u用户名:密码  http://ip:9200

reindex 的使用

首先目标索引targetindex 需要创建好,reindex不会帮你创建目标索引的

{
  "source": {
    "index": "sourceindex",
    "size": 10000  //每批插入数据的量
  },
  "dest": {
    "index": "targetindex"
  }
}

段相关

查看一个node上所有segment占用的memory总和

一个Lucene段就是一个完整的倒排索引,倒排索引由单词词典和倒排列表组成。在Lucene中,单词词典中的FST结构会被加载到内存。因此每个段都会占用一定的内存空间。可以通过下面API来查看某个节点上所有段占用的内存总量。

curl http://192.168.58.158:9200/_cat/nodes?v&h=segments.count,segments.memory,segments.index_writer_memory,segments.version_map_memory,segments.fixed_bitset_memory

修改merge的线程数

curl -XPUT  -H "Content-Type:application/json" 'http://localhost:9200/_all/_settings' -d '{

  "index.merge.scheduler.max_thread_count" : "1",
}'

段合并

POST
_forcemerge?max_num_segments=1

配置熔断器

PUT   http://localhost:9200/_cluster/settings
{
  "persistent": {
    "indices.breaker.fielddata.limit": "15%",
    "indices.breaker.request.limit": "15%",
    "indices.breaker.total.limit": "10%"
  }
}

指定分片查询

指定查询副本分片

curl localhost:9200/_search?preference=_replica 

指定查询主分片

curl localhost:9200/_search?preference=_primary

指定查询指定节点上的分片

curl localhost:9200/_search?preference=_only_node:xyz

more

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-preference.html

如何查看文档的version

在查询的_search后加上?version, 查询返回的结果中就会带上version字段

加快分片恢复速度

PUT _cluster/settings

{
    "persistent": {
        "cluster.routing.allocation.node_concurrent_recoveries": 10,
        "indices.recovery.max_bytes_per_sec": "40mb"
    }
}

加快分片rebalance速度

PUT _cluster/settings

{
    "persistent": {
        "cluster.routing.allocation.cluster_concurrent_rebalance": 10
    }
}

查询缓存

curl 'localhost:9200/my_index/_search?request_cache=true' -d'
{
  "size": 0,
  "aggs": {
    "popular_colors": {
      "terms": {
        "field": "colors"
      }
    }
  }
}
'

request cache只能缓存size=0的查询
开启request cache
在配置文件中加如下:

index.requests.cache.enable: true
curl -XPOST 'localhost:9200/kimchy,elasticsearch/_cache/clear?request_cache=true'

清空缓存

分词查询

查看分词的命令, ES配置完成后需要测试分词,看看分词是否达到预期效果。

curl 命令查看:

  1. 使用自定义的分析器查看分词:ansj_index_synonym:自定交分析器名称. pretty :json格式显示

curl -XGET ‘http://localhost:8200/zh/_analyze?analyzer=ansj_index_synonym&pretty’ -d ‘童装童鞋’

  1. 使用自定义的分词器(tokenizer)和过滤器(filters)查看分词:

curl -XGET ‘http://localhost:8200/zh/_analyze?tokenizer=ansj_index&filters=synonym&pretty’ -d ‘童装童鞋’

  1. 查询某个字段的分词:

curl -XGET ‘http://localhost:8200/zh/_analyze?field=brand_name&pretty’ -d ‘童装童鞋’

“brand_name”:字段名称,如果是字段是nest,object类型,也可以写成"brand_name. name"

修改index.max_result_window

curl -XPUT 192.168.40.31:9200/_all/_settings -d '{ "index.max_result_window" :"1000000"}'

如何设置磁盘空间告警线

PUT _cluster/settings
{
  "transient": {
    "cluster.routing.allocation.disk.watermark.low": "100gb",
    "cluster.routing.allocation.disk.watermark.high": "50gb",
    "cluster.routing.allocation.disk.watermark.flood_stage": "10gb",
    "cluster.info.update.interval": "1m"
  }
}

如何查看集群的setting

http://127.0.0.1:9200/_cluster/settings

查看文件句柄数量

http://127.0.0.1:9200/_nodes/stats/process?pretty

refresh操作可以通过API设置:

PUT /index/_settings

{“refresh_interval”: “10s”}

修改索引的副本个数

PUT index01/_settings

{

    "number_of_replicas": 2

}

开启慢查询日志

PUT
http://127.0.0.1:9200/index/_settings
{
     "index.search.slowlog.threshold.query.warn" : "10s", 
     "index.search.slowlog.threshold.fetch.debug": "500ms", 
     "index.indexing.slowlog.threshold.index.info": "5s" 
}

监控和查看集群状态

查看集群各节点内存使用情况
http://127.0.0.1:9200/_cat/nodes?v&h=name,port,sm
查看集群的健康状态
http://127.0.0.1:9200/_cat/health?v
查看集群的索引数
http://127.0.0.1:9200/_cat/indices?v
// 只显示状态为黄色的
GET /_cat/indices?v&health=yellow
// 根据文档数降序排列
GET /_cat/indices?v&s=docs.count:desc
// 显示每个索引占的内存
GET /_cat/indices?v&h=i,tm&s=tm:desc
查看集群所在磁盘的分配状况
http://127.0.0.1:9200/_cat/allocation?v
查看集群的节点
http://127.0.0.1:9200/_cat/nodes?v
查看别名,路由
http://127.0.0.1:9200/_cat/aliases?v
查看文档数量
http://127.0.0.1:9200/_cat/count?v
查看fieldData的大小
GET /_cat/fielddata?v
// 查看指定的字段
GET /_cat/fielddata?v&fields=body
GET /_cat/fielddata/body,soul?v

查看节点上fielddata

curl -X GET "localhost:9200/_cat/nodes?v&h=fielddata.memory_size"
查看主节点
GET /_cat/master?v
分片的恢复情况
GET _cat/recovery?v
查看集群的其它信息
http://127.0.0.1:9200/_cat/

创建索引

Method:PUT
http://192.168.237.130:9200/people
{
	"settings":{
		"number_of_shards": 3,
		"number_of_replicas": 1
	},
	"mappings":{
		"man":{
			"properties":{
				"name": {
					"type":"text"
					
				},
				"country": {
					"type":"keyword"
				},
				"age":{
					"type":"integer"
				},
				"date":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				}
			}
		}
	}
	
}

注意:5.x版本中索引创建支持多个type,但是在6.x中只能有一个,不然会报错!

添加字段

Method:PUT
http://x.x.x.x:9200/index/_mappings/type

{
    "properties": {
        "field": {
            "type": "short"
        }
    }
}

插入文档

指定ID插入
Method:PUT
http://192.168.237.130:9200/people/man/1
{
	"name":"wei",
	"country":"china",
	"age":28,
	"data":"1293-12-33"
}
自动ID插入
Method:POST
http://192.168.237.130:9200/people/man/
{
	"name":"wei2",
	"country":"china",
	"age":23,
	"data":"1293-12-23"
}

修改文档

直接修改文档
Method:POST
http://192.168.237.130:9200/people/man/1/_update
{
	"doc":{
		"name":"who am I"
	}
}
通过脚本修改文档
{
	"script":{
		"lang":"painless",
		"inline": "ctx._source.age = params.age",
		"params": {
			"age":100
		}
	}
}

删除

删除文档

Method:DELETE
http://192.168.237.130:9200/people/man/1

删除索引

Method:DELETE
http://192.168.237.130:9200/people

删除数据

POST indexName/_delete_by_query
{
  "query": { 
    "match_all": {
    }
  }
}

查询

简单查询
Method:GET
http://192.168.237.130:9200/book/novel/1
条件查询
Method
http://192.168.237.130:9200/book/_search
查询全部
{
	"query":{
		"match_all":{}
	},
	"from":1,
	"size":1
}

from 表示从结果中的第几条开始取数据,size表示取多少条

根据条件查询
{
	"query":{
		"match":{
			"title":"ES"
		}
	},
	"sort":[
		{
		"publish_date":{"order":"desc"}}
	]
}
聚合查询
{
	"aggs":{
		"group_by_word_count":{
			"terms":{
				"field":"word_count"
			}
		},
		"group_by_publish_date":{
			"terms":{
				"field":"publish_date"
			}
		}
	}
}
子条件查询:Query Context

在查询过程中,除了判断文档是否满足查询条件,ES还会计算一个_score来标志匹配的程度,旨在判断目标文档和查询条件匹配有多好

  • 全文本查询:针对文本类型数据
    模糊匹配:

{
   "query":{
   	"match":{
   		"author":"李四"
   	}
   }
}

完全匹配

{
   "query":{
   	"match_phrase":{
   		"author":"李四"
   	}
   }
}

多个字段匹配查询

{
   "query":{
   	"multi_match":{
   		"query":"李四",
   		"fields":["author","title"]
   	}
   }
}

query_string 查询

{
   "query":{
   	"query_string":{
   		"query":"李四 AND ES"
   	}
   }
}
  • 字段级别查询:针对结构化数据,如数字,日期等
    字段查询
{
   "query":{
   	"term":{
   		"word_count":1000
   	}
   }
}

范围查询

{
   "query":{
   	"range":{
   		"word_count":{
   			"gte":1000,
   			"lte":2000
   		}
   	}
   }
}
子条件查询:Filter Context

在查询过程中,只判断该文件是否满足条件,只有Yes或者No

{
	"query":{
		"bool":{
			"filter":{
				"term":{
					"word_count":1000
				}
			}
		}
	}
}
符合条件查询
  • 固定分数查询
{
	"query":{
		"constant_score":{
			"filter":{
				"match":{
					"title":"ES"
				}
			},
			"boost":2
		}
	}
}
  • 布尔查询
    should是或逻辑,must是与逻辑

	"query":{
		"bool":{
			
				"should":[
					{
						"match":{
							"author":"李四"
						}
					},
					{
						"match":{
							"title":"ES"
						}
					}
					]
			
		}
	}
}

查询空值

ES2.x

{
	"query":{
		"missing":{
			"field":"value"
		}
	}
}

ES5.x以上
ES5.x以后就没有missing查询了,需要用以下查询代替

{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "value"
                }
            }
        }
    }
}

手动段合并

POST /indexName/_optimize?max_num_segments=1

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值