elasticsearch–http接口数据操作
一、简介
这里介绍通过http请求,对es进行索引和数据操作,分别包含了增删改查。
二、操作
这里直接上代码,解释请参考注释。
#!/usr/bin/env bash
host="127.0.0.1"
port=9200
index="stu"
type="doc"
#################### 索引元数据操作 ###############################
#查看所有索引
curl -s -XGET "${host}:${port}/_cat/indices"
#创建索引
curl -s -XPUT "${host}:${port}/${index}" -d '
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
},
"mappings":{
"doc" : {
"properties" : {
"id" : {
"type" : "long"
},
"stuId" : {
"type" : "string",
"index": "not_analyzed"
},
"stuName" : {
"type" : "string",
"index": "not_analyzed"
}
}
}
}
}
'
#查看指定索引所有元数据
curl -s -XGET "${host}:${port}/${index}?pretty"
##查看索引mapping(类似于表字段设置)
curl -s -XGET "${host}:${port}/${index}/_mapping?pretty"
#
#查看settings
curl -s -XGET "${host}:${port}/${index}/_settings?pretty"
#删除索引
curl -s -XDELETE "${host}:${port}/${index}"
#################################################################
########################## 索引数据操作 #########################
#查看指定索引数据
curl -s -XGET "${host}:${port}/${index}/_search?pretty"
##查看指定索引数据,并指定查询起点及个数
curl -s -XGET "${host}:${port}/${index}/_search?pretty&from=0&size=200"
##直接在url地址后添加查询参数
curl -s -XGET "${host}:${port}/${index}/_search?pretty&q=stuName:apple7"
#根据指定索引指定id查看数据
doc_id="21"
curl -s -XGET "${host}:${port}/${index}/${type}/${doc_id}?pretty"
#根据指定json条件查询(方式一),query_json_file为json格式的查询条件,内容如下一项查询条件类似
query_json_file="stu_query.json"
curl -H 'content-type:application/json' -s -XGET "${host}:${port}/${index}/_search?pretty" -d @${query_json_file}
#根据指定json条件查询(方式二),直接指定
curl -H 'content-type:application/json' -s -XGET "${host}:${port}/${index}/_search?pretty" -d '
{
"query":{
"match":{
"stuName":"apple21"
}
}
}
'
#添加或更新数据(方式一,json文件,内容如下一项类似)
doc_id="21"
stu_obj_file="/home/china/shell_space/my_study/stu_obj.json"
curl -H 'content-type:application/json' -s -XPUT "${host}:${port}/${index}/${type}/${doc_id}" -d @${stu_obj_file}
#添加或更新数据(方式二,直接指定json)
curl -H 'content-type:application/json' -s -XPUT "${host}:${port}/${index}/${type}/${doc_id}" -d '
{
"id" : 21,
"stuId" : "021",
"stuName" : "apple21"
}
'
#更新数据
doc_id="21"
curl -s -XPOST "${host}:${port}/${index}/${type}/${doc_id}/_update" -d '
{
"doc":{
"stuName":"applwwe"
}
}
'
#删除数据
doc_id="21"
curl -s -XDELETE "${host}:${port}/${index}/${type}/${doc_id}"
#######################################################################