ElasticSearch

Lucene

Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供。Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻。在Java开发环境里Lucene是一个成熟的免费开源工具。就其本身而言,Lucene是当前以及最近几年最受欢迎的免费Java信息检索程序库。——《百度百科》

Lucene 是一套信息检索工具包(jar包),不包含搜索引擎系统。

ElasticSearch 是基于Lucene 做了一些封装和增强。

ElasticSearch

Elaticsearch,简称为es,es是一个基于Lucene实现的开源、分布式、Restful的全文本搜索引擎;
它还是一个分布式实时文档存储,其中每个文档的每个字段均是被索引的数据,且可被搜索;
也是一个带实时分析功能的分布式搜索引擎,能够扩展至数以百计的节点实时处理PB级的数据;

Solr简介

Solr 是Apache下的一个顶级开源项目,采用java开发,它是基于Lucene的全文搜索服务器。Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。

Solr可以独立运行,运行在Jetty、Tomcat等这些Servlet容器中,Solr 索引的实现方法很简单,用 POST 方法向 Solr 服务器发送一个描述 Field 及其内容的 XML 文档,Solr根据xm文档添加、删除、更新索引。

Solr 搜索只需要发送 HTTP GET 请求,然后对 Sol返回Xml、json等格式的查询结果进行解析,组织页面布局。Solr不提供构建U!的功能,Sor提供了一个管理界面,通过管理界面可以查询Solr的配置和运行情况。
Solr是基于lucene开发企业级搜索服务器,实际上就是封装了lucene。

Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的文件,生成索引;也可以通过提出查找请求,并得到返回结果。

Solr和ElasticSearch

当单纯的对已有数据进行搜索时,Solr更快;
当实时建立索引时,Solr会产生IO阻塞,查询性能较差,Elasticsearch具有明显的优势;

安装elasticsearch
1、安装JDK1.8,最低要求版本为1.8
Linux安装JDK
2、下载安装包
官网

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.14.1-linux-x86_64.tar.gz

3、解压安装

tar -xzf elasticsearch-8.14.1-linux-x86_64.tar.gz

# pwd
/data/apps/elasticsearch
# ll
总用量 2264
drwxr-xr-x.  2 root root    4096 6月  11 07:42 bin                    #执行文件
drwxr-xr-x.  3 root root     210 6月  18 11:08 config                 #配置文件
drwxr-xr-x.  8 root root      96 6月  11 07:42 jdk
drwxr-xr-x.  6 root root    4096 6月  11 07:42 lib                    #elasticsearch的相关jar包
-rw-r--r--.  1 root root    3860 6月  11 07:34 LICENSE.txt
drwxr-xr-x.  2 root root       6 6月  11 07:37 logs
drwxr-xr-x. 81 root root    4096 6月  11 07:42 modules                #功能模块
-rw-r--r--.  1 root root 2285977 6月  11 07:37 NOTICE.txt
drwxr-xr-x.  2 root root       6 6月  11 07:37 plugins                #插件
-rw-r--r--.  1 root root    9070 6月  11 07:34 README.asciidoc

# ll config/
总用量 40
-rw-rw----. 1 root root  1042 6月  11 07:34 elasticsearch-plugins.example.yml
-rw-rw----. 1 root root  2730 6月  11 07:34 elasticsearch.yml                        #elasticsearch配置文件,默认9200
-rw-rw----. 1 root root  2727 6月  11 07:34 jvm.options                              #JAVA虚拟机的相关配置,配置内存大小(-Xms1g、-Xmx1g)
drwxr-x---. 2 root root     6 6月  11 07:37 jvm.options.d
-rw-rw----. 1 root root 17969 6月  11 07:39 log4j2.properties                        #日志配置文件
-rw-rw----. 1 root root   473 6月  11 07:39 role_mapping.yml
-rw-rw----. 1 root root   197 6月  11 07:39 roles.yml
-rw-rw----. 1 root root     0 6月  11 07:39 users
-rw-rw----. 1 root root     0 6月  11 07:39 users_roles

# adduser es
# passwd es
# chown -R es:es /data/apps/elasticsearch-7.6.1
# chmod 770 /data/apps/elasticsearch-7.6.1

4、启动服务

# su es
$ ./elasticsearch

