1. 程式人生 > >【原創】elasticsearch入門命令

【原創】elasticsearch入門命令

安裝

命令

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.tar.gz  
tar -xvzf elasticsearch-6.4.0.tar.gz  
cd elasticsearch-6.4.0/bin
./elasticsearch -d

修改配置檔案

# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.141.129
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#

再次啟動報錯:

[2018-09-13T09:29:43,060][INFO ][o.e.b.BootstrapChecks    ] [7hyiUY2] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [2] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解決方案:

vi /etc/security/limits.conf # 新增兩行行配置,並重連SSH
elasticsearch soft nofile 65536
elasticsearch hard nofile 65537

vi /etc/sysctl.conf # 新增一行配置
vm.max_map_count=262144
sysctl -p

頁面訪問

基礎概念

叢集(Cluster)

一個叢集是由一個或多個節點(伺服器)組成的,通過所有的節點一起儲存你的全部資料並且提供聯合索引和搜尋功能的節點集合。每個叢集有一個唯一的名稱標識,預設是“elasticsearch”。這個名稱非常重要,因為一個節點(Node)只有設定了這個名稱才能加入叢集,成為叢集的一部分。

節點(Node)

一個節點是一個單一的伺服器,是你的叢集的一部分,儲存資料,並且參與叢集的索引和搜尋功能。跟叢集一樣,節點在啟動時也會被分配一個唯一的標識名稱,這個名稱預設是一個隨機的UUID(Universally Unique IDentifier)。如果你不想用預設的名稱,你可以自己定義節點的名稱。這個名稱對於管理叢集節點,識別哪臺伺服器對應叢集中的哪個節點有重要的作用。

索引(Index)

一個索引就是含有某些相似特性的文件的集合。

型別(Type)[Deprecated]

一個型別是你的索引中的一個分類或者說是一個分割槽,它可以讓你在同一索引中儲存不同型別的文件,例如,為使用者建一個型別,為部落格文章建另一個型別。現在已不可能在同一個索引中建立多個型別,並且整個型別的概念將會在未來的版本中移除。 比如天氣weather這個 Index 索引裡面,可以按城市分組(北京和上海),也可以按氣候分組(晴天和雨天)。這種分組就叫做 Type,它是虛擬的邏輯分組,用來過濾 Document。

文件(Document)

一個文件是一個可被索引的資料的基礎單元。例如,你可以給一個單獨的使用者建立一個文件,給單個產品建立一個文件,以及其他的單獨的規則。這個文件用JSON格式表現。

在一個索引或型別中,你可以根據自己的需求儲存任意多的文件。注意,雖然一個文件在物理儲存上屬於一個索引,但是文件實際上必須指定一個在索引中的型別。

分片(Shard)

前面已經提到,叢集能夠儲存超出單機容量的資訊。為了實現這種需求,ElasticSearch把資料分發到多個儲存Lucene索引的物理機上。這些Lucene索引稱為分片索引,這個分發的過程稱為索引分片(Sharding)。在ElasticSearch叢集中,索引分片(Sharding)是自動完成的,而且所有分片索引(Shard)是作為一個整體呈現給使用者的。需要注意的是,儘管索引分片這個過程是自動的,但是在應用中需要事先調整好引數。因為叢集中分片的數量需要在索引建立前配置好,而且伺服器啟動後是無法修改的,至少目前無法修改。

副本(Replica)

通過索引分片機制(Sharding)可以向ElasticSearch叢集中匯入超過單機容量的資料,客戶端操作任意一個節點即可實現對叢集資料的讀寫操作。當叢集負載增長,使用者搜尋請求阻塞在單個節點上時,通過索引副本(Replica)機制就可以解決這個問題。索引副本(Replica)機制的的思路很簡單:為索引分片建立一份新的拷貝,它可以像原來的主分片一樣處理使用者搜尋請求。同時也順便保證了資料的安全性。即如果主分片資料丟失,ElasticSearch通過索引副本使得資料不丟失。索引副本可以隨時新增或者刪除,所以使用者可以在需要的時候動態調整其數量。

命令

叢集健康監控

curl -XGET 192.168.141.129:9200/_cat/health?v&pretty
GET /_cat/health?v

響應

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1536806018 10:33:38  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

獲取叢集中的節點列表

curl -XGET 192.168.141.129:9200/_cat/nodes?v&pretty
GET /_cat/nodes?v

響應

ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.141.129           24          89   2    0.00    0.10     0.18 mdi       *      7hyiUY2

列出所有的索引

curl -XGET 192.168.141.129:9200/_cat/indices?v&pretty
GET /_cat/indices?v

響應

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana e9VV3rJSTduGLdcwqg2ZKw   1   0          1            0        4kb            4kb

建立索引

curl -XPUT 192.168.141.129:9200/customer?pretty
PUT /customer?pretty

響應

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "customer"
}

刪除索引

curl -XDELETE 192.168.141.129:9200/customer?pretty
DELETE /customer?pretty

響應

{
  "acknowledged": true
}

建立文件

# 無id
curl -XPOST 192.168.141.129:9200/customer/doc?pretty -H 'Content-Type: application/json' -d '{"name": "John Doe"}'
POST /customer/doc?pretty
{
  "name": "John Doe"
}

# 有id
curl -XPUT 192.168.141.129:9200/customer/doc/1/_create?pretty -H 'Content-Type: application/json' -d '{"name": "John Doe"}'
PUT /customer/doc/1/_create
{
  "name": "John Doe"
}

響應

{
  "_index": "customer",
  "_type": "doc",
  "_id": "6zaU0WUBANcevg0yn4q5",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}
{
  "_index": "customer",
  "_type": "doc",
  "_id": "5",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

更新文件

curl -XPOST 192.168.141.129:9200/customer/doc/1/_update?pretty -H 'Content-Type: application/json' -d '{"doc": { "name": "Jane Doe" }}'
POST /customer/doc/1/_update?pretty
{
  "doc": { "name": "Jane Doe" }
}

響應

{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 27,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 27,
  "_primary_term" : 1
}

檢視文件

curl -XGET 192.168.141.129:9200/customer/doc/1?pretty
GET /customer/doc/1?pretty

響應

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

刪除文件

curl -XDELETE 192.168.141.129:9200/customer/doc/2?pretty
DELETE /customer/doc/2?pretty

響應

{
  "_index": "customer",
  "_type": "doc",
  "_id": "2",
  "_version": 2,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}

批處理

curl -XPOST '192.168.141.129:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'

POST /customer/doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

響應

{
  "took": 9,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "customer",
        "_type": "doc",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 29,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "customer",
        "_type": "doc",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 4,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

搜尋API

悶頭造測試資料中,api命令還未學習整理。