zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【转】ElasticSearch快速使用篇(基本命令篇)

2023-03-15 22:01:58 时间

1. 创建index(database)#

curl -X PUT http://10.210.40.59:9200/manage?pretty

--服务器ip端口号就不说了 --manage 我们需要创建一个基于项目的数据库 --pretty 让Elasticsearch美化输出(pretty-print)JSON响应以便更加容易阅读

查看创建好的索引(database)信息#

curl -X GET http://10.210.40.59:9200/manage?pretty

删除索引(database)#

curl -X DELETE http://10.210.40.59:9200/manage?pretty

2. 创建type(table)#

在数据库中创建user用户表,当然表字段属性设置除了type还有很多,这里只作简单快速使用的示例

curl -X PUT 10.210.40.59:9200/manage/_mapping/user?pretty -H 'Content-Type: application/json' -d ' { "properties": { "user_id": { "type": "long" }, "user_name": { "type": "text" }, "user_phone": { "type": "keyword" } } } '

有必要提一下表字段的支持数据类型:

字符串类型:string(已过期)(5.x后改成了text类型 添加了keyword类型, 至于区别百度一下你就知道) 整数 : byte,short,integer,long 浮点数:float,double 布尔型: boolean 日期: date

查看创建好的映射信息(表字段详情)#

curl -X GET http://10.210.40.59:9200/manage/user/_mapping?pretty

添加映射(添加表字段)#

curl -X PUT 10.210.40.59:9200/manage/_mapping/user?pretty -d '{"properties":{"user_addr":{"type":"text"}}}'

3. 添加document(插入数据)#

为了方便增加数据就不用linux命令了,有点麻烦,通过postman增加 URL: POST

# 指定id增加 http://10.210.40.59:9200/manage/user/1?pretty # 不指定id,es自动生成 http://10.210.40.59:9200/manage/user/?pretty

json参数串

{ "user_id":"10", "user_name":"Daniel", "user_phone":"13678909876", "user_addr":"北京" }

4. 删除document(删除数据)#

URL: DELETE、POST

# 指定id删除 DELETE http://10.210.40.59:9200/manage/user/10 # 查询式删除 POST http://10.210.40.59:9200/manage/user/_delete_by_query?pretty # json参数串 { "query": { "bool": { "filter": { "terms": { "_id": ["1","AXGGuNaHdgsAZVXGg9_C"] } } } } }

想了解更多删除可以参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html

5. 修改document(修改数据)#

doc文档格式修改#

# POST http://10.210.40.59:9200/manage/user/AXGGv5VOdgsAZVXGg-It/_update?pretty # json { "doc": { "user_name": "Claire", "user_phone": "13898765435" } }

脚本格式修改#

# POST http://10.210.40.59:9200/manage/user/AXGGv5VOdgsAZVXGg-It/_update?pretty # json { "script": "ctx._source.user_addr = '成都'" }

批量添加

http://127.0.0.1:9200/_bulk
{ "index": { "_index": "test", "_type": "user"} }
{"name": "song40","age":33}
{ "index": { "_index": "test", "_type": "user"} }
{"name": "song40","age":33}
{ "index": { "_index": "test", "_type": "user"} }
{"name": "song40","age":33}
{ "index": { "_index": "test", "_type": "user"} }
{"name": "song40","age":33}