欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

elasticsearch简单操作(一)

发布时间:2025/4/16 编程问答 2 豆豆
生活随笔 收集整理的这篇文章主要介绍了 elasticsearch简单操作(一) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、增加记录

例如1:向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录。

curl -X POST 'localhost:9200/accounts/person/2' -d '{"user":"张三","title": "工程师","desc":"系统管理"}'

  系统提示如下错误:

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

  

需要制定插入数据格式:如下

curl -H "Content-Type: application/json" -X POST 'localhost:9200/accounts/person/2' -d '{"user":"张三","title": "工程师","desc":"系统管理"}'

系统提示:

{"_index":"accounts","_type":"person","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

  说明插入成功

 

例如2:

curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts/person/4' -d '{"user":"李四","title": "软件工程师","desc":"系理集成"}'

  系统提示:

{"_index":"accounts","_type":"person","_id":"4","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

说明插入成功。

 

可以使用PUT、POST插入数据,需要制定插入数据的数据格式,用 -H "Content-Type: application/json"制定为json格式。

 

2、数据查询

curl 'localhost:9200/accounts/person/4?pretty'

系统返回:

{"_index" : "accounts","_type" : "person","_id" : "4","_version" : 1,"found" : true,"_source" : {"user" : "李四","title" : "软件工程师","desc" : "系理集成"} }

说明查询成功。如果查询一个不存在id的记录,如命令:

curl 'localhost:9200/accounts/person/5?pretty'

id等于5的person因为没有对应的记录,所以,系统返回:

{"_index" : "accounts","_type" : "person","_id" : "5","found" : false }

 

3、删除记录

删除记录就是发出 DELETE 请求。

 

转载于:https://www.cnblogs.com/dyh004/p/8917121.html

总结

以上是生活随笔为你收集整理的elasticsearch简单操作(一)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。