(八)Index and Query a Document

Let’s now put something into our customer index. We’ll index a simple customer document into the customer index, with an ID of 1 as follows:json

如今让咱们在客户索引中加入一些内容。咱们将一个简单的客户文档索引到客户索引中,ID为1,以下所示:

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'app

 And the response:
{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

From the above, we can see that a new customer document was successfully created inside the customer index. The document also has an internal id of 1 which we specified at index time.curl

从上面能够看出,在客户索引中成功建立了一个新的客户文档。该文档的内部标识为1,咱们在索引时指定。
 
It is important to note that Elasticsearch does not require you to explicitly create an index first before you can index documents into it. In the previous example, Elasticsearch will automatically create the customer index if it didn’t already exist beforehand.
值得注意的是,Elasticsearch在您将文档编入索引以前不须要先显式建立索引。在前面的示例中,若是客户索引事先还没有存在,则Elasticsearch将自动建立客户索引。
 
Let’s now retrieve that document that we just indexed:
咱们如今检索刚刚编入索引的文档:
curl -X GET "localhost:9200/customer/_doc/1?pretty"

And the response:ide

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : { "name": "John Doe" }
}

Nothing out of the ordinary here other than a field, found, stating that we found a document with the requested ID 1 and another field, _source, which returns the full JSON document that we indexed from the previous step.ui

除了字段以外,没有任何异常,发现,说明咱们找到了一个具备请求ID 1的文档和另外一个字段_source,它返回咱们从上一步索引的完整JSON文档。
相关文章
相关标签/搜索