# netstat -tlunp|grep 9300                                    
tcp6       0      0 127.0.0.1:9300          :::*                    LISTEN      127144/java
tcp6       0      0 ::1:9300                :::*                    LISTEN      127144/java

[root@jgswy-pro elasticsearch-7.6.1]# netstat -tlunp|grep 9200
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      127144/java
tcp6       0      0 ::1:9200                :::*                    LISTEN      127144/java

5、验证访问

[root@jgswy-pro elasticsearch-7.6.1]# curl 127.0.0.1:9200
{
  "name" : "jgswy-pro",
  "cluster_name" : "elasticsearch",                               
  "cluster_uuid" : "Rwkdn6agT5u8EniZ2pz-zA",
  "version" : {
    "number" : "7.6.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
    "build_date" : "2020-02-29T00:15:25.529771Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

安装elasticsearch-head

ealsticsearch只是后端提供各种resulful api,需要安装图形化界面。
elasticsearch-head是一款专门针对于elasticsearch的客户端工具,用来展示数据。
elasticsearch-head是基于JavaScript语言编写的,可以使用npm部署,npm是Nodejs下的包管理器。

1、安装node环境

npm (Node Package Manager) 是一个专为 JavaScript 语言设计的包管理器,它使得开发者能够方便地安装、更新和管理基于 Node.js 的项目中的各种依赖库。
npm 是 Node.js 官方推荐的工具,它不仅用于下载库文件,还包括了对这些库的版本控制和模块化管理。使用 npm,开发者可以在项目的 package.json 文件中声明所需的依赖,并通过简单的命令行操作来安装它们。此外,npm 还有一个公共的注册中心(npmjs.com),其中包含了大量开源的 JavaScript 库,供全球开发者共享和使用。

# tar -xvf node-v15.14.0-linux-x64.tar
# ln -sv node-v15.14.0-linux-x64 node-v15.14.0

# vi /etc/profile

export NODE_HOME=/data/apps/node-v15.14.0
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules

# source /etc/profile

# node -v
v15.14.0

2、安装elasticsearch-head

  • 下载elasticsearch-head安装包
    github地址

  • 配置NPM镜像源

# npm config get registry                                              #默认的镜像源                    
https://registry.npmjs.org/

# npm config set registry https://registry.npmmirror.com               #设置npm源为淘宝NPM镜像

# npm config get registry
https://registry.npm.taobao.org/
  • 解压安装
# tar -zxvf elasticsearch-head-5.0.0.tar.gz
# cd elasticsearch-head-5.0.0

  • 启动elasticsearch-head
# cd elasticsearch-7.6.1/
# npm install                                                          #利用node安装相关依赖
# npm fund                                                             #查看安装项目


# cd elasticsearch-head-5.0.0                                          #到elasticsearch-head文件夹下
# npm run start                                                        #启动

问题汇总
1、账户问题
解决方法:创建Elasticsearch账户,改变目录属主以及权限

future versions of Elasticsearch will require Java 11; your Java version from [/data/apps/jdk1.8.0_411/jre] does not meet this requirement
[2024-06-19T09:47:53,979][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [jgswy-pro] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.6.1.jar:7.6.1]
        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.6.1.jar:7.6.1]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.6.1.jar:7.6.1]
        ... 6 more
uncaught exception in thread [main]
java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /data/apps/elasticsearch-7.6.1/logs/elasticsearch.log

2、没有data目录
解决方法:需要创建data目录,并概念属主

[es@jgswy-pro bin]$ ./elasticsearch
future versions of Elasticsearch will require Java 11; your Java version from [/data/apps/jdk1.8.0_411/jre] does not meet this requirement
[2024-06-19T09:49:48,219][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [jgswy-pro] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: Unable to access 'path.data' (/data/apps/elasticsearch-7.6.1/data)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.6.1.jar:7.6.1]
        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.6.1.jar:7.6.1]
Caused by: java.lang.IllegalStateException: Unable to access 'path.data' (/data/apps/elasticsearch-7.6.1/data)
        at org.elasticsearch.bootstrap.FilePermissionUtils.addDirectoryPath(FilePermissionUtils.java:70) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Security.addFilePermissions(Security.java:297) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Security.createPermissions(Security.java:252) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Security.configure(Security.java:121) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:216) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.6.1.jar:7.6.1]
        ... 6 more
