GET _validate/query?explain
curl -X GET "localhost:9200/_nodes/hot_threads"
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
PUT /_settings
{
"index.mapper.dynamic":false
}
注意:es6之后才支持
动态修改:
PUT _cluster/settings { “persistent”: { “search.max_buckets”: 20000 } }
经常修改:
编辑elasticsearch.yml
添加search.max_buckets: 20000
curl -u用户名:密码 http://ip:9200
首先目标索引targetindex 需要创建好,reindex不会帮你创建目标索引的
{
"source": {
"index": "sourceindex",
"size": 10000 //每批插入数据的量
},
"dest": {
"index": "targetindex"
}
}
一个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
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
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-preference.html
在查询的_search后加上?version, 查询返回的结果中就会带上version字段
PUT _cluster/settings
{
"persistent": {
"cluster.routing.allocation.node_concurrent_recoveries": 10,
"indices.recovery.max_bytes_per_sec": "40mb"
}
}
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 命令查看:
curl -XGET ‘http://localhost:8200/zh/_analyze?analyzer=ansj_index_synonym&pretty’ -d ‘童装童鞋’
curl -XGET ‘http://localhost:8200/zh/_analyze?tokenizer=ansj_index&filters=synonym&pretty’ -d ‘童装童鞋’
curl -XGET ‘http://localhost:8200/zh/_analyze?field=brand_name&pretty’ -d ‘童装童鞋’
“brand_name”:字段名称,如果是字段是nest,object类型,也可以写成"brand_name. name"
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"
}
}
http://127.0.0.1:9200/_cluster/settings
http://127.0.0.1:9200/_nodes/stats/process?pretty
PUT /index/_settings
{“refresh_interval”: “10s”}
PUT index01/_settings
{
"number_of_replicas": 2
}
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
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中只能有一个,不然会报错!
#插入文档
####指定ID插入
Method:PUT
http://192.168.237.130:9200/people/man/1
{
"name":"wei",
"country":"china",
"age":28,
"data":"1293-12-33"
}
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"
}
}
}
}
在查询过程中,除了判断文档是否满足查询条件,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
}
}
}
}
在查询过程中,只判断该文件是否满足条件,只有Yes或者No
{
"query":{
"bool":{
"filter":{
"term":{
"word_count":1000
}
}
}
}
}
{
"query":{
"constant_score":{
"filter":{
"match":{
"title":"ES"
}
},
"boost":2
}
}
}
"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