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
当主键ID已存在时Elasticsearch执行的操作是将原来的文档置为失效,新增一个文档,文档版本号+1,如果要实现ID存在不新增的逻辑,url如下:
http://localhost:9200/person/student/1?op_type=create
or
http://localhost:9200/person/student/1/_create
2.获取索引数据
获取索引person,类型student,主键为1的文档数据
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的文档数据,再次使用PUT命令即可,字段存在更新字段,否则新增字段。Elasticsearch内部实际上是将原来的文档置为失效,合并原有文档和传入的文本,新增一个全新的文档,版本号+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
修改后结果
"_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的文档数据,Elasticsearch不会立即删除这些文档,先置为失效状态,后台慢慢处理要删除的文档。
curl -X DELETE "http://localhost:9200/person/student/1" -i
5.Lucene语法索引数据
索引name=yyc3并且age=18的数据
curl -X GET 'http://localhost:9200/person/student/_search?q=name:"yyc3" AND age:18'
hits数组是查询结果,total是匹配的总数,默认返回10条数据
{
"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状态码200-存在,404-不存在
curl -XHEAD "http://localhost:9200/person/student/1"
7.批量获取文档数据
必选:索引_index,类型_type,主键_id
可选:_source指定返回字段,默认所有
未被索引到的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...]
}
返回结构
{
"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}
}
]
}
网友评论