ElasticSearch常用Restful API汇总

索引插入数据

PUT test_doctor/_doc/1
{
  "name1":"樊万虎",
  "name2":"樊万虎",
  "nums":"20200915,20200922,20200929"
}

新建索引Mapping

PUT /test_doctor/_mapping/_doc
{
  "_doc": {
    "properties": {
      "name1": {
        "type": "text",
        "analyzer": "index_ansj_analyzer"
      },
      "name2": {
        "type": "text"
      },
      "nums": {
        "type": "text",
        "analyzer": "comma"
      }
    }
  }
}

新建索引Setting

PUT /test_doctor
{
    "settings":{
        "number_of_shards":1,
        "analysis":{
            "analyzer":{
                "index_ansj_analyzer":{
                    "type":"custom",
                    "tokenizer":"index_ansj",
                    "filter":[
                        "my_synonym",
                        "asciifolding"
                    ]
                },
                "comma":{
                    "type":"pattern",
                    "pattern":","
                },
                "shingle_analyzer":{
                    "type":"custom",
                    "tokenizer":"standard",
                    "filter":[
                        "lowercase",
                        "shingle_filter"
                    ]
                }
            },
            "filter":{
                "my_synonym":{
                    "type":"synonym",
                    "synonyms_path":"analysis/synonym.txt"
                },
                "shingle_filter":{
                    "type":"shingle",
                    "min_shingle_size":2,
                    "max_shingle_size":2,
                    "output_unigrams":false
                }
            }
        }
    }
} 

移除集群中某一个节点

PUT _cluster/settings
{
  "transient" : {
    "cluster.routing.allocation.exclude._ip" : "10.0.0.1"
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/allocation-filtering.html

更新一条doc的某一个字段更新

POST sphinx-doctor/_doc/4861/_update
{    
   "doc" : { 
        "name" : "齐美”    
    } 
}

开启或关闭shard分配

开启:
PUT /_cluster/settings
{
    "transient" : {
        "cluster.routing.allocation.enable" : "all"
    }
}
curl -X PUT http://0.0.0.0:9200/_cluster/settings?pretty -d '{"transient":{"cluster.routing.allocation.enable":"all"}}

关闭:
PUT /_cluster/settings
{
    "transient" : {
        "cluster.routing.allocation.enable" : "none"
    }
}
curl -X PUT http://0.0.0.0:9200/_cluster/settings?pretty -d '{"transient": {"cluster.routing.allocation.enable": "none"}}' 

注意:新建索引的过程要分片,禁用了分配分片,自然也就无法新建索引。已经分配了分片只是不能再在节点之间迁移,不会影响读写。集群重启的时候关闭allocation,是为了防止节点重启时节点上的分片被重新分配,这个过程很耗费资源,而且重启是很快完成的,当集群快速重启后,其上的分片大部分是可以直接分配来用的,这样也就可以加速集群完成分片的速度,从而加速重启的速度。禁用或者不禁用allocation都不会影响业务,只是可能由于节点下线的时候由于分片重新分配导致es状态由yello到green的时间变长,从而导致重启时间边长。

删除索引下的全部数据

POST sphinx-doctor/_delete_by_query?refresh
{ "query": { "match_all": {} } }

查看索引分片文档数

GET /sphinx-diseasedoctor/_stats?level=shards

补充:查看某个索引的索引信息
GET _cat/indices/sphinx-diseasedoctor?v

通过id删除数据

DELETE
http://index1.search.prod.idc1:9200/flowindex_20180918-10:15:25/flow/5785743737

查看Es节点信息

http://10.1.22.23:9200/_nodes?pretty
http://10.3.19.30:9200/_nodes?pretty

http://192.168.0.75:9200/_nodes/stats?pretty
http://10.3.19.30:9200/_nodes/stats?pretty

测试Es的分词效果

POST _analyze
{ 
    "analyzer":"index_ansj”, 
    "text":"十堰市红十字医院”  
} 
POST _analyze 
{ 
    "analyzer":"query_ansj”, 
    "text":"十堰市红十字医院”  
}

对索引指定字段的分词器进行分词结果查询
post index_name/_analyze
{
   "field":"firtname", #要进行分析的索引中的字段
   "text":"ni cai" #要进行分析的文本 
}
例如:
POST
http://index1.search.prod.idc1:9200/doctorindex_20170830-17:00:35/_analyze
{
  "field": "drName.drNgramName",   
  "text": "成雄超"
}

自定义分词器进行测试
post _analyze
{
   "tokenizer":"standard", #指定的分词器
   "filter":["lowercase"],  #对分析后的词最后进行小写转换
   "text":"hello world" #要进行分析的文本
}
例如:
POST
http://index2.test.search.bj1:9200/_analyze
{
  "char_filter": [],
  "tokenizer": "standard",
  "filter": [
    "stop",
    "lowercase",
    "stemmer"
  ],
  "text": "Eating an apple a day keeps do"
}

//查看所有字段的分词结果
http://ip:port/indexName/indexType/id/_termvectors?fields=*  
例如:http://index2.test.search.bj1:9200/sphinx-diseasehospital/diseasehospital/5617268366/_termvectors?fields=*

//查看某一字段的分词结果
http://ip:port/indexName/indexType/id/_termvectors?fields=fieldName
例如:
线上环境es6:http://10.3.19.29/sphinx-doctor/_doc/20145/_termvectors?fields=name
测试环境es6:http://10.1.22.23:9200/sphinx-disease/_doc/2849/_termvectors?fields=name
测试环境es2:http://10.1.20.35:9200/sphinx-faculty/faculty/33002000/_termvectors?fields=alias
http://index2.test.search.bj1:9200/forum-community/doc/0/_termvectors?fields=forumCommunityName
http://index1.search.prod.idc1:9200/hospital/disease/255/_termvectors?fields=diThesaurus
http://10.1.22.23:9200/sphinx-doctor-19.11.28-111431/_doc/138239856/_termvectors?fields=grade

验证分词器的实现状

http://127.0.0.1:9200/sphinx-doctorinfo/_analyze?analyzer=comma&text=2,3,4,5,100-100


"diseaseIds": {
    "type": "string",
    "analyzer": "comma",
    "search_analyzer": "comma"
}



"analysis": {
	"analyzer": {
		"index_ansj_syno": {
			"type": "custom",
			"tokenizer": "index_ansj",
			"filter": "synonym"
		},
		"comma": {
			"type": "pattern",
			"pattern": ","
		}
	}
}

新旧索引导数据

POST _reindex

{
  "source": {
    "index": "sphinx-doctor_20190515-19:53:24"   #旧索引
  },
  "dest": {
    "index": "sphinx-doctor_20190614-16:15:07"   #新索引
  }
}

分析底层查询逻辑

GET sphinx-doctor/_validate/query?explain
{
  "query": {
    "bool": {
      "should": {
        "match": {
          "hospitalname": {
            "query": "滨州市"
          }
        }
      }
    }
  }
}

结果:
{
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "valid" : true,
  "explanations" : [
    {
      "index" : "sphinx-doctor-19.07.12-144934",
      "valid" : true,
      "explanation" : "(hospitalname:滨州市 hospitalname:滨州 hospitalname:滨 hospitalname:州市 hospitalname:州 hospitalname:市)"
    }
  ]
}

新建索引别名

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "sphinx-doctor-19.07.12-144934",
        "alias": "sphinx-doctor"
      }
    }
  ]
}

