(八)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文檔。
相關文章
相關標籤/搜索