1. 程式人生 > >Elasticsearch雜記(1)

Elasticsearch雜記(1)

Elasticssearch 查詢 搜索

一、安裝使用Elasticsearch

準備3個centos7節點

1、安裝

1.0 各節點時間需要同步

# /usr/sbin/ntpdate s2c.time.edu.cn

1.1 由於Elasticsearch是java開發,需要先安裝JDK環境

這邊使用系統默認的openjdk1.7,但是還需要安裝1.7的其他包組:
[root@master1 ~]# yum install java-1.7.0-openjdk-devel

設置java環境變量:
[root@master1 ~]# vim /etc/profile.d/java.sh

export JAVA_HOME=/usr

1.2 下載安裝rpm包

官方站點:https://www.elastic.co/downloads/past-releases

[root@master1 ~]# wget https://https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.noarch.rpm

安裝
[root@master1 ~]# yum install elasticsearch-1.7.2.noarch.rpm

1.3 修改配置文件

[root@master1 ~]# cd /etc/elasticsearch/
[root@master1 elasticsearch]# vim elasticsearch.yml 

cluster.name: myes1     #集群中的名字

node.name: "master1.com"    #本地節點的名字

index.number_of_shards: 5   #切片數量

index.number_of_replicas: 1 #副本數量

transport.tcp.port: 9300 #集群默認端口9300

1.4 啟動

[root@master1 ~]# systemctl daemon-reload

[root@master1 ~]# systemctl start elasticsearch.service

工作端口9200/tcp,集群檢測端口9300/tcp已經監聽

技術分享圖片

1.5 定義啟動第二個節點

[root@master2 ~]# yum install java-1.7.0-openjdk-devel

設置java環境變量:
[root@master1 ~]# scp /etc/profile.d/java.sh master2:/etc/profile.d/

安裝:
[root@master1 ~]# scp elasticsearch-1.7.2.noarch.rpm master2:/root/

[root@master2 ~]# yum install elasticsearch-1.7.2.noarch.rpm

復制master1節點的配置修改:
[root@master1 ~]# scp /etc/elasticsearch/elasticsearch.yml master2:/etc/elasticsearch/

修改配置文件:
[root@master2 ~]# vim /etc/elasticsearch/elasticsearch.yml 

cluster.name: myes

node.name: "master2.com"

啟動:
[root@master2 ~]# systemctl start elasticsearch.service

查看狀態:

技術分享圖片

1.6 定義第三個節點

master3配置同master2,註意集群節點名字要一樣,node節點改成master3.com即可

1.7 抓包觀察集×××互信息

[root@master2 ~]# yum install -y tcpdump

技術分享圖片

2、 查看集群信息(只能通過curl命令與其API進行交互)

2.1 查看節點狀態是否正常

