Elasticsearch(ES)在SpringBoot中应用

下载Elasticsearch的地址:

Elasticsearch:官方分布式搜索和分析引擎 | Elastic在 RESTful 风格的分布式免费开源搜索和分析引擎开源中,Elasticsearch 处于领先地位,速度快,可实现水平可扩展性和可靠性,并能让您轻松进行管理。免费启用。https://www.elastic.co/cn/elasticsearch/

下载可视化工具(ElasticHD)地址:

GitHub - qax-os/ElasticHD: Elasticsearch 可视化DashBoard, 支持Es监控、实时搜索,Index template快捷替换修改,索引列表信息查看, SQL converts to DSL等Elasticsearch 可视化DashBoard, 支持Es监控、实时搜索,Index template快捷替换修改,索引列表信息查看, SQL converts to DSL等 - GitHub - qax-os/ElasticHD: Elasticsearch 可视化DashBoard, 支持Es监控、实时搜索,Index template快捷替换修改,索引列表信息查看, SQL converts to DSL等https://github.com/qax-os/ElasticHD

下载ik分词器(Ik分词器就是一款中文分词器,下载版本要和ES版本对应):

GitHub - medcl/elasticsearch-analysis-ik: The IK Analysis plugin integrates Lucene IK analyzer into elasticsearch, support customized dictionary.The IK Analysis plugin integrates Lucene IK analyzer into elasticsearch, support customized dictionary. - GitHub - medcl/elasticsearch-analysis-ik: The IK Analysis plugin integrates Lucene IK analyzer into elasticsearch, support customized dictionary.https://github.com/medcl/elasticsearch-analysis-ik/

JAVA中使用maven添加依赖(依赖包的版本要和下载的ES版本对应,ES版本迭代兼容还是比较差的,我用7.4.0和7.17.0在代码中用同种写法会有一个版本抛异常):

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>X.X.X</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>X.X.X</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>X.X.X</version>
        </dependency>

设置用户名和密码

这里需要为4个用户分别设置密码,elastic, kibana, logstash_system,beats_system

elastic 账号:拥有 superuser 角色,是内置的超级用户。
kibana 账号:拥有 kibana_system 角色,用户 kibana 用来连接 elasticsearch 并与之通信。Kibana 服务器以该用户身份提交请求以访问集群监视 API 和 .kibana 索引。不能访问 index。
logstash_system 账号:拥有 logstash_system 角色。用户 Logstash 在 Elasticsearch 中存储监控信息时使用。

执行设置用户名和密码的命令,这里需要为4个用户分别设置密码,elastic, kibana, logstash_system,beats_system

bin/elasticsearch-setup-passwords interactive

修改密码命令如下(网上找的,这块还没实践过,因为curl是linux的,我是在windows搭建,而且暂时没有修改密码的必要)

curl -H "Content-Type:application/json" -XPOST -u elastic 'http://127.0.0.1:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'

Elasticsearch启动

windows下启动 : 双击bin目录下的elasticsearch.bat即可

linux系统启动es : ./bin/elasticsearch -d 

(-d为在后台启动,不加-d时可以看到启动过程,出现started时启动完成)

启动时如果闪退,可以在logs里查看原因,如果报错信息是

org.elasticsearch.ElasticsearchException: X-Pack is not supported and Machine Learning is not available for [windows-x86]; you can use the other X-Pack features (unsupported) by setting xpack.ml.enabled: false in elasticsearch.yml

表示X-Pack不受支持,不适用于XX系统,那么就关掉xpack就行了

可以在config\elasticsearch.yml中加入一行

xpack.ml.enabled: false

Elasticsearch之settings和mappings的意义

简单的说,就是

  settings是修改分片和副本数的。

  mappings是修改字段和类型的。

可以用url方式来操作它们,也可以用java方式来操作它们。建议用url方式,因为简单很多。

在SpringBoot中的配置

spring:
    elasticsearch:
        rest:
          uris: http://127.0.0.1:9200
        protocol: http
        hostname: 127.0.0.1:9200
        port: 9200
        schema: http
        connectTimeOut: 2000
        socketTimeOut: 30000
        connectionRequestTimeOut: 1000
        maxConnectNum: 100
        maxConnectPerRoute: 100
        username: elastic
        password: xx

后续学习在JAVA中的操作
SpringBoot集成Elasticsearch7.4 实战(一)_仁聪的博客-CSDN博客_springboot集成es7来源:https://www.jianshu.com/p/1fbfde2aefa5一、SpringBoot集成Elasticsearch7.4 实战(一):这些操作都是在CentOS下操作的,主要带大家熟悉下Elasticsearch环境。1.1.下载& Linux下ElasticSearch安装1.2.中文分词插件IK1.3.索引1.4.如何数据管理二、Spring...https://blog.csdn.net/qq_34792830/article/details/105343450

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值