Elasticsearch填充


在本節中,我們將向Elasticsearch新增一些索引,對映和資料。此資料將用於本教學中解釋的範例中。

建立索引

POST http://localhost:9200/schools

請求正文
它可以包含索引特定的設定,但是現在,它的預設設定為空。

響應

{"acknowledged": true}

這意味著建立索引成功

建立對映和新增資料

Elasticsearch將根據請求體中提供的資料自動建立對映,我們將使用其批次功能在此索引中新增多個JSON物件。

POST http://localhost:9200/schools/_bulk

請求體

{
   "index":{
      "_index":"schools", "_type":"school", "_id":"1"
   }
}
{
   "name":"Central School", "description":"CBSE Affiliation", "street":"Nagan",
   "city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
   "fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
}
{
   "index":{
      "_index":"schools", "_type":"school", "_id":"2"
   }
}
{
   "name":"Saint Paul School", "description":"ICSE 
   Afiliation", "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075",
   "location":[28.5733056, 77.0122136], "fees":5000,
   "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
}
{
   "index":{"_index":"schools", "_type":"school", "_id":"3"}
}
{
   "name":"Crescent School", "description":"State Board Affiliation", "street":"Tonk Road", 
   "city":"Jaipur", "state":"RJ", "zip":"176114","location":[26.8535922, 75.7923988],
   "fees":2500, "tags":["Well equipped labs"], "rating":"4.5"
}

響應結果 -

{
   "took":328, "errors":false,"items":[
      {
         "index":{
            "_index":"schools", "_type":"school", "_id":"1", "_version":1, "_shards":{
               "total":2, "successful":1, "failed":0
            }, "status":201
         }
      },

      {
         "index":{
            "_index":"schools", "_type":"school", "_id":"2", "_version":1, "_shards":{
               "total":2, "successful":1, "failed":0
            }, "status":201
         }
      },

      {
         "index":{
            "_index":"schools", "_type":"school", "_id":"3", "_version":1, "_shards":{
               "total":2, "successful":1, "failed":0
            }, "status":201
         }
      }
   ]
}

新增另一個索引

建立索引

POST http://localhost:9200/schools_gov

請求正文

它可以包含索引特定的設定,但現在它的預設設定為空。

響應

{"acknowledged": true} (This means index is created)

建立對映和新增資料

POST http://localhost:9200/schools_gov/_bulk

請求正文

{
   "index":{
      "_index":"schools_gov", "_type":"school", "_id":"1"
   }
}
{
   "name":"Model School", "description":"CBSE Affiliation", "street":"silk city",
   "city":"Hyderabad", "state":"AP", "zip":"500030", "location":[17.3903703, 78.4752129],
   "fees":200, "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
}
{
   "index":{
      "_index":"schools_gov", "_type":"school", "_id":"2"
   }
}
{
   "name":"Government School", "description":"State Board Affiliation",
   "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
   "location": [18.599752, 73.6821995], "fees":500, "tags":["Great Sports"], "rating":"4"
}

響應

{
   "took":179, "errors":false, "items":[
      {
        "index":{
           "_index":"schools_gov", "_type":"school", "_id":"1", "_version":1, "_shards":{
              "total":2, "successful":1, "failed":0
            }, "status":201
         }
      },

      {
         "index":{
            "_index":"schools_gov", "_type":"school", "_id":"2", "_version":1, "_shards":{
               "total":2, "successful":1, "failed":0
            }, "status":201
         }
      }
   ]
}