[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/?preey‘
{
  "status" : 200,
  "name" : "master1.com",
  "cluster_name" : "myes",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"   #相當於hello world
}

2.2 cat的API顯示模式

[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/‘

技術分享圖片

2.2.1 顯示集群節點:

[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/nodes‘
master2.com 10.201.106.132 7 59 0.01 d m master2.com 
master1.com 10.201.106.131 5 61 0.00 d * master1.com 
master3.com 10.201.106.133 5 71 0.08 d m master3.com 

2.2.2 顯示詳細格式

[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/nodes?v‘

技術分享圖片

heap.percent:堆內存  ???
ram.percent:內存使用量
load:負載
node.role:節點角色
master:*符號為主節點

2.2.3 查看選項幫助

[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/nodes?help‘

只顯示需要的列:
[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/nodes?h=name,ip,port,uptime,heap,current,load‘
master2.com 10.201.106.132 9300   8.1h 0.03 
master1.com 10.201.106.131 9300 452.3d 0.00 
master3.com 10.201.106.133 9300     8h 0.02 

2.2.4 查看主節點

[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/master‘
GFN2hX0aQpySOSYqwXD8mw master1.com 10.201.106.131 master1.com 
[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/master?v‘
id                     host        ip             node        
GFN2hX0aQpySOSYqwXD8mw master1.com 10.201.106.131 master1.com 

查看集群健康狀態:
[root@master1 ~]# curl -X GET ‘http://10.201.106.131:9200/_cat/health?v‘
epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks 
1523578507 08:15:07  myes    green           3         3      0   0    0    0        0             0 

2.3.5 _cluster 專用於查看集群的子項

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cluster/health?v‘
{"cluster_name":"myes","status":"green","timed_out":false,"number_of_nodes":3,"number_of_data_nodes":3,"active_primary_shards":0,"active_shards":0,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0}

pretty美觀方式顯示

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cluster/health?pretty‘
{
  "cluster_name" : "myes",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 3,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0
}

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cluster/health?level=cluster&pretty‘

3、cluster API

3.1 查看集群版本

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cluster/state/version?pretty‘
{
  "cluster_name" : "myes",
  "version" : 7
}

3.2 查看誰是主節點

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cluster/state/master_node?pretty‘
{
  "cluster_name" : "myes",
  "master_node" : "GFN2hX0aQpySOSYqwXD8mw"
}

查看所有節點:
[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cluster/state/nodes?pretty‘
{
  "cluster_name" : "myes",
  "nodes" : {
    "Mv0nNWAaTz6v9OD_iTinIA" : {
      "name" : "master2.com",
      "transport_address" : "inet[/10.201.106.132:9300]",
      "attributes" : { }
    },
    "GFN2hX0aQpySOSYqwXD8mw" : {
      "name" : "master1.com",
      "transport_address" : "inet[/10.201.106.131:9300]",
      "attributes" : { }
    },
    "YAY5iFmJQAGHa6OuvLaMuA" : {
      "name" : "master3.com",
      "transport_address" : "inet[/10.201.106.133:9300]",
      "attributes" : { }
    }
  }
}

3.3 查看集群統計數據

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cluster/stats/?pretty‘

4、節點狀態信息

4.1 查看節點信息

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_nodes/stats/?pretty‘

5、Plusins(插件)

5.1 查看ES(Elasticsearch)插件目錄

[root@master1 ~]# rpm -ql elasticsearch | grep plugin
/usr/share/elasticsearch/bin/plugin
/usr/share/elasticsearch/plugins    #插件目錄

5.2 列出所有插件

查看使用幫助:
[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -h

[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -l
Installed plugins:
    - No plugin detected in /usr/share/elasticsearch/plugins

5.3 安裝插件

直接通過URL安裝插件:
[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -i marvel -u http://10.201.106.1:8080/plugins/marvel-latest.zip
-> Installing marvel...
Trying http://10.201.106.1:8080/plugins/marvel-latest.zip...
Downloading .............................DONE
Installed marvel into /usr/share/elasticsearch/plugins/marvel

列出安裝的插件:
[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -l
Installed plugins:
    - marvel

本地安裝方法:
下載目錄文件(不太好用):
wget -c -r -np -k -L -p http://10.201.106.1:8080/plugins/    

[root@master1 plugins]# pwd
/root/10.201.106.1:8080/plugins
[root@master1 plugins]# ls
bigdesk-latest.zip             index.html
elasticsearch-head-latest.zip  ~login
elasticsearch-kopf-master.zip  marvel-latest.zip
將以上文件mv到root家目錄下

本地安裝bigdesk插件:
[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -i bigdesk -u file:///root/bigdesk-latest.zip

查看:
[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -l
Installed plugins:
    - marvel
    - bigdesk

安裝其他插件:
[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -i head -u file:///root/elasticsearch-head-latest.zip

[root@master1 ~]# /usr/share/elasticsearch/bin/plugin -i kopf -u file:///root/elasticsearch-kopf-master.zip

5.4 訪問插件

http://10.201.106.131:9200/_plugin/marvel

技術分享圖片

http://10.201.106.131:9200/_plugin/bigdesk

技術分享圖片

http://10.201.106.131:9200/_plugin/head

技術分享圖片

http://10.201.106.131:9200/_plugin/kopf

技術分享圖片

6、索引

6.1 查看剛才插件安裝自動生成的索引

[root@master1 ~]# curl -XGET ‘http://10.201.106.131:9200/_cat/indices?v‘
health status index              pri rep docs.count docs.deleted store.size pri.store.size 
green  open   .marvel-2018.04.13   1   1        435            0      3.9mb          2.1mb 
green  open   .marvel-kibana       1   1          1            0      6.5kb          3.2kb 

6.2 創建索引下的文檔

郭靖同學,性別男,年齡25,課程:降龍十八掌
[root@master1 ~]# curl -XPUT ‘10.201.106.131:9200/students/class1/1?pretty‘ -d ‘
> {
> "first_name":"Jing",
> "last_name":"Guo",
> "gender":"Male",
> "age":25,
> "courses":"Xianglong Shiba Zhang"
> }‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "created" : true
}

添加第二個同學:
[root@master1 ~]# curl -XPUT ‘10.201.106.131:9200/students/class1/2?pretty‘ -d ‘
> {
> "first_name":"Rong",
> "last_name":"Huang",
> "gender":"Female",
> "age":23,
> "courses":"Luoying Shenjian"
> }‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 1,
  "created" : true
}

6.3 顯示剛才創建的索引下的文檔

[root@master1 ~]# curl -XGET ‘localhost:9200/students/class1/1?pretty‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source":
{
"first_name":"Jing",
"last_name":"Guo",
"gender":"Male",
"age":25,
"courses":"Xianglong Shiba Zhang"
}
}

查看第二個同學:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/class1/2?pretty‘

put重復ID,會被替換。建議生產環境,使用ID生成器

6.4 更新文檔

PUT方法會覆蓋原有文檔:PUT方法會覆蓋原有文檔:

修改黃蓉的年齡:
[root@master1 ~]# curl -XPOST ‘localhost:9200/students/class1/2/_update?pretty‘ -d ‘
{
"doc":{"age":22}
}‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 2
}

查看:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/class1/2?pretty‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 2,
  "found" : true,
  "_source":{"first_name":"Rong","last_name":"Huang","gender":"Female","age":22,"courses":"Luoying Shenjian"}

6.5 刪除文檔

[root@master1 ~]# curl -XDELETE ‘localhost:9200/students/class1/2‘
{"found":true,"_index":"students","_type":"class1","_id":"2","_version":3}[root@master1 ~]# 

查看已經沒找到ID2的文檔信息:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/class1/2?pretty‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "found" : false
}

6.6 刪除整個索引

查看有哪些索引:
[root@master1 ~]# curl -XGET ‘localhost:9200/_cat/indices?v‘
health status index              pri rep docs.count docs.deleted store.size pri.store.size 
green  open   .marvel-2018.04.13   1   1       4942            0     31.5mb           16mb 
green  open   students             5   1          1            0      7.8kb          3.9kb 
green  open   .marvel-kibana       1   1          1            0      6.5kb          3.2kb 

刪除students索引:
[root@master1 ~]# curl -XDELETE ‘localhost:9200/students‘
{"acknowledged":true}

再查看索引,students已經沒了:
[root@master1 ~]# curl -XGET ‘localhost:9200/_cat/indices?v‘

6.7 重新創建文檔

[root@master1 ~]# curl -XPUT ‘10.201.106.131:9200/students/class1/1?pretty‘ -d ‘
> {
> "first_name":"Jing",
> "last_name":"Guo",
> "gender":"Male",
> "age":25,
> "courses":"Xianglong Shiba Zhang"
> }‘

[root@master1 ~]# curl -XPUT ‘10.201.106.131:9200/students/class/2?pretty‘ -d ‘
> {
> "first_name":"Rong",
> "last_name":"Huang",
> "gender":"Female",
> "age":23,
> "courses":"Luoying shenjian"
> }‘

7、數據查詢

7.1 列出索引下的所有文檔

全量查詢,較少使用
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?pretty‘

qeury body查詢方式,能編寫更為復雜的查詢:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?pretty‘ -d ‘
> {
> "query": { "match_all": {} }
> }‘

7.2 查詢所有索引

數據特別多
[root@master1 ~]# curl -XGET ‘localhost:9200/_search?pretty‘

/_search:所有索引;
/INDEX_NAME/_search:單索引;
/INDEX1,INDEX2/_search:多索引;
/s*,t*/_search:通配符查找索引
/students/class1/_search:單類型搜索
/students/class1,class2/_search:多類型搜索

7.3 幾種查詢方式

1、GET /_search?q=‘Xianglong‘
2、GET /_search?q=‘Xianglong Shiba Zhang‘
3、GET /_search?q=courses:‘Xianglong Shibao Zhang‘
4、GET /_search?q=courses:‘Xianglong‘

前兩個:表示在_all域搜索;
後兩個:在指定的域上搜索;

7.4 幾種查詢方式測試

刪除
[root@master1 ~]# curl -XDELETE ‘localhost:9200/students/class/1‘

[root@master1 ~]# curl -XDELETE ‘localhost:9200/students/class/2‘
{"found":true,"_index":"students","_type":"class","_id":"2","_version":2}[root@master1 ~]# 

添加:
[root@master1 ~]# curl -XPUT ‘localhost:9200/students/class1/1?pretty‘ -d ‘
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}‘

[root@master1 ~]# curl -XPUT ‘localhost:9200/students/class1/2?pretty‘ -d ‘
{
"name": "Yang Guo",
"gender": "Male",
"age": 17,
"class": "Gumu Pai"
}‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 1,
  "created" : true
}

查詢:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q="Guo"‘
{"took":36,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.11506981,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}},{"_index":"students","_type":"class1","_id":"2","_score":0.11506981,"_source":
{
"name": "Yang Guo",
"gender": "Male",
"age": 17,
"class": "Gumu Pai"

添加
[root@master1 ~]# curl -XPUT ‘localhost:9200/students/class1/3?pretty‘ -d ‘
{
"name": "Huang Rong",
"gender": "Female",
"age": 22,
"parent": "Guo Xiaotian"
}‘
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "3",
  "_version" : 1,
  "created" : true
}

只要域中帶guo,都能搜出來:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q="Guo"‘
{"took":12,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.11506981,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}},{"_index":"students","_type":"class1","_id":"2","_score":0.11506981,"_source":
{
"name": "Yang Guo",
"gender": "Male",
"age": 17,
"class": "Gumu Pai"
}},{"_index":"students","_type":"class1","_id":"3","_score":0.11506981,"_source":
{
"name": "Huang Rong",
"gender": "Female",
"age": 22,
"parent": "Guo Xiaotian"
}}]}}[

只搜索Guo Jing
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q="Guo%20Jing"‘{"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.23013961,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.23013961,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"

查詢name=Guo
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q=name:"Guo"‘
{"took":30,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.19178301,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.19178301,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}},{"_index":"students","_type":"class1","_id":"2","_score":0.19178301,"_source":
{
"name": "Yang Guo",
"gender": "Male",
"age": 17,
"class": "Gumu Pai"
}}]}}[

再添加一個人: 
[root@master1 ~]# curl -XPUT ‘localhost:9200/students/class1/4?pretty‘ -d ‘
> {
> "name": "GuoXiang",
> "gender": "Female",
> "age": 10,
> "class": "Emei Pai"
> }‘

再次搜索Guo,沒有郭襄(切詞後的結果):
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q=name:"Guo"‘
{"took":18,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.19178301,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.19178301,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}},{"_index":"students","_type":"class1","_id":"2","_score":0.19178301,"_source":
{
"name": "Yang Guo",
"gender": "Male",
"age": 17,
"class": "Gumu Pai"
}}]}}

