1. Easy-ES介绍
Easy-Es
2. 导入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>//排除框架中原有的依赖包
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>2.0.0-beta1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.14.0</version>
</dependency>
在实体属性上添加注解
package com.yy.model;
import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName;
import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.annotation.rely.IdType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@IndexName(aliasName = "Es_product")//起别名,不起别名每次都要重新刷新
public class EsProduct {
@IndexId(type = IdType.CUSTOMIZE)
private Integer id;
@IndexField(fieldType = FieldType.TEXT)
private String name;
@IndexField(fieldType = FieldType.INTEGER)
private Integer categoryId;
@IndexField(fieldType = FieldType.DOUBLE)
private BigDecimal price;
@IndexField(fieldType = FieldType.TEXT)
private String brief;
@IndexField(fieldType = FieldType.KEYWORD)
private String img;
@IndexField(fieldType = FieldType.TEXT)
private List<String> tags;
@IndexField(fieldType = FieldType.INTEGER)
private Integer highOpinion;
@IndexField(fieldType = FieldType.INTEGER)
private Integer salesVolume;
@IndexField(fieldType = FieldType.DATE)
private LocalDateTime productionDate;
}
新建mapper类
package com.yy.dao;
import cn.easyes.core.core.BaseEsMapper;
import cn.easyes.starter.register.EsMapperScan;
import com.yy.model.EsProduct;
import org.springframework.stereotype.Component;
@Component
public interface EsProductMapper extends BaseEsMapper<EsProduct> {
}
在application.properties文件中配置 ES
# Easy-Es配置部分
easy-es:
# 启用Easy-Es功能
enable: true
# 设置Elasticsearch服务器地址和端口
address: 192.168.23.27:9200
# 全局配置项,设置是否打印执行的DSL语句(便于调试)
global-config:
print-dsl: true
Elasticsearch DSL (Domain Specific Language) 是一种专门设计用来与Elasticsearch搜索引擎进行交互的查询语言。它是一种基于JSON格式的查询语法,允许用户以结构化的方式来构建复杂的查询、过滤条件、聚合操作以及其他高级功能。
通过Elasticsearch DSL,开发人员可以灵活且高效地执行各种查询操作,包括全文本搜索、精确匹配、范围查询、布尔组合查询、排序、分页、高亮显示文本、统计计算、地理位置查询以及复杂的聚合分析等。
例如,一个简单的Elasticsearch DSL查询语句可能是查找索引my_index
中field1
字段包含关键词value1
的文档:
{
"query": {
"match": {
"field1": "value1"
}
}
}
在这个例子中,query
对象封装了一个match
查询,用于查找匹配给定字段内容的文档。
Elasticsearch还提供了多种客户端库,比如Elasticsearch官方提供的Java、Python、JavaScript等客户端,以及像elasticsearch-dsl这样的高级库,使得开发人员能够以更加面向对象或更符合各编程语言习惯的方式来构建和执行这些DSL查询语句。
编写测试对索引(相当于数据库中的表)进行创建、删除、查询索引
package com.yy;
import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.conditions.select.LambdaEsQueryWrapper;
import cn.hutool.core.collection.CollUtil;
import com.yy.dao.EsProductMapper;
import com.yy.model.EsProduct;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@SpringBootTest
class UserEsTests {
@Autowired
private EsProductMapper esProductMapper;
//创建索引,一般也不用单独创建索引,操作时会默认自动创建
@Test
void test1() {
// Boolean b = esMapper.createIndex();
}
//插入单条
@Test
void test2() {
EsProduct esProduct = EsProduct.builder()
.id(1)
.name("小米")
.img("图片地址")
.brief("小米(MI)Redmi Note12 5G 120Hz OLED屏幕 骁龙4移动平台 5000mAh长续航 8GB+128GB子夜黑 小米红米")
.price(new BigDecimal(18))
.categoryId(1)
.highOpinion(20)
.productionDate(LocalDateTime.now())
.salesVolume(99)
.tags(CollUtil.newArrayList("120高刷", "舒适护眼"))
.build();
esProductMapper.insert(esProduct);
}
//批量插入
@Test
void test3() {
List<EsProduct> esProducts = CollUtil.newArrayList();
for (int i = 1; i <= 10; i++) {
EsProduct esProduct = EsProduct.builder()
.id(i)
.name("小米"+i)
.img("图片地址"+i)
.brief("小米(MI)Redmi Note"+i+" 5G 120Hz OLED屏幕 骁龙4移动平台 5000mAh长续航 8GB+128GB子夜黑 小米红米")
.price(new BigDecimal(500.36+i))
.categoryId(1)
.highOpinion(100+i)
.productionDate(LocalDateTime.now())
.salesVolume(200+i)
.tags(CollUtil.newArrayList("12"+i+"高刷","舒适护眼"))
.build();
esProducts.add(esProduct);
}
esProductMapper.insertBatch(esProducts);
}
//删除
@Test
void test4() {
esProductMapper.deleteBatchIds(CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
}
@Test
void update() {
EsProduct esProduct = EsProduct.builder()
.id(1)
.name("su7------------------")
.img("图片地址")
.brief("小米汽车")
.price(new BigDecimal(18))
.categoryId(9)
.highOpinion(20)
.productionDate(LocalDateTime.now())
.salesVolume(99)
.tags(CollUtil.newArrayList("120高刷","舒适护眼"))
.build();
Integer integer = esProductMapper.updateById(esProduct);
}
//查询id等于1d的数据
@Test
void select() {
LambdaEsQueryWrapper queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
queryWrapper.eq("id",1);
List list = esProductMapper.selectList(queryWrapper);
}
@Test
void select2() {
LambdaEsQueryWrapper queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
queryWrapper.queryStringQuery("汽车之家"); //所有字段都去匹配
List list = esProductMapper.selectList(queryWrapper);
}
@Test
void select3() {
LambdaEsQueryWrapper queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
queryWrapper.eq("categoryId",1);
List list = esProductMapper.selectList(queryWrapper);
}
@Test
void select4() {
LambdaEsQueryWrapper<EsProduct> queryWrapper = new LambdaEsQueryWrapper<>();
queryWrapper.in(EsProduct::getCategoryId,1,9);
List list = esProductMapper.selectList(queryWrapper);
}
@Test
void select5() {
LambdaEsQueryWrapper<EsProduct> queryWrapper = new LambdaEsQueryWrapper<>();
//queryWrapper.match(EsProduct::getBrief,"汽车",1.0F);
//queryWrapper.match(EsProduct::getName,"汽车",1.0F);
//queryWrapper.multiMatchQuery("汽车", Operator.OR, EsProduct::getName,EsProduct::getBrief);
queryWrapper.in("categoryId",1,9); //where categroyId in (1,9) and ( name like '%汽车%' or brief like '%汽车%')
queryWrapper.and(
w->w.match(EsProduct::getBrief,"汽车",1.0F)
.or().match(EsProduct::getName,"汽车",2.0F));
List list = esProductMapper.selectList(queryWrapper);
String dsl = esProductMapper.getSource(queryWrapper);
System.out.println(dsl);
}
@Test
void select6() {
LambdaEsQueryWrapper<EsProduct> queryWrapper = new LambdaEsQueryWrapper<>();
queryWrapper.in("categoryId",1,9); //where categroyId in (1,9) and ( name like '%汽车%' or brief like '%汽车%')
queryWrapper.and(
w->w.match(EsProduct::getBrief,"高刷",1.0F)
.or().match(EsProduct::getName,"高刷",2.0F)
.or().match(EsProduct::getTags,"高刷",1.0F));
queryWrapper.orderByDesc(EsProduct::getSalesVolume);
EsPageInfo<EsProduct> esProductEsPageInfo = esProductMapper.pageQuery(queryWrapper, 2, 3);
String dsl = esProductMapper.getSource(queryWrapper);
System.out.println(dsl);
}
@Test
void select7() {
LambdaEsQueryWrapper<EsProduct> queryWrapper = new LambdaEsQueryWrapper<>();
queryWrapper.match(EsProduct::getName,"水汽车门");
List<EsProduct> esProducts = esProductMapper.selectList(queryWrapper);
}
}
原生Api调用
1查看索引mapping关系
GET /es_product/_mapping
2查看某个文档,具体字段的分词
GET /product/_doc/2/_termvectors?fields=brief