一、单机版ElasticSearch
对于基于 RedHat 的发行版,在 /etc/yum.repos.d/ 目录或基于 OpenSuSE 的发行版的 /etc/zypp/repos.d/ 目录中创建一个名为 elasticsearch.repo 的文件,其中包含:
[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md
您的存储库已准备好使用。您现在可以使用以下命令之一安装 Elasticsearch:
sudo yum install --enablerepo=elasticsearch elasticsearch
sudo dnf install --enablerepo=elasticsearch elasticsearch
sudo zypper modifyrepo --enable elasticsearch && \
sudo zypper install elasticsearch; \
sudo zypper modifyrepo --disable elasticsearch
启动停止:
systemctl start elasticsearch
systemctl stop elasticsearch
配置远程访问:
# 切换root用户
sudo -I
vim /etc/elasticsearch/elasticsearch.yml
# 设置远程访问
network.host: 0.0.0.0
# 开启访问端口
http.port: 9200
# 开启远程访问之后重启服务时抛异常,开启此属性就可以
cluster.initial_master_nodes: ["node-1"]
# 防火墙开放9200端口
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload
# 查看已开放端口
firewall-cmd --zone=public --list-ports
参考连接:
二、SpirngBoot整合
1、yml配置
spring:
# ES配置
elasticsearch:
rest:
uris: 172.16.196.3:9200
# 数据源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 8901
url: jdbc:mysql://localhost:3306/learn?serverTimezone=UTC&useSSL=false
mybatis:
# config-location: classpath:mybatis/mapper/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
use-generated-keys: true
2、使用Spring data Jpa(ElasticsearchRepository)操作ES
import com.yasir.search.bean.EsProduct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface EsProductRepository extends ElasticsearchRepository<EsProduct, Long> {
/**
* 搜索查询
*
* @param productName 商品名称
* @param page 分页信息
* @return
*/
Page<EsProduct> findByProductName(String productName, Pageable page);
}
ElasticsearchRepository类关键方法:
| 关键字 | 使用示例 | 等同于的ES查询 |
|---|---|---|
| And | findByNameAndPrice | {“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
| Or | findByNameOrPrice | {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
| Is | findByName | {“bool” : {“must” : {“field” : {“name” : “?”}}}} |
| Not | findByNameNot | {“bool” : {“must_not” : {“field” : {“name” : “?”}}}} |
| Between | findByPriceBetween | {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
| LessThanEqual | findByPriceLessThan | {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
| GreaterThanEqual | findByPriceGreaterThan | {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}} |
| Before | findByPriceBefore | {“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
| After | findByPriceAfter | {“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}} |
| Like | findByNameLike | {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}} |
| StartingWith | findByNameStartingWith | {“bool” : {“must” : {“field” : {“name” : {“query” : “? *”,”analyze_wildcard” : true}}}}} |
| EndingWith | findByNameEndingWith | {“bool” : {“must” : {“field” : {“name” : {“query” : “*?”,”analyze_wildcard” : true}}}}} |
| Contains/Containing | findByNameContaining | {“bool” : {“must” : {“field” : {“name” : {“query” : “?”,”analyze_wildcard” : true}}}}} |
| In | findByNameIn(Collectionnames) | {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}} |
| NotIn | findByNameNotIn(Collectionnames) | {“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}} |
| True | findByAvailableTrue | {“bool” : {“must” : {“field” : {“available” : true}}}} |
| False | findByAvailableFalse | {“bool” : {“must” : {“field” : {“available” : false}}}} |
| OrderBy | findByAvailableTrueOrderByNameDesc | {“sort” : [{ “name” : {“order” : “desc”} }],”bool” : {“must” : {“field” : {“available” : true}}}} |
问题点:
-
单机版ES整合
SpringBoot过程中集群状态异常(存在Unassigned);则需要先删除索引,手动创建模板的备份数量为0的索引,之后再启动项目。
Unassigned索引
参考连接:
关于ElasticsearchRepository的使用笔记
如果觉得Jpa不好用可以参考:









网友评论