1.添加索引
添加索引person氢伟,類型為student高职,主鍵為1
curl -X PUT -H "Content-Type: application/json" -d
'{"id":1,"name":"yyc1","age":27,"interets":["game","basketball"]}'
'http://localhost:9200/person/student/1' -i
當(dāng)主鍵ID已存在時(shí)Elasticsearch執(zhí)行的操作是將原來的文檔置為失效,新增一個(gè)文檔溺拱,文檔版本號+1,如果要實(shí)現(xiàn)ID存在不新增的邏輯谣辞,url如下:
http://localhost:9200/person/student/1?op_type=create
or
http://localhost:9200/person/student/1/_create
2.獲取索引數(shù)據(jù)
獲取索引person迫摔,類型student,主鍵為1的文檔數(shù)據(jù)
curl "http://localhost:9200/person/student/1"
{
"_index":"person",
"_type":"student",
"_id":"1",
"_version":1,
"found":true,
"_source":{
"id":1,
"name":"yyc1",
"age":27,
"interests":[
"game",
"basketball"
]
}
}
3.修改索引
修改ID=1的文檔數(shù)據(jù)泥从,再次使用PUT命令即可句占,字段存在更新字段,否則新增字段躯嫉。Elasticsearch內(nèi)部實(shí)際上是將原來的文檔置為失效纱烘,合并原有文檔和傳入的文本杨拐,新增一個(gè)全新的文檔,版本號+1
curl -X PUT -H "Content-Type: application/json" -d '{"id":1,"name":"yyc1","age":27,"interets":["game","basketball","movies"],"desc":"modify index"}' 'http://localhost:9200/person/student/1' -i
修改后結(jié)果
"_index":"person",
"_type":"student",
"_id":"1",
"_version":2,
"found":true,
"_source":{
"id":1,
"name":"yyc1",
"age":27,
"intreets":[
"game",
"basketball",
"movies"
],
"desc":"modify index"
}
}
4.刪除索引
刪除ID=1的文檔數(shù)據(jù)擂啥,Elasticsearch不會(huì)立即刪除這些文檔哄陶,先置為失效狀態(tài),后臺(tái)慢慢處理要?jiǎng)h除的文檔哺壶。
curl -X DELETE "http://localhost:9200/person/student/1" -i
5.Lucene語法索引數(shù)據(jù)
索引name=yyc3并且age=18的數(shù)據(jù)
curl -X GET 'http://localhost:9200/person/student/_search?q=name:"yyc3" AND age:18'
hits數(shù)組是查詢結(jié)果屋吨,total是匹配的總數(shù),默認(rèn)返回10條數(shù)據(jù)
{
"took":3,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":1,
"max_score":1.287682,
"hits":[
{
"_index":"person",
"_type":"student",
"_id":"3",
"_score":1.287682,
"_source":{
"id":3,
"name":"yyc3",
"age":18,
"intreests":[
"game",
"basketball",
"java"
],
"desc":"java developer"
}
}
]
}
}
6.判斷文檔是否存在
HTTP狀態(tài)碼200-存在山宾,404-不存在
curl -XHEAD "http://localhost:9200/person/student/1"
7.批量獲取文檔數(shù)據(jù)
必選:索引_index至扰,類型_type,主鍵_id
可選:_source指定返回字段资锰,默認(rèn)所有
未被索引到的found=fasle
curl "http://localhost:9200/_mget"
{
"docs" : [
{
"_index":"xxx",
"_type":"xxx",
"_id":xxx,
"_source":["xxx","xxx",...]
},
{
"_index" : "xxx",
"_type" : "xxx",
"_id" : xxx,
"_source":["xxx","xxx",...]
},
...
]
}
相同索引/類型
curl "http://localhost:9200/profilecenter/profile/_mget"
{
"ids":[1,2,3...]
}
返回結(jié)構(gòu)
{
"docs":[
{
"_index":"xxx",
"_type":"xxx",
"_id":xxx,
"_version":xxx,
"found":true/false,
"_source":{xxx}
},
{
"_index":"xxx",
"_type":"xxx",
"_id":xxx,
"_version":xxx,
"found":true/false,
"_source":{xxx}
}
]
}