现在咱们来往customer索引中放一些文档吧。记得先前,为了索引一个文档我们必须告诉Elasticsearch该文档要被索引的类型。
咱们简单的在customer索引中创建一个customer文档,使用“external”类型,ID设置为1. 使用下面的命令:
PUT /customer/external/1?pretty { "name": "John Doe" }
或者使用POST MAN:
得到返回结果:
{ "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true }
从上面的结果我们得知一个新的external类型的customer文档被成功创建在了customer索引中。该文档的内部ID为1,是我们在建立索引的时候指定的。
有重要的一点需要注意的是Elasticsearch并不要求我们再索引一个文档前必须创建一个索引。 在前面的例子,Elasticsearch会自动创建一个customer索引,加入customer索引并不存在。
现在让咱们来读取刚刚索引的文档:
GET /customer/external/1?pretty
我们将得到:
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "found": true, "_source": { "name": "John Doe" } }
需要特殊说明的是返回值中的属性found,告诉我们找到了一个与我们给定的ID匹配的文档。以及_source属性返回了我们之间索引的完整的JSON文档。
本文系本人根据官方文档的翻译,能力有限、水平一般,如果对想学习Elasticsearch的朋友有帮助,将是本人的莫大荣幸。
原文出处:https://www.elastic.co/guide/en/elasticsearch/reference/current/_index_and_query_a_document.html