Elasticsearch基础之:通过Restful API操作
1、上传数据
PUT http://localhost:9200/megacorp/employee/1
请求body的内容:
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
注:
也可以用以下命令来上传数据,
POST http://localhost:9200/megacorp/employee/
这个时候上传之后的"_ID"就不像上面自定义的为1,而是由elasticsearch自动生成ID。
PUT方式相当于是覆盖数据,可以用来做修改操作
如果是批量上传数据,可以使用如下命令:
curl -XPOST 127.0.0.1:9200/bank/account/_bulk?pretty --data-binary @accounts.json
其中accounts.json 是一个json数据
2、查询数据
-
2.1、根据id查询
GET http://localhost:9200/megacorp/employee/1
查询结果如下:
{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"found":true,"_source":{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}}
-
2.2、批量查询
GET http://localhost:9200/megacorp/employee/_search
查询结果如下:
{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"megacorp","_type":"employee","_id":"2","_score":1.0,"_source":{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}},{"_index":"megacorp","_type":"employee","_id":"1","_score":1.0,"_source":{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}},{"_index":"megacorp","_type":"employee","_id":"3","_score":1.0,"_source":{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}}]}}
-
2.3、根据字段条件搜索
查询姓氏中包含“Smith”的员工- 简单搜索
GET http://localhost:9200/megacorp/employee/_search?q=last_name:Smith - DSL(Domain Specific Language特定领域语言)搜索
POST http://localhost:9200/megacorp/employee/_search
请求body如下:
- 简单搜索
{
"query" : {
"match" : {
"last_name" : "Smith"
}
},
"_source":["first_name","last_name","age"],
"from":0,
"size":2,
"sort":{"first_name":{"order":"desc"}}
}
其中:
from和size是起到分页的作用,sort是排序作用
_source代表结果只显示这几个字段
当然还有更复杂的语法,例如增加filter:
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"last_name" : "smith"
}
}
}
}
}
范围操作符包含:
gt : 大于
gte : 大于等于
lt : 小于
lte : 小于等于
-
2.4、多值搜索
POST http://localhost:9200/megacorp/employee/_search
请求body如下:
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
以上查询代表查询about字段包含rock或者climbing的数据
如果要查询about字段包含"rock climbing"整个字符串的数据,那要这么查:
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
- 2.5、bool搜索
POST http://localhost:9200/megacorp/employee/_search
请求body如下:
{
"query" : {
"bool":{
"must":[
{"match" : {"first_name" : "Jane"}},
{"match" : {"last_name" : "Smith"}}
]
}
}
}
返回的结果只包含Jane Smith
其中:
1、must是代表必须同时满足,相当于&&
2、如果must改成should,代表有一个满足就可以,相当于||,得到的结果是John Smith和Jane Smith
3、如果must改成must_not,代表两个都不满足,得到的结果是Douglas Fir
-
2.6、聚合搜索
例如查询每种兴趣下职员的平均年龄
POST http://localhost:9200/megacorp/employee/_search
请求body如下:
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
其中,terms是分组操作,相当于group by。
返回结果:
{
...,
"aggregations": {
"all_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "music",
"doc_count": 2,
"avg_age": {
"value": 28.5
}
},
{
"key": "forestry",
"doc_count": 1,
"avg_age": {
"value": 35
}
},
{
"key": "sports",
"doc_count": 1,
"avg_age": {
"value": 25
}
}
]
}
}
}
3、删除数据
DELETE http://localhost:9200/megacorp/employee/123
总结
索引操作的命令如下:
1、获取索引
curl -XGET ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/{id}’
2、索引数据
curl -XPOST ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/{id}’ -d’{“a”:”avalue”,”b”:”bvalue”}’
3、删除索引
curl -XDELETE ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/{id}’
4、设置mapping
curl -XPUT [http://localhost:9200/](http://localhost:9200/){index}/{type}/_mapping -d ‘{
“{type}” : {
“properties” : {
“date” : {
“type” : “long”
},
“name” : {
“type” : “string”,
“index” : “not_analyzed”
},
“status” : {
“type” : “integer”
},
“type” : {
“type” : “integer”
}
}
}
}’
5、获取mapping
curl -XGET [http://localhost:9200/](http://localhost:9200/){index}/{type}/_mapping
6、搜索
curl -XGET ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/_search’ -d ‘{
“query” : {
“term” : { “user” : “kimchy” } //查所有 “match_all”: {}
},
“sort” : [{ “age” : {“order” : “asc”}},{ “name” : “desc” } ],
“from”:0,
“size”:100
}
curl -XGET ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/_search’ -d ‘{
“filter”: {“and”:{“filters”:[{“term”:{“age”:”123”}},{“term”:{“name”:”张三”}}]},
“sort” : [{ “age” : {“order” : “asc”}},{ “name” : “desc” } ],
“from”:0,
“size”:100
}
参考
elasticsearch 通过HTTP RESTful API 操作数据
https://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html
使用curl命令操作elasticsearch And 使用http 查询ES