SpringBoot + Elasticsearch之踩坑记录

本文详细介绍了如何在SpringBoot 1.x和2.x环境中配置和使用Elasticsearch,包括ES的安装、配置以及与SpringBoot项目的集成过程。特别关注了elasticsearch-ik分词器的安装和配置,以及Head插件的安装方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot 1.x支持elasticsearch <5.x

elasticsearch安装:按官网安装步骤:

(1)curl -L -O https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.4/elasticsearch-2.3.4.tar.gz

(2)tar -xvf elasticsearch-2.3.4.tar.gz

(3)cd elasticsearch-2.3.4/bin

(4)./elasticsearch

elasticsearch配置:

打开ES的config文件夹下的elasticsearch.yml;

配置文件如下:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
 cluster.name: my-name
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
 node.name: name
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
 path.data: /opt/modules/elasticsearch-2.3.4/data
#
# Path to log files:
#
 path.logs: /opt/modules/elasticsearch-2.3.4/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: false
#bootstrap.system_call_filter: false
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
 network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
 http.port: 9200
 transport.tcp.port: 9300
 transport.tcp.compress: true
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
 discovery.zen.ping.unicast.hosts: ["xx.xxx.xxx.xxx"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true
 http.cors.enabled: true
 http.cors.allow-origin: "*"
# elasticsearch plugin ik 
 index.analysis.analyzer.ik.type: "ik"

elasticsearch - ik分词器安装:

首先上git根据自己的ES版本号下载对应版本的ik。例如ES2.3.4的 https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v1.9.4(这里推荐下载第一个zip,可以省去mvn,当初第一次mvn真的花了很久时间)。

(1)将下载的zip解压缩,获得config文件夹、ik的一个jar文件、一些其他的文件。

config文件夹改名为analysis-ik,放入ES的config中

剩余文件放入ES的plugins目录下的analysis-ik文件夹中(文件夹没有,需要自己创建)。

 

重要!!!!!!!linux一些环境配置可参见:https://blog.youkuaiyun.com/daerzei/article/details/79761086

spring boot 2.x支持elasticsearch >5.x

es安装可参考此博客:https://blog.youkuaiyun.com/daerzei/article/details/79761086

分词器可参考:https://blog.youkuaiyun.com/u014315200/article/details/78630001

配置文件与上面的大相同,将配置文件最下面的分词器删除掉就行了,删除如下语句

index.analysis.analyzer.ik.type: "ik"

 

head的安装:

先安装nodejs,然后再安装head

nodejs安装:https://blog.youkuaiyun.com/xiatiancc/article/details/78872157

1、下载head插件源码

https://github.com/mobz/elasticsearch-head

由于elasticsearch5.0版本变化较大,目前elasticsearch5.0 暂时不支持直接安装,但是head作者提供了另一种安装方法。

可参见官网地址: https://github.com/mobz/elasticsearch-head

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值