删除索引别名

POST / _aliases {
    "actions": [{
        "remove": {
            "index": "sphinx-doctor-19.07.12-144934",
            "alias": "sphinx-doctor"
        }
    }]
}

重新命名别名

POST /_aliases
{
    "actions": [
        {"remove": {"index": "test1", "alias": "alias1"}},
        {"add": {"index":"test1", "alias": "alias2"}}
    ]
}

查看集群状态

http://192.168.0.75:9200/_cluster/state?pretty

http://192.168.0.75:9200/_cluster/stats?pretty

返回一个正在添加到更新集群状态的列表

http://192.168.0.75:9200/_cluster/tasks?pretty

查看索引的Setting&Mapping

Setting:

http://192.168.0.77:9200/account/_settings?pretty

GET:http://127.0.0.1:9200/sphinx-hospitalfaculty/_settings?preserve_existing=true

{
	"sphinx-hospitalfaculty_20190605-15:41:33": {
		"settings": {
			"index": {
				"refresh_interval": "30s",
				"number_of_shards": "10",
				"translog": {
					"flush_threshold_ops": "100000"
				},
				"similarity": {
					"BM25_title": {
						"type": "BM25",
						"b": "0.2",
						"k1": "0"
					},
					"my_BM25": {
						"type": "BM25",
						"b": "0",
						"k1": "1.0"
					}
				},
				"index": {
					"max_result_window": "10000"
				},
				"creation_date": "1559720492916",
				"analysis": {
					"analyzer": {
						"index_ansj_syno": {
							"filter": "synonym",
							"type": "custom",
							"tokenizer": "index_ansj"
						}
					}
				},
				"number_of_replicas": "1",
				"uuid": "9EnuxeYuQUyQkeYRPawK-Q",
				"version": {
					"created": "2030599"
				}
			}
		}
	}
}

Mapping:

http://192.168.0.171:9200/account/_mappings?pretty  

查看集群健康状态信息

http://192.168.0.75:9200/_cluster/health?pretty

查询某一索引:
http://192.168.0.75:9200/_cluster/health/sphinx-faculty?pretty

查看es磁盘信息

http://192.168.0.75:9200/_cat/allocation?v&pretty

查看索引状态信息

http://192.168.0.75:9200/_stats

查看集群设置

http://192.168.0.75:9200/_all/_settings

查看索引内容

http://192.168.0.75:9200/account/_search

查看es集群副本分配节点

http://192.168.0.75:9200/_cat/shards?v

查看每个索引的健康状况

http://127.0.0.1:9200/_cat/indices?v

查看节点信息

http://127.0.0.1:9200/_cat/nodes?v

查看各个分片上索引的信息

http://127.0.0.1:9200/_cat/shards?v

修改索引的max_result_window

PUT:http://127.0.0.1:9200/sphinx-hospitalfaculty/_settings?preserve_existing=true

{
  "max_result_window": "2000000000"
}

响应:
{
	"acknowledged": true
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值