参考地址 https://www.yiibai.com/elasticsearch/elasticsearch-getting-start.html
集群
1. 查看集群状态
GET http://localhost:9200/_cluster/health?pretty
2. 查看指定索引库的健康状态
GET http://localhost:9200/_cluster/health/index_name?pretty
GET http://localhost:9200/_cluster/health/index_name,index_name2?pretty
3. 查看es集群节点
GET http://localhost:9200/_cat/nodes?v
查看所有索引及其状态
GET http://localhost:9200/_cat/indices?v&pretty
索引操作
1. 创建索引
PUT http://127.0.0.1:9200/<index>
通过如下参数来指定分片数、副本数量
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
2. 查看索引及结构
GET http://127.0.0.1:9200/<index>/_mapping?pretty
2. 修改索引 只能别名实现
3. 删除索引
DELETE http://127.0.0.1:9200/<index>
4. 查看索引别名
GET http://127.0.0.1:9200/_cat/aliases?v&pretty
创建索引别名
POST http://127.0.0.1:9200/_aliases
{
"actions" : [{"add" : {"index" : "xxx" , "alias" : "ss"}}]
}
5. 删除某一索引下全部数据
POST http://127.0.0.1:9200/<index>/_delete_by_query
{
"query": {
"match_all": {}
}
}
6. 查看索引数据
GET http://127.0.0.1:9200/<index>/_search
文档操作
GET http://192.168.43.45:9200/index/type/_search
ignore_unavailable = true
如果URL中存在的一个或多个索引不存在,则不会发生错误或操作不会停止
GET http://localhost:9200/<index>/<type>/<id>
添加mapping
1.使用put方法加字段
my_index/_mapping/_doc _doc 任意 type名称
{
"properties": {
"字段":{
"type": "类型"
}
}
}
2.设置默认值 使用post
my_index/my_type/_update_by_query?refresh&conflicts=proceed
{
"script": {
"lang": "painless",
"inline": "if (ctx._source.字段== null) {ctx._source.字段= 0}"
}
}
curl -i -X POST \
-H "Content-Type:application/json" \
-d \
'{
"script": {
"lang": "painless",
"inline": "if (ctx._source.DoctorType== null) {ctx._source.DoctorType= 0}"
}
}' \
-u elastic:medlinker123456 'http://127.0.0.1:9200/prescription_search_v1/_update_by_query?refresh&conflicts=proceed'
_update_by_query 指导
https://blog.csdn.net/gui66497/article/details/81389037
字段类查询查询 match,term等
字段类查询语法
1. match query
{
"query": {
"match": {
"ID": 9498961
}
}
}
elasticdump操作
curl基础鉴权
curl -u elastic:Medlinker@elastic http://es-cn-oew1t3ioh000k4i6t.public.elasticsearch.aliyuncs.com:9200
elasticdump命令使用
直接迁移会导致迁移后分片数量为主1,副1。
推荐在迁移前,在新集群中建立索引分片,然后进行数据迁移。
参数介绍:
--input 数据源地址,地址后加入索引名
--output 目标地址,地址后加入原有索引名或新索引名
--type data为数据拷贝
mapping拷贝映射(意思是把原始索引的mapping结构迁移给目标索引)
analyzer拷贝analyzer分词
--limit 限制每次移动多少个文件(默认是100)
2.1 备份
./elasticdump --input=http://118.31.236.23:9200/develop_prescription_search_v1 --output=./develop_prescription_search_v1.json
2.2 还原
./elasticdump --input=/opt/esdump/test.json --output=http:/192.168.1.3:9200/test --type=data
2.3 数据迁移
./elasticdump --input=http:/192.168.1.2:9200/test --output=http:/192.168.1.3:9200/test --type=data
2.4 备份并压缩
./elasticdump --input=http://118.31.236.23:9200/develop_prescription_search_v1 --output=$ | gzip > ./prescription_search_v1.gz
elasticdump \
--input=http://192.168.5.101:9200/my_index --httpAuthFile=./auth.ini\
--output=http://192.168.5.202:9200/my_index --httpAuthFile=./auth.ini\
--type=analyzer
elasticdump \
--input=http://192.168.5.101:9200/my_index \
--output=http://192.168.5.202:9200/my_index \
--type=mapping
elasticdump \
--input=http://192.168.5.101:9200/my_index \
--output=http://192.168.5.202:9200/my_index \
--type=data
统计数量:
POST /qa_prescription_search/prescription_type/_count
{
"query": {
"bool": {
"must": [
{
"match": {
"PrTagType": {
"operator": "AND",
"query": 5
}
}
}
]
}
}
}
POST /index/type/_update_by_query?refresh&conflicts=proceed
{
"query": {
"match": {
"DoctorId": 100
}
},
"script": {
"lang": "painless",
"inline": "ctx._source['City']='chengdu';ctx._source['HospitalName']='第二人民医院';ctx._source['Province']='四川省'"
}
}
post index/type/_delete_by_query 删除某字段不为空的数据
{
"query": {
"bool": {
"must": {
"exists": {
"field": "字段名"
}
}
}
}
}
分词测试
GET /_analyze
{
"text": "蓝瘦香菇",
"analyzer": "ik_pinyin_analyzer" //ik_smart ik_max_word
}