什么是ElasticSearch蝇恶?
ElasticSearch是一個(gè)基于Lucene的搜索服務(wù)器。它提供了一個(gè)分布式多用戶能力的全文搜索引擎蜈块,基于RESTful web接口陷谱。
Elasticsearch是用Java開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布鸥昏,是第二流行的企業(yè)搜索引擎塞俱。設(shè)計(jì)用于云計(jì)算中,能夠達(dá)到實(shí)時(shí)搜索互广,穩(wěn)定敛腌,可靠卧土,快速,安裝使用方便像樊。
安裝
CentOS 6.6
- 安裝
Java
環(huán)境
$ yum install java-1.8.0-openjdk.x86_64
- 安裝 elasticsearch
# 當(dāng)前最新版本是 2.2.0尤莺,我們這邊使用 rpm 來安裝
$ wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.2.0/elasticsearch-2.2.0.rpm
$ rpm -ivh elasticsearch-2.2.0.rpm
MacOSX
jdk 可以去官網(wǎng)直接下載然后安裝
安裝 ElasticSearch
$ bundle install elasticsearch
?配置
如果是 MacOSX
系統(tǒng),則相對比較簡單生棍。由于服務(wù)器使用的是 CentOS 6.6
颤霎,因此著重介紹 CentOS
下的配置
安裝成功后,會在 /etc/elasticsearch 生成兩個(gè)配置文件:
- elasticsearch.yml - Elasticsearch 服務(wù)配置文件
- logging.yml 日志配置文件
打開 /etc/elasticsearch/elasticsearch.yml涂滴,對文件中的如下項(xiàng)進(jìn)行修改友酱,記得要去掉前面的 #
cluster.name: my-cluster # 集群名稱
node.name: my-node # 節(jié)點(diǎn)名稱
path.data: /xxx/elasticsearch/data # 數(shù)據(jù)存放路徑
path.logs: /xxx/elasticsearch/logs # 日志存放路徑
network.host: 127.0.0.1 # 只允許本機(jī)訪問
修改后,保存柔纵。啟動(dòng) elasticsearch 服務(wù)
service elasticsearch start
此外缔杉,我們可以把 elasticsearch ?設(shè)置為開機(jī)自啟動(dòng)
$ chkconfig --add elasticsearch
測試
訪問 http://localhost:9200 ,如果出現(xiàn)如下響應(yīng)搁料,則?服務(wù)運(yùn)行成功或详。
bash-4.1# curl -X GET 'http://localhost:9200'
{
"name" : "my-node",
"cluster_name" : "my-cluster",
"version" : {
"number" : "2.2.0",
"build_hash" : "8ff36d139e16f8720f2947ef62c8167a888992fe",
"build_timestamp" : "2016-01-27T13:32:39Z",
"build_snapshot" : false,
"lucene_version" : "5.4.1"
},
"tagline" : "You Know, for Search"
}
使用
- 添加一條記錄
curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
返回
{
_index: "tutorial",
_type: "helloworld",
_id: "1",
_version: 1,
_shards: {
total: 2,
successful: 1,
failed: 0
},
created: true
}
- tutorial is the index of the data in Elasticsearch.
- helloworld is the type.
- 1 is the id of our entry under the above index and type.
- 通過如下方式獲取實(shí)體的數(shù)據(jù)
curl -X GET 'http://localhost:9200/tutorial/helloworld/1'
- 如果要修改已經(jīng)存在的實(shí)體,可以使用 HTTP PUT 請求:
curl -X PUT 'localhost:9200/tutorial/helloworld/1?pretty' -d '{
"message": "Hello People!"
}'
更新成功后返回:
{
"_index" : "tutorial",
"_type" : "helloworld",
"_id" : "1",
"_version" : 2,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : false
}
其中 _version
會自增(+1)郭计,從 1 變成 2
- 刪除存在的實(shí)體
curl -X DELETE 'localhost:9200/tutorial/helloworld/1?pretty'
遇到的問題
- 錯(cuò)誤1
bash-4.1# service elasticsearch start
Starting elasticsearch: log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /xxx/elasticsearch/logs/my-cluster.log (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
根據(jù) https://discuss.elastic.co/t/elasticsearch-unable-to-start/23824/5的提示
Ok so that will have installed the rpm package for Elasticsearch. It will have created an elasticsearch user on your linux OS and when you start the Elasticsearch service it will be running as the elasticsearch user. The problem you are encountering will be that the elasticsearch user does not have permission to write to /var/log/elasticsearch or the files inside it.
使用 rpm
安裝的話霸琴,會使用 elasticsearch
這個(gè)用戶來運(yùn)行 elasticsearch,因此要修改下 /xxx/elasticsearch
的用戶組
$ chmod -R elasticsearch /xxx/elasticsearch
- 錯(cuò)誤2
bash-4.1# service elasticsearch start
Starting elasticsearch: [ OK ]
bash-4.1# Exception in thread "main" BindTransportException[Failed to bind to [9300-9400]]; nested: ChannelException[Failed to bind to: localhost/35.127.0.0:9400]; nested: BindException[Cannot assign requested address];
把 /ect/elasticsearch/elasticsearch.yml
里的 localhost -> 127.0.0.1 后
network.host: 127.0.0.1
重新啟動(dòng)昭伸,不再報(bào)錯(cuò)