Elasticsearch教程之一:介绍


Elasticsearch可以说是企业级搜索应用的最佳选择。相较于传统的关系型数据库,Elasticsearch更适合于存储海量数据并对其进行全文检索和数据分析(聚合)。同时ES适合进行结构化/非结构化查询。很多大型机构都使用ES,比如:Github, Wiki, 百度, 新浪,阿里等。

Elasticsearch教程

Elasticsearch系列教程

介绍

Solr和ES都是基于Lucene的,那么两者的区别在哪里?

  • 首先,ES内置分布式协调管理功能,而Solr则需要和Apache ZooKeeper配合才能实现分布式集群管理。
  • ES更擅长实时搜索,Solr则适合于传统的搜索场景。
  • ES的使用上更倾向于ES核心+插件以提供高级功能,而Solr多以官方提供的功能为主。
  • ES只支持json格式,Solr则可以支持更多格式。

ES的使用场景

  • 基于全文检索的应用
  • 对日志文件的分析

免费的Elastic Search / OpenSearch账号

可以申请bonsai的sandbox免费账号:https://bonsai.io/

Elasticsearch的安装(Ubuntu为例)

系统配置:

  • Ubuntu 20.04LTS
  • 4GB内存 2 vCPUs
  • 一个具有sudo权限的用户
  • OpenJDK 11

运行命令:

curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt update
sudo apt install elasticsearch

对ES进行配置:

sudo nano /etc/elasticsearch/elasticsearch.yml

添加:

network.host: localhost

尝试进行启动:

sudo systemctl start elasticsearch

如果想要在每次系统启动时自动启动ES:

sudo systemctl enable elasticsearch

结果看到这个错误提示:

Job for elasticsearch.service failed because a timeout was exceeded.
See "systemctl status elasticsearch.service" and "journalctl -xe" for details.

在运行“systemctl status elasticsearch.service”时得到如下提示:

elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
     Active: failed (Result: timeout) since Tue 2021-11-08 22:01:10 UTC; 31s ago
       Docs: https://www.elastic.co
    Process: 2888 ExecStart=/usr/share/elasticsearch/bin/systemd-entrypoint -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=143)
   Main PID: 2888 (code=exited, status=143)

Jun 08 21:59:55 ubuntu20 systemd[1]: Starting Elasticsearch...
Jun 08 22:01:10 ubuntu20 systemd[1]: elasticsearch.service: start operation timed out. Terminating.
Jun 08 22:01:10 ubuntu20 systemd[1]: elasticsearch.service: Failed with result 'timeout'.
Jun 08 22:01:10 ubuntu20 systemd[1]: Failed to start Elasticsearch.

解决方法:

sudo mkdir /etc/systemd/system/elasticsearch.service.d
echo -e "[Service]\nTimeoutStartSec=180" | sudo tee /etc/systemd/system/elasticsearch.service.d/startup-timeout.conf
sudo systemctl daemon-reload

测试一下更改的timeout是否有效:

sudo systemctl show elasticsearch | grep ^Timeout

再重新启动

sudo systemctl start elasticsearch

至此问题解决了。

使用curl进行测试

可以测试一下:

curl http://localhost:9200

添加信息到ES:

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

上面命令中:

  • tutorial是数据在ES中的索引
  • helloworld是数据类型
  • 1是在以上索引和类型中的数据ID

更改上面的数据:

curl -X PUT -H "Content-Type: application/json"  'localhost:9200/tutorial/helloworld/1?pretty' -d ' { "message": "Hello, elasticsearch!" }'

读取信息:

curl -X GET -H "Content-Type: application/json" 'http://localhost:9200/tutorial/helloworld/1?pretty'

安装Kibana

sudo apt install kibana

编辑配置文件:

sudo nano /etc/kibana/kibana.yml

添加内容:

server.port: 5601
server.host: "localhost"
elasticsearch.hosts: ["http://localhost:9200"]
kibana.index: ".kibana"

尝试启动Kibana:

sudo systemctl start kibana

如果想在系统启动时自动启动Kibana:

sudo systemctl enable kibana

这时还不能通过浏览器访问Kibana,需要完成以下步骤:

sudo apt-get install nginx apache2-utils
sudo htpasswd -c /etc/nginx/htpasswd.users kibanaadmin

输入一个密码

更新nginx的配置:

sudo nano /etc/nginx/sites-available/default

其内容为:

auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/htpasswd.users;

location / {
   proxy_pass http://localhost:5601;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection 'upgrade';
   proxy_set_header Host $host;
   proxy_cache_bypass $http_upgrade;
}

启动nginx:

sudo service nginx restart

访问端口80:http://localhost,就会看到kibana界面。

进入devtool,输入命令并执行:

GET tutorial/_doc/1

添加/测试数据

插入一条数据(ElasticVue中)

Elasticsearch教程

插入一条数据(OpenSearch Dashboards中)

POST testindex/_doc/2
{
    "name": "Lucy",
    "address": "11 temp Road"
}

Elasticsearch教程

直接读取

GET testindex/_doc/2

查询

GET testindex/_search
{
    "query": {
        "match_all": {}
     }
}

推荐工具

推荐一个浏览器插件:ElasticVue,在其中可以方便的:

  • 获取集群信息
  • 管理索引
  • 进行搜索
  • 使用REST查询的UI
  • 管理系统快照

Elasticsearch教程

安装ElasticSearch本地Docker镜像

如未安装Docker的话

安装docker

sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
apt-cache policy docker-ce
sudo apt install docker-ce
sudo systemctl status docker
sudo usermod -aG docker ${USER}
sudo systemctl status docker

安装ElasticSearch Docker镜像

docker pull amazon/opendistro-for-elasticsearch:latest
docker run -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" amazon/opendistro-for-elasticsearch:latest

测试:

curl -XGET https://localhost:9200 -u admin:admin --insecure
curl -XGET https://localhost:9200/_cat/nodes?v -u admin:admin --insecure
curl -XGET https://localhost:9200/_cat/plugins?v -u admin:admin --insecure

也可以通过浏览器测试:

Elasticsearch教程

也可以将其添加到ElasticVue插件中进行管理。

停止ElasticSearch服务

首先找到container-id:

docker ps

然后运行:

docker stop <container-id>

文章作者: 逻思
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明来源 逻思 !