1语婴、上傳數(shù)據(jù)
PUT http://localhost:9200/megacorp/employee/1
請(qǐng)求body的內(nèi)容:
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
注:
也可以用以下命令來(lái)上傳數(shù)據(jù),
POST http://localhost:9200/megacorp/employee/
這個(gè)時(shí)候上傳之后的"_ID"就不像上面自定義的為1场航,而是由elasticsearch自動(dòng)生成ID青抛。
PUT方式相當(dāng)于是覆蓋數(shù)據(jù)蜜另,可以用來(lái)做修改操作
如果是批量上傳數(shù)據(jù),可以使用如下命令:
curl -XPOST 127.0.0.1:9200/bank/account/_bulk?pretty --data-binary @accounts.json
其中accounts.json 是一個(gè)json數(shù)據(jù)
2捣辆、查詢數(shù)據(jù)
-
2.1此迅、根據(jù)id查詢
GET http://localhost:9200/megacorp/employee/1
查詢結(jié)果如下:
{"_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
查詢結(jié)果如下:
{"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罢坝、根據(jù)字段條件搜索
查詢姓氏中包含“Smith”的員工- 簡(jiǎn)單搜索
GET http://localhost:9200/megacorp/employee/_search?q=last_name:Smith - DSL(Domain Specific Language特定領(lǐng)域語(yǔ)言)搜索
POST http://localhost:9200/megacorp/employee/_search
請(qǐng)求body如下:
- 簡(jiǎn)單搜索
{
"query" : {
"match" : {
"last_name" : "Smith"
}
},
"_source":["first_name","last_name","age"],
"from":0,
"size":2,
"sort":{"first_name":{"order":"desc"}}
}
其中:
from和size是起到分頁(yè)的作用嘁酿,sort是排序作用
_source代表結(jié)果只顯示這幾個(gè)字段
當(dāng)然還有更復(fù)雜的語(yǔ)法男应,例如增加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
請(qǐng)求body如下:
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
以上查詢代表查詢about字段包含rock或者climbing的數(shù)據(jù)
如果要查詢about字段包含"rock climbing"整個(gè)字符串的數(shù)據(jù),那要這么查:
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
- 2.5众弓、bool搜索
POST http://localhost:9200/megacorp/employee/_search
請(qǐng)求body如下:
{
"query" : {
"bool":{
"must":[
{"match" : {"first_name" : "Jane"}},
{"match" : {"last_name" : "Smith"}}
]
}
}
}
返回的結(jié)果只包含Jane Smith
其中:
1谓娃、must是代表必須同時(shí)滿足蜒滩,相當(dāng)于&&
2奶稠、如果must改成should锌订,代表有一個(gè)滿足就可以画株,相當(dāng)于||,得到的結(jié)果是John Smith和Jane Smith
3蜈项、如果must改成must_not续挟,代表兩個(gè)都不滿足诗祸,得到的結(jié)果是Douglas Fir
-
2.6、聚合搜索
例如查詢每種興趣下職員的平均年齡
POST http://localhost:9200/megacorp/employee/_search
請(qǐng)求body如下:
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
其中博个,terms是分組操作功偿,相當(dāng)于group by脖含。
返回結(jié)果:
{
...,
"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投蝉、刪除數(shù)據(jù)
DELETE http://localhost:9200/megacorp/employee/123
總結(jié)
索引操作的命令如下:
1瘩缆、獲取索引
curl -XGET ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/{id}’
2、索引數(shù)據(jù)
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归露、設(shè)置mapping
curl -XPUT [http://localhost:9200/](http://localhost:9200/){index}/{type}/_mapping -d ‘{
“{type}” : {
“properties” : {
“date” : {
“type” : “l(fā)ong”
},
“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 通過(guò)HTTP RESTful API 操作數(shù)據(jù)
https://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html
使用curl命令操作elasticsearch And 使用http 查詢ES