只搜Guo Jing:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q=name:"Guo%20Jing"‘
{"took":21,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.38356602,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.38356602,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}}]}}[root@master1 ~]# 

7.5 年齡搜索測試

[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q=25‘
{"took":10,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.11506981,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}}]}}

再加一個人,聲明25人之一
[root@master1 ~]# curl -XPUT ‘localhost:9200/students/class1/5‘ -d ‘
> {
> "name": "Xiaolong Nv",
> "gender": "Female",
> "age": 18,
> "description": "one of 25"
> }‘

再搜索25:
[root@master1 ~]# curl -XGET ‘localhost:9200/students/_search?q=25‘
{"took":12,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"5","_score":0.11506981,"_source":
{
"name": "Xiaolong Nv",
"gender": "Female",
"age": 18,
"description": "one of 25"
}},{"_index":"students","_type":"class1","_id":"1","_score":0.11506981,"_source":
{
"name": "Guo Jing",
"gender": "Male",
"age": 25,
"class": "Gai Bang"
}}]}}[root@master1 ~]# 

7.6 通過marvel插件鏈接插件

瀏覽器訪問
http://10.201.106.131:9200/_plugin/marvel/sense/

8、映射

8.1 查看指定類型的mapping

[root@master1 ~]# curl ‘localhost:9200/students/_mapping/class1?pretty‘
{
  "students" : {
    "mappings" : {
      "class1" : {
        "properties" : {
          "age" : {
            "type" : "long"
          },
          "class" : {
            "type" : "string"
          },
          "courses" : {
            "type" : "string"
          },
          "description" : {
            "type" : "string"
          },
          "first_name" : {
            "type" : "string"
          },
          "gender" : {
            "type" : "string"
          },
          "last_name" : {
            "type" : "string"
          },
          "name" : {
            "type" : "string"
          },
          "parent" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

Elasticsearch雜記(1)