1. 程式人生 > 實用技巧 >linux系統Elasticsearch基礎(2)

linux系統Elasticsearch基礎(2)

一、跟ES互動的方式

1.使用curl命令

#建立索引
[root@db01 ~]# curl -XPUT 'http://10.0.0.51:9200/test'

#插入資料
[root@db01 ~]# curl -XPUT 'localhost:9200/student/user/1?pretty' -H 'Content-Type: application/json' -d '{"name": "lhd","sex":"man","age":"18","about":"good good study","interests":["chinese","english"]}'

2.head外掛

3.kibana的方式

1)安裝kibana

#上傳程式碼包
[root@db01 ~]# rz kibana-6.6.0-x86_64.rpm

#安裝
[root@db01 ~]# rpm -ivh kibana-6.6.0-x86_64.rpm

2)配置kibana

[root@db01 ~]# vim /etc/kibana/kibana.yml

[root@db01 ~]# grep "^[a-z]" /etc/kibana/kibana.yml
#程序的埠
server.port: 5601
#監聽地址
server.host: "10.0.0.51"
#指定ES的地址
elasticsearch.hosts: ["http://10.0.0.51:9200"]
#kibana也會建立索引
kibana.index: ".kibana"

3)啟動kibana

[root@db01 ~]# systemctl start kibana.service

#驗證
[root@db01 ~]# netstat -lntp       
tcp        0      0 10.0.0.51:5601          0.0.0.0:*               LISTEN      88636/node

4)訪問頁面

http://10.0.0.51:5601

二、ES資料操作

1.建立索引

#語法:
PUT /<index>

#示例:
PUT /qiudao
PUT zengdao

2.建立資料

1)資料結構

ES儲存資料三個必要構成條件
構成條件 說明
_index 索引(資料儲存的地方)
_type 型別(資料對應的類)
_id 資料唯一識別符號

2)語法

PUT /<index>/_doc/<_id>
POST /<index>/_doc/
PUT /<index>/_create/<_id>
POST /<index>/_create/<_id>

index:索引名稱,如果索引不存在,會自動建立
_doc:型別
<_id>:唯一識別符,建立一個數據時,可以自定義ID,也可以讓他自動生成

3)使用自定義ID插入資料

PUT /student/user/4
{
  "name":"congtianqi",
  "sex":"male"
}

#企業使用該方式少
	1.需要修改id值
	2.當指定ID時,插入資料時會查詢資料對比ID值

4)使用隨機ID插入資料

POST /student/user/
{
  "name":"liuxinyu",
  "sex":"fmale"
}

5)新增指定欄位

POST /student/user/
{
  "id":"1",
  "name":"liuxinyu",
  "sex":"fmale"
}

#企業應用較多

3.查詢資料

1)簡單查詢

#檢視所有索引資訊
GET _all
GET /_all

#檢視所有索引的資料
GET _all/_search

#檢視指定索引資訊
GET student

#檢視指定索引的資料
GET student/_search

#檢視指定資料
GET student/user/1

2)條件查詢

1>方法一:
GET /student/_search
{
  "query": {
    "term": {
      "age": {
        "value": "18"
      }
    }
  }
}
2>方法二:
GET /student/_search
{
  "query": {
    "term": {
      "age":"18"
    }
  }
}
3>方法三:
GET /student/_search
{
  "query": {
    "match": {
      "age": "18"
    }
  }
}

3)多條件查詢

1>must查詢
#查詢條件必須全部滿足
GET /student/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "age": {
              "value": "18"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "lhd"
            }
          }
        }
      ]
    }
  }
}
2>filter查詢
#跟must一樣,只不過在資料量很大時,比must查詢快一點
GET /student/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "age": {
              "value": "18"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "lhd"
            }
          }
        }
      ]
    }
  }
}
3>should查詢
#多條件查詢時,查詢條件只要有一個滿足就可以
GET /student/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "age": {
              "value": "18"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "lhd"
            }
          }
        }
      ]
    }
  }
}
4>must_not查詢
5>must和should結合
#查詢年齡是21歲或者年齡是18歲並且名字是lhd的資料
GET /student/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "age": {
              "value": "21"
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "age": {
                    "value": "18"
                  }
                }
              },
              {
                "term": {
                  "name": {
                    "value": "lhd"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
6>條件範圍查詢
GET /student/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 20,
              "lte": 25
            }
          }
        }
      ]
    }
  }
}

4.修改資料

#修改資料時指定id修改
PUT /student/user/1
{
  "name":"song",
  "sex":"fmale",
  "age":"18"
}

#注意,修改資料時,除了要修改的值。其他的值也要帶上
PUT /student/user/2
{
  "name":"lhd",
  "sex":"man",
  "age":"19"
}

5.刪除資料

#刪除指定ID資料
DELETE /student/user/4

#刪除索引
DELETE /student

三、叢集

1.叢集

1)叢集狀態

1.紅色:資料都不完整
2.黃色:資料完整,但是副本有問題
3.綠色:資料和副本全都沒有問題

2)節點型別

1.主節點:負責排程分配資料
2.資料節點:處理分配到自己的資料

3)分片

1.主分片:儲存資料,負責讀寫資料
2.副本分片:主分片的備份

2.搭建叢集

1)同步時間

2)安裝Java環境

3)安裝ES

4)配置檔案

[root@db01 ~]# grep "^[a-z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: es-cluster
node.name: node-1
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"]
discovery.zen.minimum_master_nodes: 2

[root@db02 ~]# grep "^[a-z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: es-cluster
node.name: node-1
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52"]
discovery.zen.minimum_master_nodes: 2

5)根據配置檔案建立目錄

6)啟動ES