1. 程式人生 > >Elasticsearch學習筆記——安裝和數據導入

Elasticsearch學習筆記——安裝和數據導入

itl ati shard eat ada 分享 must 數據 分享圖片

到elasticsearch網站下載最新版本的elasticsearch 6.2.1

https://www.elastic.co/downloads/elasticsearch

下載tar包,然後解壓到/usr/local目錄下,修改一下用戶和組之後可以使用非root用戶啟動,啟動命令

./bin/elasticsearch

然後訪問http://127.0.0.1:9200/

技術分享圖片

接下來導入json格式的數據,數據內容如下

{"index":{"_id":"1"}}
{"title":"許寶江","url":"7254863","chineseName":"許寶江","sex":"男","occupation":" 灤縣農業局局長","nationality":"中國"}
{"index":{"_id":"2"}}
{"title":"鮑誌成","url":"2074015","chineseName":"鮑誌成","occupation":"醫師","nationality":"中國","birthDate":"1901年","deathDate":"1973年","graduatedFrom":"香港大學"}

需要註意的是{"index":{"_id":"1"}}和文件末尾另起一行換行是不可少的

否則會出現400狀態,錯誤提示分別為

Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]
The bulk request must be terminated by a newline [\n]"

使用下面命令來導入json文件

其中的people.json為文件的路徑,可以是/home/common/下載/xxx.json

其中的es是index,people是type,在elasticsearch中的index和type可以理解成關系數據庫中的database和table,兩者都是必不可少的

curl -H "Content-Type: application/json" -XPOST ‘localhost:9200/es/people/_bulk?pretty&refresh‘ --data-binary "@people.json"

成功後的返回值是200,比如

{
  "took" : 233,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "es",
        "_type" : "people",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "forced_refresh" : true,
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "es",
        "_type" : "people",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "forced_refresh" : true,
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}

接下來可以使用對應的查詢語句對數據進行查詢

Elasticsearch學習筆記——安裝和數據導入