码迷,mamicode.com
首页 > 其他好文 > 详细

elasticsearch基础用法

时间:2016-11-23 19:41:11      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:dice   需要   accounts   索引   url   pre   2.3   arc   node   

来自官网,版本为2.3

不能用root启动,新加一个用户,然后测试启动;

adduser elsearch
su elsearch
./elasticsearch

若没有错误表示成功;

正常使用肯定需要后台启动(可选指定集群名和节点名);

./elasticsearch -d
./elasticsearch --cluster.name my_cluster_name --node.name my_node_name -d

下面三个命令依次为,检查集群状况、检查节点状况、显示所有的索引(刚开始是没有索引的)

curl ‘localhost:9200/_cat/health?v‘
curl ‘localhost:9200/_cat/nodes?v‘
curl ‘localhost:9200/_cat/indices?v‘

创建一个索引 customer(可选,因为直接插入数据的时候若索引名不存在会自动创建);

在索引customer中插入一条 type=external, id=1 的数据并查询这条数据;

删除这条数据,最后删除索引customer;

curl -XPUT ‘localhost:9200/customer?pretty‘
curl -XPUT ‘localhost:9200/customer/external/1?pretty‘ -d ‘{"name": "John Doe"}‘
curl -XGET ‘localhost:9200/customer/external/1?pretty‘
curl -XDELETE ‘localhost:9200/customer/external/2?pretty‘
curl -XDELETE ‘localhost:9200/customer?pretty‘

可以重复执行插入数据的命令来替换整条数据;

插入数据时可以不指定id,这样会自动生成一个(注意这里是post);

curl -XPOST ‘localhost:9200/customer/external?pretty‘ -d ‘{"name": "Jane Doe"}‘

而修改某条数据则应该这样;

curl -XPOST ‘localhost:9200/customer/external/1/_update?pretty‘ -d ‘{"doc": { "name": "John Doe update" }}‘
curl -XPOST ‘localhost:9200/customer/external/1/_update?pretty‘ -d ‘{"doc": { "name": "John Doe update", "age": 20 }}‘

修改数据还可以用脚本script,需要另外开篇记录;

下面是批量插入和更新数据;

curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty‘ -d ‘
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
‘
curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty‘ -d ‘
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
‘

导入文件中的数据,当前目录下有个accounts.json文件(要求格式为上述批量操作的格式,感觉不实用);

curl -XPOST ‘localhost:9200/bank/account/_bulk?pretty‘ --data-binary "@accounts.json"

两种查出所有数据的命令;

curl ‘localhost:9200/bank/_search?q=*&pretty‘
curl -XPOST ‘localhost:9200/bank/_search?pretty‘ -d ‘
{
  "query": { "match_all": {} }
}‘

 

elasticsearch基础用法

标签:dice   需要   accounts   索引   url   pre   2.3   arc   node   

原文地址:http://www.cnblogs.com/toSeek/p/6094808.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!