ElasticSearch 入门(1)

什么是ElasticSearch?

  1. ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。

  2. Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

安装

CentOS 6.6
  1. 安装 Java 环境
$ yum install java-1.8.0-openjdk.x86_64
  1. 安装 elasticsearch
 # 当前最新版本是 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
  1. jdk 可以去官网直接下载然后安装

  2. 安装 ElasticSearch

$ bundle install elasticsearch

�配置

如果是 MacOSX 系统,则相对比较简单。由于服务器使用的是 CentOS 6.6,因此着重介绍 CentOS 下的配置

安装成功后,会在 /etc/elasticsearch 生成两个配置文件:

  • elasticsearch.yml - Elasticsearch 服务配置文件
  • logging.yml 日志配置文件

打开 /etc/elasticsearch/elasticsearch.yml,对文件中的如下项进行修改,记得要去掉前面的 #

cluster.name: my-cluster # 集群名称
node.name: my-node # 节点名称

path.data: /xxx/elasticsearch/data # 数据存放路径
path.logs: /xxx/elasticsearch/logs # 日志存放路径

network.host: 127.0.0.1 # 只允许本机访问

修改后,保存。启动 elasticsearch 服务

service elasticsearch start 

此外,我们可以把 elasticsearch �设置为开机自启动

$ chkconfig --add elasticsearch

测试

访问 http://localhost:9200 ,如果出现如下响应,则�服务运行成功。

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"
}

使用

  1. 添加一条记录
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.
  1. 通过如下方式获取实体的数据
curl -X GET 'http://localhost:9200/tutorial/helloworld/1'
  1. 如果要修改已经存在的实体,可以使用 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

  1. 删除存在的实体
curl -X DELETE 'localhost:9200/tutorial/helloworld/1?pretty'

遇到的问题

  • 错误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)

根据 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 这个用户来运行 elasticsearch,因此要修改下 /xxx/elasticsearch 的用户组

$ chmod -R  elasticsearch /xxx/elasticsearch
  • 错误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 

重新启动,不再报错

你可能感兴趣的:(ElasticSearch 入门(1))