分佈式架構之ElasticSearch 初探

ElasticSearch

https://blog.csdn.net/zysgdhf4253/article/details/80961116

https://www.cnblogs.com/szwdun/p/10664348.html

 

1、普通版本安裝:

//到官網下載並上傳服務器,地址:https://www.elastic.co/downloads/elasticsearch
//也可直接服務器下載
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.1.tar.gz
tar -xvf elasticsearch-6.0.1.tar.gz
cd elasticsearch-6.0.1/bin
./elasticsearch -d

//啓動後另一窗口訪問一下
curl 127.0.0.1:9200

//上面如果用root用戶啓動會報錯,elasticsearch不允許用root用戶啓動,因爲它能接收腳本,防止被惡意執行腳本,所以我們新建用戶並分配權限,然後用該用戶啓動即可
adduser elasticsearchUser
passwd elasticsearchUser
chown -R elasticsearchUser elasticsearch-6.0.1
su elasticsearchUser

//安裝sql插件
//./bin/elasticsearch-plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/6.0.1.0/elasticsearch-sql-6.0.1.0.zip

//安裝中文分詞器IKAnalyzer
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.0.1/elasticsearch-analysis-ik-6.0.1.zip

    

ERROR: bootstrap checks failed
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
臨時設置:sudo sysctl -w vm.max_map_count=262144
永久修改:
修改/etc/sysctl.conf 文件,添加 “vm.max_map_count”設置
並執行:sysctl -p

2、docker版本安裝

  • docker :docker run -d --name=elasticsearch -p 9200:9200 -p 9300:9300 elasticsearch:6.4.0
  • 改內存大小,因爲主機內存不夠可能會報內存不足異常
find /var/lib/docker/overlay/ -name jvm.options
//把jvm.options文件的內存改成下面
-Xms256m  
-Xmx256m

        或者啓動時指定參數:-e "ES_JAVA_OPTS=-Xms256m -Xmx256m"

  • 還要改一下主機虛擬內存:sysctl -w vm.max_map_count=262144
  • 訪問:http://106.12.189.57:9200/
  • 配置跨域,進到容器
//安裝vim編輯器
apt-get update
apt-get install vim

vi config/elasticsearch.yml
//文件中加入跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"

//退出容器,重啓
  • 安裝 ElasticSearch-Head
docker pull mobz/elasticsearch-head:5
docker run -d --name es_admin -p 9100:9100 mobz/elasticsearch-head:5
  • elasticearch sql插件安裝
  • 安裝中文分詞器IKAnalyzer
docker exec -it elasticsearch /bin/bash

#此命令需要在容器中運行
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
docker restart elasticsearch

 

二、初步使用

以下所有訪問方式,帶地址的則是在瀏覽器地址欄訪問,而請求類型開頭的都是放在 kibana裏的dev tools訪問

1、查看集羣健康狀態

http://localhost:9200/_cat,http://localhost:9200/_cat/health?v,status有三個值green、yellow、red 分別代表健康、部分異常、不可用

2、查看索引(帶v會返回表頭)

http://localhost:9200/_cat/indices?v,GET /_cat/indices?v

3、創建索引(帶pretty會返回格式化的json)

默認創建(默認備份是1,默認分片是5到1024,索引名要小寫且不能重複):
PUT /person?pretty

帶參數的創建(shards分片2,replicas備份數2):
PUT people
{
    "settings" : {
        "index" : {
            "number_of_shards" : 2,
            "number_of_replicas" : 2
        }
    }
}

4、添加一個文檔到索引中

PUT /person/_doc/1?pretty
{
  "name": "xiaolin"
}

5、獲取指定文檔

GET /person/_doc/1?pretty

6、查詢所有文檔

GET /person/_search?q=*&pretty

7、給索引加mapping映射

es7之前,mappings和properties之間是多一層的
PUT user
{
    "mappings": {
      "my_doc": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "age": {
            "type": "long",
            "index": false
          }
        }
     }
  }
}

es7之後,取消了一層
PUT user
{
    "mappings": {
		"properties": {
		  "name": {
			"type": "text",
			"fields": {
			  "keyword": {
				"type": "keyword",
				"ignore_above": 256
			  }
			}
		  },
		  "age": {
			"type": "long",
			"index": false
		  }
		}
	}
}

8、查看索引配置信息

GET /person/_settings
GET /person/_mapping

9、刪除索引

DELETE /person

10、判斷索引是否存在

HEAD person

11、關閉和開啓索引

POST user/_close
POST user/_open

12、索引模板

//創建

//查看
GET /_template/template_1
//刪除
DELETE /_template/template_1

13、索引監控

 

 


 


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