欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux系统下 centos7下搭建ElasticSearch中间件及常用接口演示

程序员文章站 2023-11-16 09:18:34
一、中间件简介 1、基础概念 elasticsearch是一个基于lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于restful web接口。...

一、中间件简介

1、基础概念

elasticsearch是一个基于lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于restful web接口。elasticsearch是用java开发的,并作为apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。

2、分布式数据库

分布式数据库系统通常使用较小的计算机系统,每台计算机可单独放在一个地方,每台计算机中都可能有dbms的一份完整拷贝副本,或者部分拷贝副本,并具有自己局部的数据库,位于不同地点的许多计算机通过网络互相连接,共同组成一个完整的、全局的逻辑上集中、物理上分布的大型数据库。

3、核心角色

1)节点和集群

cluster代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的。es的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看es集群,在逻辑上是个整体。单个 elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

2)shards分片

代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。

3)document文档
index 里面单条的记录称为 document(文档)。许多条 document 构成了一个 index。document 使用 json 格式表示。

4)index索引

elastic 会索引所有字段,查找数据的时候,直接查找该索引。每个 index (即理解为数据库名称)的名字必须是小写。

5)type类型

document 可以根据type进行虚拟的逻辑分组,用来过滤 document,即理解为数据库表名称。

二、中间件安装

1、安装环境和版本

centos7
jdk1.8
elasticsearch-6.3.2

2、下载解压

下载的路径,当前目录的文件夹下,也可以指定下载路径。wget -p 目录 网址。

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.zip
[root@localhost roo]# mv elasticsearch-6.3.2.zip /usr/local/mysoft/
[root@localhost mysoft]# unzip elasticsearch-6.3.2.zip

3、启动软件

[root@localhost mysoft]# cd elasticsearch-6.3.2/
[root@localhost elasticsearch-6.3.2]# ./bin/elasticsearch

1)报错一

org.elasticsearch.bootstrap.startupexception: 
java.lang.runtimeexception: can not run elasticsearch as root

新建用户组和用户

[root@localhost]# useradd esroot
[root@localhost]# passwd esroot
[root@localhost]# groupadd esgroup
[root@localhost]# usermod -g esgroup esroot

esroot用户授权

chown esroot /usr/local/mysoft/elasticsearch-6.3.2 -r

切换到esroot用户

[root@localhost mysoft]# su - esroot
[esroot@localhost ~]$ su #回到root用户

2)报错二

max file descriptors [4096] for elasticsearch process is too low, 
increase to at least [65536]

执行如下命名,该操作在root权限下操作。

[root@localhost roo]# vim /etc/security/limits.conf 

添加内容

* soft nofile 65536
* hard nofile 65536

切回esroot用户

 再次启动,没有报错信息。

4、打开命令行测试

curl localhost:9200
[roo@localhost ~]$ curl localhost:9200
{
 "name" : "yms44oi",
 "cluster_name" : "elasticsearch",
 "cluster_uuid" : "2zxjbnkjsjiev_k1iwmzrq",
 "version" : {
 "number" : "6.3.2",
 "build_flavor" : "default",
 "build_type" : "zip",
 "build_hash" : "053779d",
 "build_date" : "2018-07-20t05:20:23.451332z",
 "build_snapshot" : false,
 "lucene_version" : "7.3.1",
 "minimum_wire_compatibility_version" : "5.6.0",
 "minimum_index_compatibility_version" : "5.0.0"
 },
 "tagline" : "you know, for search"
}

这样elasticsearch-6.3.2环境搭建成功。

 请求9200端口,elastic 返回一个 json 对象,包含当前节点、集群、版本等信息。
 按下 ctrl + c,elastic 就会停止运行。

5、配置外部访问

默认情况下,elastic 只允许本机访问,如果需要远程访问,可以修改 elastic 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动 elastic。

[esroot@localhost config]$ cd /usr/local/mysoft/elasticsearch-6.3.2/config
[esroot@localhost config]$ vim elasticsearch.yml 
network.host: 0.0.0.0

6、安装ik中文分词器

切换到root用户

[root@localhost elasticsearch-6.3.2]$ ./bin/elasticsearch-plugin 
install 
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.2/elasticsearch-analysis-ik-6.3.2.zip

三、入门操作

索引创建和删除

1、创建索引

[esroot@localhost ~]$ curl -x put 'localhost:9200/esindex01'
# 返回数据
{
 "acknowledged": true,
 "shards_acknowledged": true,
 "index": "esindex01"
}

服务器返回一个 json 对象,acknowledged:true字段表示操作成功。

2、删除索引

[esroot@localhost ~]$ curl -x delete 'localhost:9200/esindex01'
{"acknowledged":true}

acknowledged:true字段表示操作成功。

四、源代码地址

github地址:知了一笑
https://github.com/cicadasmile
码云地址:知了一笑
https://gitee.com/cicadasmile

总结

以上所述是小编给大家介绍的linux系统下 centos7下搭建elasticsearch中间件及常用接口演示 ,希望对大家有所帮助