ES 歷史學習筆記梳理

簡介

官網:
https://www.elastic.co/

Elastic Search
1.基於Apache Lucene的開源搜索引擎
2.採用Java編寫 RESTful API風格
3.較容易的橫向擴展

應用場景
1.海量數據分析引擎
2.數據搜索引擎
3.數據倉庫

安裝

下載地址:
https://www.elastic.co/downloads/elasticsearch

官網推薦安裝:
1.Download and unzip Elasticsearch
Elasticsearch can also be installed from our package repositories using apt or yum, or installed on Windows using an MSI installer package. See Repositories in the Guide.

2.Run bin/elasticsearch (or bin\elasticsearch.bat on Windows)

3.Run curl http://localhost:9200/ or Invoke-RestMethod http://localhost:9200 with PowerShell

4.Dive into the getting started guide and video.
https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html
https://www.elastic.co/webinars/getting-started-elasticsearch

安裝實踐(Windows)

1.解壓zip

2.執行bin/elasticsearch.bat

當看到提示
publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}, {[::1]:9200}
started
表示啓動成功

打開瀏覽器 輸入127.0.0.1:9200
查看服務信息
{
    name: "GG9WPj_",
    cluster_name: "elasticsearch",
    cluster_uuid: "4ZrTtRADRaCtnb84OxAbFg",
    version: {
        number: "5.6.0",
        build_hash: "781a835",
        build_date: "2017-09-07T03:09:58.087Z",
        build_snapshot: false,
        lucene_version: "6.6.0"
    },
    tagline: "You Know, for Search"
}

這裏是未修改任何配置信息的情況下進行啓動服務的

3.修改配置
打開config/elasticsearch.yml
修改相關參數重新啓動

4.後臺啓動
在bin目錄下,打開命令行窗口 
elasticsearch -d

啓動參數參考:
Option                Description
------                -----------
-E <KeyValuePair>     Configure a setting
-V, --version         Prints elasticsearch version information and exits
-d, --daemonize       Starts Elasticsearch in the background
-h, --help            show help
-p, --pidfile <Path>  Creates a pid file in the specified path on start
-q, --quiet           Turns off standard ouput/error streams logging in console
-s, --silent          show minimal output
-v, --verbose         show verbose output

安裝head插件

1.head插件地址
http://mobz.github.io/elasticsearch-head/

2.安裝
在V2中,進入elasticsearch/bin目錄
使用plugin可以直接安裝插件
plugin install mobz/elasticsearch-head


3.在瀏覽器中輸入如下地址即可使用
http://localhost:9200/_plugin/head/




4.在V5.6版本中去除了plugin命令
改名爲elasticsearch-plugin
但是使用elasticsearch-plugin install mobz/elasticsearch-head 報錯:
ERROR: Unknown plugin mobz/elasticsearch-head
目前未清楚原因


使用npm進行安裝
4.1 安裝nodejs
4.2 安裝npm
4.3 使用npm安裝head插件
git clone https://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head/
npm install

4.4 啓動
cd elasticsearch-head/
npm start

使用如下地址進行訪問
http://localhost:9100

這樣訪問發現head並沒有連接上es
這裏需要修改一下ES的配置重新啓動

4.4.1 修改elasticsearch.yml
# 增加跨域的配置(需要重啓es才能生效)
http.cors.enabled: true
http.cors.allow-origin: "*"

4.4.2 編輯head/Gruntfile.js,修改服務器監聽地址,增加hostname屬性,將其值設置爲*
		connect: {
			server: {
				options: {
					port: 9100,
					base: '.',
					keepalive: true,
					hostname: '*'
				}
			}
		}

4.4.3 編輯head/_site/app.js,修改head連接es的地址,將localhost修改爲es的IP地址
# 原配置
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
# 將localhost修改爲ES的IP地址
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://YOUR-ES-IP:9200";

4.4.4 重新啓動es和head

集羣

注意:在比較乾淨的解壓版本進行復制

1.主節點配置
# 注意 冒號後面有一個空格
# 增加跨域的配置(需要重啓es才能生效)
http.cors.enabled: true
http.cors.allow-origin: "*"
#集羣名稱
cluster.name: menges
# 單節點名稱
node.name: master
# 指定這個節點爲master
node.master: true
#設置綁定的ip地址
network.host: 127.0.0.1
#端口
http.port: 9200

2.隨從節點1
#集羣名稱
cluster.name: menges
# 單節點名稱
node.name: slave1
#設置綁定的ip地址
network.host: 127.0.0.1
#端口
http.port: 8200
# 找到master節點
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

3.隨從節點2
#集羣名稱
cluster.name: menges
# 單節點名稱
node.name: slave2
# network.host: 127.0.0.1
http.port: 8400
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]


然後從主開始啓動節點

第一次實踐時,複製節點時把原來的data的數據也複製過來了,所以導致啓動失敗

配置樣例

#集羣名稱
cluster.name: menges
# 單節點名稱
node.name: slave1
#設置綁定的ip地址
network.host: 127.0.0.1
#端口
http.port: 8200
# 找到master節點
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]

es基本概念

集羣 cluster

節點 node

索引 含有相同屬性的文檔集合(相當於數據庫) 命名規則(英文字母小寫)

類型 索引中可以有多個類型(相當於表)

文檔 可以被索引的基本數據單位(相當於一條記錄)


分片 每個索引都有多個分片,每個分片是一個Lucene索引

備份 拷貝一個分片就是一個分片的備份

(創建索引時 默認創建5個分片 一個備份)

數據操作


創建索引
1.非結構化創建
在head頁面上打開“索引”,點擊新建索引,輸入 索引名稱、分片數和副本數

2.結構化創建
使用postman

路徑
127.0.0.1:9200/<索引名稱>
動作
PUT
(body->raw->json)
參數(json串)
{
    "settings":{
        "number_of_shards":3,  分片數
        "number_of_replicas":1  備份數
    },
    "mappings":{
        "man":{  類型
            "properties":{  屬性列表
                "name":{   屬性名
                    "type":"text"  屬性類型 text(文本)
                },
                "country":{
                    "type":"text"
                },
                "age":{
                    "type":"integer"  正型
                },
                "date":{
                    "type":"date",  日期類型
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"  epoch_millis表示時間戳
                }
            }
        },
        "woman":{

        }
    }
}

創建成功
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "people"
}

點擊索引下方的信息 索引信息  查看索引結構


API格式
http://<ip>:<port>/<索引>/<類型>/<文檔id>

動作:
GET 查詢
PUT 插入、修改
POST 插入
DELETE 刪除



1.指定文檔id插入
路徑
http://127.0.0.1:9200/<索引名>/<類型名>/<指定文檔id>
動作
PUT
參數
{
    "name":"萌寶",
    "country":"中國",
    "age":24,
    "date":"1993-04-05"
}

2.自動產生文檔id插入
路徑
http://127.0.0.1:9200/<索引名>/<類型名>
動作
POST
參數
{
    "name":"萌寶",
    "country":"中國",
    "age":24,
    "date":"1993-04-05"
}
返回:
{
    "_index": "people",
    "_type": "man",
    "_id": "AV6GklFAopU8OVQktwk3",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章