ES安裝手冊

http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html
https://github.com/elastic/elasticsearch
###【在多臺機器上執行下面的命令】###
#es啓動時須要使用非root用戶,全部建立一個xiaoniu用戶:
useradd xiaoniu
#爲hadoop用戶添加密碼:
echo 123456 | passwd --stdin xiaoniu
#將bigdata添加到sudoers
echo "xiaoniu ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/xiaoniu
chmod 0440 /etc/sudoers.d/xiaoniu
#解決sudo: sorry, you must have a tty to run sudo問題,在/etc/sudoer註釋掉 Default requiretty 一行
sudo sed -i 's/Defaults requiretty/Defaults:xiaoniu !requiretty/' /etc/sudoershtml

#建立一個bigdata目錄
mkdir /{bigdata,data}
#給相應的目錄添加權限
chown -R xiaoniu:xiaoniu /{bigdata,data}node

-------------------------------------------------------------------------------------------------
1.安裝jdk(jdk要求1.8.20以上)linux

2.上傳es安裝包git

3.解壓es
tar -zxvf elasticsearch-5.4.3.tar.gz -C /bigdata/github

4.修改配置
vi /bigdata/elasticsearch-5.4.3/config/elasticsearch.yml
#集羣名稱,經過組播的方式通訊,經過名稱判斷屬於哪一個集羣
cluster.name: bigdata
#節點名稱,要惟一
node.name: es-1
#數據存放位置
path.data: /data/es/data
#日誌存放位置(可選)
path.logs: /data/es/logs
#es綁定的ip地址
network.host: 192.168.10.16
#初始化時可進行選舉的節點
discovery.zen.ping.unicast.hosts: ["node-4", "node-5", "node-6"]數據庫


/bigdata/elasticsearch-5.4.3/bin/elasticsearch -d
-------------------------------------------------------------------------------------------------
#出現錯誤
[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]瀏覽器

#用戶最大可建立文件數過小
sudo vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536curl

#查看可打開文件數量
ulimit -Hnelasticsearch


#最大虛擬內存過小
sudo vi /etc/sysctl.conf
vm.max_map_count=262144ide

#查看虛擬內存的大小
sudo sysctl -p


5.使用scp拷貝到其餘節點
scp -r elasticsearch-5.4.3/ node-5:$PWD
scp -r elasticsearch-5.4.3/ node-6:$PWD

6.在其餘節點上修改es配置,須要修改的有node.name和network.host

7.啓動es(/bigdata/elasticsearch-5.4.3/bin/elasticsearch -h查看幫助文檔)
/bigdata/elasticsearch-5.4.3/bin/elasticsearch -d


8.用瀏覽器訪問es所在機器的9200端口
http://192.168.10.16:9200/
{
"name" : "node-2",
"cluster_name" : "bigdata",
"cluster_uuid" : "v4AHbENYQ8-M3Aq8J5OZ5g",
"version" : {
"number" : "5.4.3",
"build_hash" : "eed30a8",
"build_date" : "2017-06-22T00:34:03.743Z",
"build_snapshot" : false,
"lucene_version" : "6.5.1"
},
"tagline" : "You Know, for Search"
}

kill `ps -ef | grep Elasticsearch | grep -v grep | awk '{print $2}'`

#查看集羣狀態
curl -XGET 'http://192.168.10.16:9200/_cluster/health?pretty'
http://192.168.10.16:9200/_cluster/health?pretty
------------------------------------------------------------------------------------------------------------------

RESTful接口URL的格式:
http://192.168.10.16:9200/<index>/<type>/[<id>]
其中index、type是必須提供的。
id是可選的,不提供es會自動生成。
index、type將信息進行分層,利於管理。
index能夠理解爲數據庫;type理解爲數據表;id至關於數據庫表中記錄的主鍵,是惟一的。


#向store索引中添加一些書籍
curl -XPUT 'http://192.168.10.16:9200/store/books/1' -d '{
"title": "Elasticsearch: The Definitive Guide",
"name" : {
"first" : "Zachary",
"last" : "Tong"
},
"publish_date":"2015-02-06",
"price":"49.99"
}'

#在linux中經過curl的方式查詢
curl -XGET 'http://192.168.10.18:9200/store/books/1'

#經過瀏覽器查詢
http://192.168.10.18:9200/store/books/1


#在添加一個書的信息
curl -XPUT 'http://192.168.10.18:9200/store/books/2' -d '{
"title": "Elasticsearch Blueprints",
"name" : {
"first" : "Vineeth",
"last" : "Mohan"
},
"publish_date":"2015-06-06",
"price":"35.99"
}'


