1、测试es是否启动成功(pretty参数是为了让查询结果更方便阅读)
curl 'http://localhost:9200/?pretty'
2、计算集群中文档的数量
curl -X GET "192.168.226.100:9200/_count" -H 'Content-Type: application/json' -d''
3、创建megacorp索引 employee类型 id为1的数据
索引名必须小写,不能以下划线开头,不能包含逗号。
类型命名可以是大写或者小写,但是不能以下划线或者句号开头,不应该包含逗号, 并且长度限制为256个字符。
如果该id的文档已存在,则更新文档,链接后加/_create表示不存在创建文档,存在不做操作。
链接中不加id系统会自动生成id,使用post请求代替put请求
curl -X PUT "localhost:9200/megacorp/employee/1" -H 'Content-Type: application/json' -d'
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
'
4、查询megacorp索引 employee类型下id为1的数据
curl -X GET "localhost:9200/megacorp/employee/1"
5、查询megacorp索引 employee类型下的所有数据
curl -X GET "localhost:9200/megacorp/employee/_search"
6、使用表达式搜索
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
'
7、查询集群健康状态
curl -X GET "localhost:9200/_cluster/health"
8、创建索引blogs并指定主分片(默认为5个)和副分片(默认为1个)的个数(3个主分片,每个主分片有1份备份,一共6个分片,最大扩容到6个节点,每个分片获取节点的所有资源)
curl -X PUT "localhost:9200/blogs" -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
'
9、修改副本分片的个数(主分片个数无法修改,shard = hash(routing) % number_of_primary_shards,routing默认是文档的id,也可主请求时通过参数指定,根据这个公式确定文档被分片到哪个主分片存储,修改主分片数后就无法找到之前存储的数据了)
curl -X PUT "localhost:9200/blogs/_settings" -H 'Content-Type: application/json' -d'
{
"number_of_replicas" : 2
}
'
10、查看文档是否存在
curl -i -XGET http://localhost:9200/website/blog/124?pretty
看响应体中found字段
11、乐观并发控制
只有当es中id为1的文档的版本号为1才执行更新,否则返回错误信息
curl -X PUT "localhost:9200/website/blog/1?version=1" -H 'Content-Type: application/json' -d'
{
"title": "My first blog entry",
"text": "Starting to get the hang of this..."
}
'
12、查询userindex索引user类型的映射(每个字段的值是什么类型)
curl http://192.168.226.100:9200/userindex/_mapping/user?pretty
参考:Elasticsearch权威指南