🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Index and Query a Document(索引和查询文档) 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/_index_and_query_a_document.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/_index_and_query_a_document.html) 译文链接 : [http://www.apache.wiki/pages/viewpage.action?pageId=4260701](http://www.apache.wiki/pages/viewpage.action?pageId=4260701) 贡献者 : [那伊抹微笑](/display/~wangyangting),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 现在让我们放入一些东西动我们的 **customer** 索引中去。回想一下前面的内容,为了索引一个文档,我们必须告诉 **Elasticsearch** 在索引中应该使用哪种类型。 让我们索引一个简单的 **customer** 文档到 **customer** 索引中,“**external**” 类型,与一个为 **1** 的 **ID**,如下所示 :  ``` curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -d' { "name": "John Doe" }' ``` 响应如下 :  ``` { "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true } ``` 从上面我们可以看到一个新的 **customer** 文档被成功的创建到 **customer** 索引和 **external** 类型中。该文档还有一个为 **1** 的内部 **ID**,它是我们在索引时指定的。 需要注意的是,在您可以索引文档到 **Elasticsearch** 之前时,它不需要您首先就明确的创建一个索引。在前面的例子中,**Elasticsearch** 将自动的创建 **customer** 索引(如果它事先不存在)。 现在让我们检索我们刚刚索引的文档 :  ``` curl -XGET 'localhost:9200/customer/external/1?pretty&pretty' ``` 响应如下 :  ``` { "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "name": "John Doe" } } ``` 除了一个字段 **found** 之外,没有什么特别的东西,它指出了我们使用 **ID** 为 **1** 请求的状态,另一个字段,**_source**,它返回了我们先前步骤中索引的全部的 **JSON** 文档。