# 經過ID得到文檔信息
curl -XGET 'http://192.168.10.18:9200/store/books/1'

#在瀏覽器中查看
http://92.168.10.18:9200/store/books/1

# 經過_source獲取指定的字段
curl -XGET 'http://192.168.10.16:9200/store/books/1?_source=title'
curl -XGET 'http://192.168.10.16:9200/store/books/1?_source=title,price'
curl -XGET 'http://192.168.10.16:9200/store/books/1?_source'

#能夠經過覆蓋的方式更新
curl -XPUT 'http://192.168.10.16:9200/store/books/1' -d '{
"title": "Elasticsearch: The Definitive Guide",
"name" : {
"first" : "Zachary",
"last" : "Tong"
},
"publish_date":"2016-02-06",
"price":"99.99"
}'

# 或者經過 _update API的方式單獨更新你想要更新的
curl -XPOST 'http://192.168.10.16:9200/store/books/1/_update' -d '{
"doc": {
"price" : 88.88
}
}'

curl -XGET 'http://192.168.10.16:9200/store/books/1'

#刪除一個文檔
curl -XDELETE 'http://192.168.10.16:9200/store/books/1'


curl -XPUT 'http://192.168.10.16:9200/store/books/4' -d '{
"title": "Elasticsearch: The Definitive Guide",
"author": "Guide",
"publish_date":"2016-02-06",
"price":"35.99"
}'


#https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
# 最簡單filter查詢
# SELECT * FROM books WHERE price = 35.99
# filtered 查詢價格是35.99的
# 返回的的分是1.0
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"price": 35.99
}
}
}
}
}'

# 返回的的分是1.0
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"constant_score": {
"filter": {
"term": {
"price": 35.99
}
}
}
}
}'

# 返回的的分是0.0
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"bool": {
"filter" : {
"term" : {
"price" : 35.99
}
}
}
}
}'

#指定多個值
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query" : {
"bool" : {
"filter" : {
"terms" : {
"price" : [35.99, 99.99]
}
}
}
}
}'

curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query" : {
"bool" : {
"must": {
"match_all": {}
},
"filter" : {
"terms" : {
"price" : [35.99, 99.99]
}
}
}
}
}'


# SELECT * FROM books WHERE publish_date = "2015-02-06"
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query" : {
"bool" : {
"filter" : {
"term" : {
"publish_date" : "2015-02-06"
}
}
}
}
}'

# bool過濾查詢,能夠作組合過濾查詢
# SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND publish_date != "2016-02-06"
# 相似的,Elasticsearch也有 and, or, not這樣的組合條件的查詢方式
# 格式以下:
# {
# "bool" : {
# "must" : [],
# "should" : [],
# "must_not" : [],
# }
# }
#
# must: 條件必須知足,至關於 and
# should: 條件能夠知足也能夠不知足,至關於 or
# must_not: 條件不須要知足,至關於 not

curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query" : {
"bool" : {
"should" : [
{ "term" : {"price" : 35.99}},
{ "term" : {"price" : 99.99}}
],
"must_not" : {
"term" : {"publish_date" : "2016-02-06"}
}
}
}
}'


# 嵌套查詢
# SELECT * FROM books WHERE price = 35.99 OR ( publish_date = "2016-02-06" AND price = 99.99 )

curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"bool": {
"should": [
{
"term": {
"price": 35.99
}
},
{
"bool": {
"must": [
{
"term": {
"publish_date": "2016-02-06"
}
},
{
"term": {
"price": 99.99
}
}
]
}
}
]
}
}
}'

# range範圍過濾
# SELECT * FROM books WHERE price >= 10 AND price < 99
# gt : > 大於
# lt : < 小於
# gte : >= 大於等於
# lte : <= 小於等於

curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"range" : {
"price" : {
"gte" : 10,
"lt" : 99
}
}
}
}

#name和author都必須包含Guide,而且價錢等於33.99或者188.99
curl -XGET 'http://192.168.10.16:9200/store/books/_search' -d '{
"query": {
"bool": {
"must": {
"multi_match": {
"operator": "and",
"fields": [
"name",
"author"
],
"query": "Guide"
}
},
"filter": {
"terms": {
"price": [
35.99,
188.99
]
}
}
}
}
}'

 

http://192.168.10.16:9200/store/books/_search

相關文章
相關標籤/搜索