Toc
  1. Elasticsearch
    1. 一. 是什么
    2. 一. 使用场景
    3. 一. 项目中使用
      1. 1.1 pom依赖
      2. 1.2 controller接口
    4. 二. 导入ES
    5. 2.1 接口
    6. 配置文件
      1. 1. 在centos7上以root用户启动elasticsearch时, 提示不能以root形式启动
      2. 2. 本地项目连接远程es时服务可用, 但报错Connection refused的问题
      3. 3. elasticsearch使用两个端口
Toc
0 results found
Goblin
Elasticsearch实现搜索
2017/05/22 Code 笔记 业务

Elasticsearch

一. 是什么

后台启动es

su elsearch
cd /opt/elastic.../bin
./elasticsearch -d

ik分词器

一. 使用场景

一. 项目中使用

mall项目的搜索模块:

目录结构

mall-search

​ |- src

​ |- main

​ |- java

​ |- com.mall.search

​ |- config

​ - MyBatisConfig

​ - Swagger2Config

​ |- controller

​ - EsProductController

​ |- dao

​ - EsProductDao

​ |- domain

​ - EsProduct

​ - EsProductAttributeValue

​ - EsProductRelatedInfo

​ |- repository

​ - EsProductRepository

​ |- service

​ |- impl

​ - EsProductServiceImpl

​ - EsProductService

​ - MallSearchApplication

​ |- resources

​ |- dao

​ |- EsProductDao.xml

​ - application.yml

​ - application-dev.yml

​ - application-prod.yml

​ - pom.xml

1.1 pom依赖


<dependencies>
<dependency>
<groupId>com.macro.mallgroupId>
<artifactId>mall-mbgartifactId>
dependency>

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
dependencies>

1.2 controller接口

/esProduct
/importAll 导入所有数据库中商品到ES
/delete/{id} 根据id删除商品
/delete/batch 根据id批量删除商品
/create/{id} 根据id创建商品
/search/simple 简单搜索
/search 综合搜索、筛选、排序
/recommend/{id} 根据商品id推荐商品
/search/relate 获取搜索的相关品牌、分类及筛选属性

二. 导入ES

2.1 接口

@ApiOperation(value = "导入所有数据库中商品到ES")
@RequestMapping(value = "/importAll", method = RequestMethod.POST)
@ResponseBody
public CommonResult importAllList() {
int count = esProductService.importAll();
return CommonResult.success(count);
}

importAll方法:

@Override
public int importAll() {
List esProductList = productDao.getAllEsProductList(null);
Iterable esProductIterable = productRepository.saveAll(esProductList);
Iterator iterator = esProductIterable.iterator();
int result = 0;
while (iterator.hasNext()) {
result++;
iterator.next();
}
return result;
}
<select id="getAllEsProductList" resultMap="esProductListMap">
select
p.id id,
p.product_sn productSn,
p.brand_id brandId,
p.brand_name brandName,
p.product_category_id productCategoryId,
p.product_category_name productCategoryName,
p.pic pic,
p.name name,
p.sub_title subTitle,
p.price price,
p.sale sale,
p.new_status newStatus,
p.recommand_status recommandStatus,
p.stock stock,
p.promotion_type promotionType,
p.keywords keywords,
p.sort sort,
pav.id attr_id,
pav.value attr_value,
pav.product_attribute_id attr_product_attribute_id,
pa.type attr_type,
pa.name attr_name
from pms_product p
left join pms_product_attribute_value pav on p.id = pav.product_id
left join pms_product_attribute pa on pav.product_attribute_id= pa.id
where delete_status = 0 and publish_status = 1
<if test="id!=null">
and p.id=#{id}
if>
select>

配置文件

