Elasticsearch安装使用

By admin, 22 五月, 2018

1. 下载安装包

到官网下载安装包:https://www.elastic.co/downloads/elasticsearch

我下载的是用在Ubuntu上的deb包。

 

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.deb

$ sudo dpkg -i elasticsearch-6.2.4.deb

2. 配置Elasticsearch为启动服务

sudo systemctl enable elasticsearch.service

3. 配置(可选)

$ sudo vi /etc/elasticsearch/elasticsearch.yml

 

. . .
cluster.name: mycluster1
node.name: "My First Node"
. . .

服务默认占用内存1G,因为我的服务器内存小,改成256M:

$ sudo vi /etc/elasticsearch/jvm.options

-Xms256m
-Xmx256m

4. 启动服务

$ sudo systemctl start elasticsearch

5. 测试

$ curl -X GET 'http://localhost:9200'

 

$ curl -XGET 'http://localhost:9200/_nodes?pretty'

 

$ curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'

$ curl -X GET 'http://localhost:9200/tutorial/helloworld/1'

6. 安装中文分词

/usr/share/elasticsearch$ sudo ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zipes/download/

$ sudo service elasticsearch restart

用法详见:https://github.com/medcl/elasticsearch-analysis-ik/

7. 查看索引状态

curl -XGET 'http://localhost:9200/_cat/indices?format=json&pretty'

8. 增加身份验证机制(可选)

默认情况下,9200端口仅可本地访问。如果允许外部机器控制Elastic Search,那么应该增加身份验证机制。可以利用Nginx做一个反向代理来实现简单的身份验证。

/etc/nginx/sites-enabled$ cat elasticsearch
upstream elasticsearch {
    server 127.0.0.1:9200;
}

server {
    listen 8200;
    auth_basic "Protected Elasticsearch";
    auth_basic_user_file /etc/nginx/elasticsearch_passwords;

    location / {
      proxy_pass http://elasticsearch;
      proxy_redirect off;
    }
}

$ printf "elasticsearch:$(openssl passwd -crypt PASSWORD)" > /etc/nginx/elasticsearch_passwords

$ service nginx restart

$ curl -i localhost:8200
# HTTP/1.1 401 Unauthorized
# ...

$ curl -i elasticsearch:PASSWORD@localhost:8200
# HTTP/1.1 200 OK
# ...

 

8. 官方教程及API

进一步的用法参考API文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html

教程:jiao chenhttps://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

参考:

评论

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
请输入"Drupal10"