简单操作elasticsearch(es版本7.6)
简单操作elasticsearch主要是指创建索引,对数据进行增删改查的操作。通常情况下我们使用es head进行这些操作,也可以通过postman或者是其它的http请求工具进行操作。
1、索引管理
a、创建索引(索引名称必须小写!!!)
请求地址:http://localhost:9200/{IndexName}
http方法:PUT
参数示例:
{
"settings":{
"number_of_shards":2,
"number_of_replicas":1
},
"mappings":{
"properties":{
"id":{"type":"keyword"},
"country":{"type":"keyword"},
"province":{"type":"keyword"},
"city":{"type":"keyword"},
"address1":{"type":"text"},
"remark":{"type":"text"}
}
}
}
其中settings是设置索引分片信息:分片数,副本数。
head示例::
也可以使用Postman或者其他http请求工具实现:
postman实现示例
b、查看索引
请求地址:http://localhost:9200/IndexName/_mappings
http方法:GET
示例:
c、查看索引的配置(settings)
请求地址:http://localhost:9200/{IndexName}/_settings
http方法:GET
2、添加数据
注意:默认情况下添加数据时,如果指定的某个字段不存在时,es会自动创建该字段
a、指定id,不存在则创建,存在则更新
路径:http://localhost:9200/{IndexName}/_doc/{id}
http方法:PUT
参数示例:
{
"country": "CN",
"province": "广东",
"City": "广州",
"address1": "天河区猎德村123"
}
完整示例截图:
b、不指定id,直接创建,id由es自动生成
路径:http://localhost:9200/IndexName/_doc
http方法:POST
{
“country”:"CN",
"province":"广东",
“City”:“广州”,
"address1":"白云区xxx"
}
完整示例截图
3、查询数据
路径:http://localhost:9200/{IndexName}/_search
方法:POST
参数示例
{
"query": {
"bool": {
"must": [
{
"term": {
"province": "广东"
}
}
]
}
}
}
完整示例截图
4、修改数据
路径:http://localhost:9200/{IndexName}/_update_by_query
方法:POST
参数示例
{
"script": {
"source": "ctx._source.province='广东省';ctx._source.address1=''"
},
"query": {
"bool": {
"must": [
{
"term": {
"province": "广东"
}
}
]
}
}
}
完整请求示例
5、删除数据
路径:http://localhost:9200/{IndexName}/_delete_by_query
方法:POST
参数示例、
{
"query": {
"bool": {
"must": [
{
"term": {
"province": "广东省"
}
}
]
}
}
}
完整请求示例
----------------------------
标签:http,9200,示例,elasticsearch,localhost,7.6,IndexName,es
来源: /johnyong/p/12873386.html