# ======================== 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.
#
# es为大多数设置都给出了不错的默认值, 在你进行配置时, 确保你真的明白你这么做的影响以及结果.
#
# 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 consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# 配置一个节点的主要方法之一就是通过这个文件, 这个模板罗列出了你可能想要配置集群的
# 最重要的设置项.
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster: 给你的集群设置一个集群名, 默认是elasticsearch
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node: 给你的节点设置节点名
#
# 默认从elasticsearch-6.8.8.jar/lib/elasticsearch-6.8.8.jar!config/names.txt中随机选择一个名称
#
#node.name: node-1
#
# Add custom attributes to the node: 给你的节点设置一个标志(描述)
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# 设置存储数据的目录的路径, 用逗号分隔多个位置, 默认存储在 es文件夹/data 目录下
#
#path.data: /path/to/data
#
# Path to log files: 默认在es文件夹/logs
#
# 日志文件的路径
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup: 锁定物理内存地址,防止elasticsearch内存被交换出去,
# 也就是避免es使用swap交换分区
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size 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.
# 当系统交换内存时,Elasticsearch的性能很差。
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# 为es设置ip绑定,默认是127.0.0.1,也就是默认只能通过127.0.0.1 或者localhost才能访问
#
# es1.x版本默认绑定的是0.0.0.0 所以不需要配置,但是es2.x版本默认绑定的是127.0.0.1,需要配置
#
# network.host: 192.168.0.1
#
# 生产环境, 这里换成0.0.0.0, 允许外部访问
network.host: 0.0.0.0
#
#
# Set a custom port for HTTP: 为es设置自定义端口,默认是9200
#
# 注意:在同一个服务器中启动多个es节点的话,默认监听的端口号会自动加1:例如:9200,9201,9202...
# http.port: 9200
#
# For more information, consult the network module documentation. 更多的信息在网络文档上
#
# --------------------------------- Discovery ----------------------------------
# 当启动新节点时,通过这个ip列表进行节点发现,组建集群
# 默认节点列表:
# 127.0.0.1,表示ipv4的回环地址。
# [::1],表示ipv6的回环地址
#
# 在es1.x中默认使用的是组播(multicast)协议,默认会自动发现同一网段的es节点组建集群,
# 在es2.x中默认使用的是单播(unicast)协议,想要组建集群的话就需要在这指定要发现的节点信息了。
# 注意:如果是发现其他服务器中的es服务,可以不指定端口[默认9300],如果是发现同一个服务器中的es服务,就需要指定端口了。
#
# 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"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
# 通过配置这个参数来防止集群脑裂现象 (集群总节点数量/2)+1
#
#discovery.zen.minimum_master_nodes: (集群总节点数量/2)+1
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
# 一个集群中的N个节点启动后,才允许进行数据恢复处理,默认是1
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
# 多样化配置:
#
# Require explicit names when deleting indices: 删除节点时需要明确的支出
#
#action.destructive_requires_name: true
#
#
#总结
#
#  1、es已经为大多数参数设置合理的默认值
#
#  2、这个配置文件中列出来了针对生产环境下的一些重要配置
#
#  3、注意:这个文件是yaml格式的文件
#
#    (1):属性顶格写,不能有空格
#
#    (2):缩进一定不能使用tab制表符
#
#    (3):属性和值之间的:后面需要有空格
#        network.host: 192.168.80.200
#

1. 在centos7上以root用户启动elasticsearch时, 提示不能以root形式启动

原因: 出于系统安全考虑的设置 ,不允许root账号启动

[root@iz2zect9h9unbhaoadluycz bin]# sudo ./elasticsearch

[2020-04-23T14:51:31,719][WARN ]
[o.e.b.ElasticsearchUncaughtExceptionHandler] [unknown] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root

Caused by: java.lang.RuntimeException: can not run elasticsearch as root

解决方法:

创建elsearch用户组及elsearch用户:

groupadd elsearch
useradd elsearch -g elsearch
passwd elsearch

更改elasticsearch文件夹及内部文件的所属用户及组为elsearch:elsearch
cd /opt
chown -R elsearch:elsearch elasticsearch-6.8.8

切换到elsearch用户再启动
su elsearch
cd /opt/elasticsearch-6.8.8/bin
./elasticsearch

2. 本地项目连接远程es时服务可用, 但报错Connection refused的问题

Es远程连接elasticsearch时, 会在本地localhost:9200进行一次Elasticsearch health check, 而本地此时是没有启动es的, 此时就会报错java.net.ConnectException: Connection refused

这种检测意义不大, 可以关闭, 或者设置为远程线上检查

management:
health:
elasticsearch:
enabled: false

3. elasticsearch使用两个端口

restful接口9200和tcp接口9300, 在云服务器的安全组配置中, 要开放这两个端口, 如果使用了宝塔面板, 在宝塔中也要配置开放端口. 项目中使用的是9300接口

spring:
data:
elasticsearch:
repositories:
enabled: true
cluster-nodes: xxx.xxx.xx.xx:9300

https://blog.csdn.net/lixiaohai_918/article/details/89569611

打赏
支付宝
微信
本文作者:Goblin
版权声明:本文首发于Goblin的博客,转载请注明出处!