一、文档的增删改查
-
索引单个文档
curl -XPUT "http://192.168.2.102:9200/school/student/1?pretty" -d' { "name":"student1", "address":"厦门湖里区", "age":18, "favorite":["读书","打篮球"], "grade":2, "remark":"努力、勤奋!" }' curl -XPUT "http://192.168.2.102:9200/school/student/2?pretty" -d' { "name":"student2", "address":"厦门集美区", "age":19, "favorite":["下棋","打篮球"], "grade":3, "remark":"喜欢思考,喜欢动手!" }' curl -XPUT "http://192.168.2.102:9200/school/student/3?pretty" -d' { "name":"student3", "address":"厦门同安区", "age":19, "favorite":["下棋","踢足球","玩游戏"], "grade":2, "remark":"富有创造力!" }'
-
更新文档
通过doc进行更新: curl -XPOST "http://192.168.2.102:9200/school/student/1/_update?pretty" -d' { "doc":{ "age":19 } }' 通过script进行更新: curl -XPOST "http://192.168.2.102:9200/school/student/1/_update?pretty" -d' { "script" : "ctx._source.age += 5" }'
-
删除文档
curl -XDELETE "http://192.168.2.102:9200/school/student/1?pretty"
-
查询文档
curl -XGET "http://192.168.2.102:9200/school/student/1?pretty"
-
批量查询
curl -XGET "http://192.168.2.102:9200/school/student/_mget?pretty" -d' { "ids": ["1","2","3","4"] }' curl -XGET "http://192.168.2.102:9200/school/student/_mget?pretty" -d' { "docs":[{ "_id":"1" },{ "_id":"2" }] }'
-
批量操作(bulk操作必需严格遵循一行一行的格式)
curl -XPOST "http://192.168.2.102:9200/school/student/_bulk?pretty" -d'
{"index":{"_id":"4"}}
{"name":"student4","address":"厦门翔安区","age":19,"favorite":["玩游戏"],"grade":2,"remark":"不喜欢看书!"}
{"update":{"_id":"1"}}
{"doc":{"age":30}}
{"delete":{"_id":"2"}}
'
二、搜索
-
URI搜索
#不带字段的搜索,默认查询_all字段 curl -XGET "http://192.168.2.102:9200/school/_search?q=student1&pretty" #可以指定要查询的字段 curl -XPOST "http://192.168.2.102:9200/school/_search?q=age:30&pretty"
-
带RequestBody的搜索
curl -XPOST "http://192.168.2.102:9200/school/student/_search?pretty" -d' { "query":{"match_all": {}}, "size": 20, "sort": [ { "age": { "order": "desc" } } ], "_source":["name","address"] }' curl -XPOST "http://192.168.2.102:9200/school/student/_search?pretty" -d' { "query": { "match_phrase": { "address": "门翔" } } }' curl -XPOST "http://192.168.2.102:9200/school/student/_search?pretty" -d' { "query": { "match_phrase": { "address": "厦门" } }, "sort": [ { "age": { "order": "desc" } } ] }'
-
其他搜索相关
#查询涉及到的分片,在routing的时候可以有效的了解路由到哪个分片 curl -XPOST "http://192.168.2.102:9200/school/student/_search_shards" #exists的搜索 POST /school/student/_search/exists?q=age:19 #validate检验查询语句是否正确 POST /school/studen/_validate/query?rewrite=true { "query":{ "term": { "age":19 } } } POST /school/studen/_validate/query?explain { "query":{ "term": { "age":"abc" } } } #explain POST /school/student/1/_explain?q=age:19
三、聚合查询
-
指标性的聚合查询
#查询平均值 curl -XPOST "http://192.168.2.102:9200/school/student/_search" -d' { "aggs":{ "avg_age":{ "avg":{ "field":"age" } } }, "size": 0 }' #统计去重后的字段个数,类似于mysql的distinct curl -XPOST "http://192.168.2.102:9200/school/student/_search" -d' { "aggs" : { "age_count" : { "cardinality" : { "field" : "age" } } }, "size": 0 }' #扩展的状态,计算出对应字段的最大值/最小值/和等 curl -XPOST "http://192.168.2.102:9200/school/student/_search" -d' { "aggs":{ "age_extend_stats":{ "extended_stats":{ "field":"age" } } }, "size":0 }' #统计最大值 curl -XPOST "http://192.168.2.102:9200/school/student/_search" -d' { "aggs":{ "max_age":{ "max":{ "field":"age" } } }, "size": 0 }' #统计最小值 curl -XPOST "http://192.168.2.102:9200/school/student/_search" -d' { "aggs":{ "min_age":{ "min":{ "script":"doc[\"age\"].value" } } }, "size": 0 }'
四、索引相关API
- 创建索引
#创建索引,并指定分片和副本的数量 curl -XPUT 'http://localhost:9200/twitter/' -d '{ "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } }' #创建的时候同时指定分片和副本数量以及mapping信息 curl -XPOST localhost:9200/test -d '{ "settings" : { "number_of_shards" : 1 }, "mappings" : { "type1" : { "properties" : { "field1" : { "type" : "string", "index" : "not_analyzed" } } } } }' #删除索引 curl -XDELETE 'http://localhost:9200/twitter/' #查看索引信息,可以指定需要查看的信息也可以查看整个信息,包括mapping、settting等 curl -XGET 'http://localhost:9200/twitter' curl -XGET 'http://localhost:9200/twitter/_settings,_mappings' curl -XGET 'http://localhost:9200/twitter/_mapping/tweet' #查看某个字段的mapping curl -XGET 'http://localhost:9200/twitter/_mapping/tweet/field/text' #检测索引、类型是否存在 curl -XHEAD -i 'http://localhost:9200/twitter' curl -XHEAD -i 'http://localhost:9200/twitter/tweet' #关闭和打开索引:关闭在集群上基本上没有任何开销,但是读写会进入block状态 curl -XPOST 'localhost:9200/my_index/_close' curl -XPOST 'localhost:9200/my_index/_open' #创建mapping,并动态添加属性字段 curl -XPUT localhost:9200/test/type/_mapping -d '{ "properties" : { "field1" : { "type" : "string", "index" : "not_analyzed" } } }'
-