Caused by: java.nio.file.AccessDeniedException: /data/apps/elasticsearch-7.6.1/data
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84) ~[?:1.8.0_411]
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[?:1.8.0_411]
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) ~[?:1.8.0_411]
        at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384) ~[?:1.8.0_411]
        at java.nio.file.Files.createDirectory(Files.java:674) ~[?:1.8.0_411]
        at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781) ~[?:1.8.0_411]
        at java.nio.file.Files.createDirectories(Files.java:767) ~[?:1.8.0_411]
        at org.elasticsearch.bootstrap.Security.ensureDirectoryExists(Security.java:389) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.FilePermissionUtils.addDirectoryPath(FilePermissionUtils.java:68) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Security.addFilePermissions(Security.java:297) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Security.createPermissions(Security.java:252) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Security.configure(Security.java:121) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:216) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.6.1.jar:7.6.1]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.6.1.jar:7.6.1]
        ... 6 more
uncaught exception in thread [main]
java.lang.IllegalStateException: Unable to access 'path.data' (/data/apps/elasticsearch-7.6.1/data)
Likely root cause: java.nio.file.AccessDeniedException: /data/apps/elasticsearch-7.6.1/data
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
        at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
        at java.nio.file.Files.createDirectory(Files.java:674)
        at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
        at java.nio.file.Files.createDirectories(Files.java:767)
        at org.elasticsearch.bootstrap.Security.ensureDirectoryExists(Security.java:389)
        at org.elasticsearch.bootstrap.FilePermissionUtils.addDirectoryPath(FilePermissionUtils.java:68)
        at org.elasticsearch.bootstrap.Security.addFilePermissions(Security.java:297)
        at org.elasticsearch.bootstrap.Security.createPermissions(Security.java:252)
        at org.elasticsearch.bootstrap.Security.configure(Security.java:121)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:216)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /data/apps/elasticsearch-7.6.1/logs/elasticsearch.log

3、java版本问题

当前ES版本的特性需要java11才能支持

结局方法:

vi elasticsearch-env
#使用ES内置的jdk,注意制定出自己的es安装目录
export JAVA_HOME=/data/apps/elasticsearch-7.6.1/jdk/
export PATH=$JAVA_HOME/bin:$PATH
#添加jdk判断
if [ -x "$JAVA_HOME/bin/java" ]; then
    JAVA="/data/apps/elasticsearch-7.6.1/jdk/bin/java"
else
    JAVA=`which java`
fi

# now set the path to java

具体位置参考:

错误信息

[es@host elasticsearch-7.6.1]$ ./bin/elasticsearch
future versions of Elasticsearch will require Java 11; your Java version from [/data/apps/jdk1.8.0_411/jre] does not meet this requirement
Exception in thread "main" java.nio.file.AccessDeniedException: /data/apps/elasticsearch-7.6.1/config/jvm.options
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
        at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
        at java.nio.file.Files.newByteChannel(Files.java:361)
        at java.nio.file.Files.newByteChannel(Files.java:407)
        at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
        at java.nio.file.Files.newInputStream(Files.java:152)
        at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:64)

4、使用非 root用户启动ES,而该用户的文件权限不足而被拒绝执行。
解决方法:

chown -R 用户名:用户名  文件(目录)名
chown -R es:es elasticsearch-7.6.1

错误信息:

[es@host bin]$ ./elasticsearch
Exception in thread "main" java.nio.file.AccessDeniedException: /data/apps/elasticsearch-7.6.1/config/jvm.options
        at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
        at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:219)
        at java.base/java.nio.file.Files.newByteChannel(Files.java:374)
        at java.base/java.nio.file.Files.newByteChannel(Files.java:425)
        at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420)
        at java.base/java.nio.file.Files.newInputStream(Files.java:159)
        at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:64)

参考和转载

https://blog.csdn.net/smilehappiness/article/details/118466378(es安装)
https://blog.csdn.net/smilehappiness/article/details/118477460(node安装)
https://blog.csdn.net/qq_45363033/article/details/107148950

posted @ 2024-06-25 09:47  *一炁化三清*  阅读(12)  评论(0编辑  收藏  举报