相关推荐
Elasticsearch 删除索引
现在让我们删除刚才创建的索引,然后再列出所有索引:
API:
DELETE /customer?pretty
GET /_cat/indices?v
复制
curl命令访问API:
curl -X DELETE "localhost:9200/customer?pretty"
curl -X GET "localhost:9200/_cat/indices?v"
复制
Elasticsearch 创建和查询文档
现在把一些内容放入customer
索引中。我们将一个简单的客户文档放到customer
索引中,ID为1,如下所示:
API:
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
复制
curl命令访问API:
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
复制
响应:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
复制
可以看到,customer
索引中成功创建了一个新文档。文档的内部id也是1,在创建文档时指定。
注意,Elasticsearch并不要求,先要有索引,才能将文档编入索引。创建文档时,如果指定索引不存在,将自动创建。
现在检索刚才编入索引的文档:
GET /customer/_doc/1?pretty
复制
curl命令访问API:
curl -X GET "localhost:9200/customer/_doc/1?pretty"
复制
响应:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
复制
这里有一个字段found
为真,表示找到了一个ID为1的文档,另一个字段_source
,该字段返回完整JSON文档。