赞
踩
- 主要引用 elasticsearch使用相关依赖
- 注意ES 服务端版本(7.4.0)要与客户端版本相容
- 注意 hutool的工具类 实体转map 版本
<!--引入springboot父工程依赖--> <!--引入依赖作用: 可以省去version标签来获得一些合理的默认配置 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> </parent><dependencies> <!--elasticsearch使用相关依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!--引入提供Web开发场景依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--这里用到了类型转Map,这里用到hutool的工具类(版本高一些,低版本的 如果传的就是Map类型,在map转map时会报错)--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core</artifactId> <version>5.7.13</version> </dependency> <!--用@Data 减少JavaBean get...set...方法--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--两个用来做测试的jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies></pre> <h2 id="devmenu3"><a name="t1"></a>2.application 配置</h2> <pre data-index="1" class="set-code-show" name="code">spring:
elasticsearch:
rest:
uris: 127.0.0.1:9200 #—ES的连接地址,多个地址用逗号分隔
username: #—用户名
password: #—密码
connection-timeout: 1000 #—连接超时时间(默认1s)
read-timeout: 1000 #—读取超时时间(默认30s)
- 注解:@Document用来声明Java对象与ElasticSearch索引的关系
indexName 索引名称(是字母的话必须是小写字母)
type 索引类型
shards 主分区数量,默认5
replicas 副本分区数量,默认1
createIndex 索引不存在时,是否自动创建索引,默认true
不建议自动创建索引(自动创建的索引 是按着默认类型和默认分词器)- 注解:@Id 表示索引的主键
- 注解:@Field 用来描述字段的ES数据类型,是否分词等配置,等于Mapping描述
index 设置字段是否索引,默认是true,如果是false则该字段不能被查询
store 默认为no,被store标记的fields被存储在和index不同的fragment中,以便于快速检索。虽然store占用磁盘空间,但是减少了计算。
type 数据类型(text、keyword、date、object、geo等)
analyzer 对字段使用分词器,注意一般如果要使用分词器,字段的type一般是text。
format 定义日期时间格式,详细见 官方文档: format | Elasticsearch Guide [8.9] | Elastic.- 注解:@CompletionField 定义关键词索引 要完成补全搜索
analyzer 对字段使用分词器,注意一般如果要使用分词器,字段的type一般是text。
searchAnalyzer 显示指定搜索时分词器,默认是和索引是同一个,保证分词的一致性。
maxInputLength:设置单个输入的长度,默认为50 UTF-16 代码点
常用数据类型:
简单类型:
字符串类型: - string(不再支持)可知string类型的field已经被移除了, 我们需要用text或keyword类型来代替string.
text:会分词,不支持聚合
keyword:不会分词,将全部内容作为一个词条,支持聚合数字类型: 尽可能选择范围小的数据类型, 字段的长度越短, 索引和搜索的效率越高;(优先考虑使用带缩放因子的浮点类型)
byte : 有符号的8位整数, 范围: [-128 ~ 127]
short : 有符号的16位整数, 范围: [-32768 ~ 32767]
integer : 有符号的32位整数, 范围: [−231 ~ 231-1]
long : 有符号的64位整数, 范围: [−263 ~ 263-1]
float : 32位单精度浮点数
double : 64位双精度浮点数
half_float : 16位半精度IEEE 754浮点类型
scaled_float : 缩放类型的的浮点数, 比如price字段只需精确到分, 57.34缩放因子为100, 存储结果为5734日期类型:date JSON没有日期数据类型, 所以在ES中, 日期可以是:
— 包含格式化日期的字符串 “2018-10-01”, 或"2018/10/01 12:10:30" (可以通过 format属性 定义日期时间格式)DateFormat 官方文法.
— 代表时间毫秒数的长整型数字.
— 代表时间秒数的整数.布尔类型: boolean 可以接受表示真、假的字符串或数字:
— 真值: true, “true”, “on”, “yes”, “1”…
— 假值: false, “false”, “off”, “no”, “0”, “”(空字符串), 0.0, 0
复杂类型:
- 数组[]: 由数组中第一个非空值决定数组类型(type = FieldType.Keyword)
- List集合: 由数组中第一个非空值决定数组类型(type = FieldType.Keyword)
- 嵌套类型: list里泛型是object形式的或自定义对象(type = FieldType.Nested)
- 对象:{ }object for single JSON objects 单个JSON对象(type = FieldType.Object)
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.util.Date;
import java.util.List;
/*
学生
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
//indexName名字如果是字母那么必须是小写字母
@Document(indexName = “student”, type = “_doc”, replicas = 1, shards = 1, createIndex = true)
public class Student {
@Id
@Field(index = true, store = true, type = FieldType.Keyword)
private String sId;
@Field(index = true, store = true, type = FieldType.Keyword)
private String sName;
@Field(index = true, store = true, type = FieldType.Text, analyzer = “ik_smart”)
//Text可以分词 ik_smart=粗粒度分词 ik_max_word 为细粒度分词
private String sAddress;
@Field(index = false, store = true, type = FieldType.Integer)
private Integer sAge;
@Field(index = false, store = true, type = FieldType.Date, format = DateFormat.basic_date_time)
private Date sCreateTime;
@Field(index = false, store = true, type = FieldType.Object)
private Headmaster sHeadmaster;//班主任
@Field(index = true, store = false, type = FieldType.Keyword)
private String[] sCourseList; //数组类型 由数组中第一个非空值决定(这里数组和集合一个意思了)
@Field(index = true, store = false, type = FieldType.Keyword)
private List<String> sColorList; //集合类型 由数组中第一个非空值决定
@Field(index = true, store = false, type = FieldType.Nested)//嵌套类型list里泛型是object形式的或自定义对象
private List<Teacher> sTeacherList; //教所有科目的老师
}
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.*; import org.springframework.data.elasticsearch.core.completion.Completion;
import java.util.Date;
/*
科目老师
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = “teacher”, type = “_doc”, replicas = 1, shards = 1, createIndex = true)
public class Teacher {
//主键id
@Id
@Field(index = true, store = true, type = FieldType.Keyword)//index:设置通过这个字段是否可以进行搜索
private String tId;
//姓名
@Field(index = true, store = true, type = FieldType.Keyword)
private String tName;
//英文姓名
@Field(index = true, store = true, type = FieldType.Keyword)
private String tEnglishName;
//班级
@Field(index = true, store = true, type = FieldType.Keyword)
private String tClassName;
//地址
@Field(index = true, store = true, type = FieldType.Text, analyzer = “ik_smart”)
private String tAddress;
//至理名言
@Field(index = true, store = true, type = FieldType.Keyword)
private String tFamous;
//年龄
@Field(index = true, store = true, type = FieldType.Integer)
private Integer tAge;
//日期
@Field(index = true, store = true, type = FieldType.Date, format = DateFormat.basic_date_time)
private Date tCreateTime;
//定义关键词索引 要完成补全搜索,必须要用到特殊的数据类型completion,
//要汉字拼音都能补全,必须要使用自定义的ik+pinyin分词器
// maxInputLength:设置单个输入的长度,默认为50 UTF-16 代码点
@CompletionField(analyzer = “ik_smart”, searchAnalyzer = “ik_smart”, maxInputLength = 100)
private Completion completion;
public Teacher(String tId, String tName, String tEnglishName, String tClassName, String tAddress, Integer tAge, Date tCreateTime) {
this.tId = tId;
this.tName = tName;
this.tEnglishName = tEnglishName;
this.tClassName = tClassName;
this.tAddress = tAddress;
this.tAge = tAge;
this.tCreateTime = tCreateTime;
}
}
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; import java.util.Date; import java.util.List;
/*
班主任
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = “headmaster”, type = “_doc”, replicas = 1, shards = 1, createIndex = true)
public class Headmaster {
@Field(index = true, store = false, type = FieldType.Keyword)
private String hId;
@Field(index = true, store = false, type = FieldType.Keyword)
private String hName;
@Field(index = true, store = false, type = FieldType.Keyword)
private String hAddress;
@Field(index = false, store = false, type = FieldType.Integer)
private Integer hSalary;
@Field(index = false, store = false, type = FieldType.Keyword)
private List<String> hClass;
@Field(index = true, store = true, type = FieldType.Date, format = DateFormat.basic_date_time)
private Date hCreateTime;
}
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
- createIndex :新增索引(数据库)
- putMapping :新增映射(表结构)
- 一定要通过这种方法创建,否则创建出来的映射都是默认的
createIndex && putMapping 创建索引及映射测试
- 入参classType : 对象是要被ES注解所引用的的(基于@Field和@Documen注解属性 创建索引和映射)
import com.it.mhh.elasticsearch.service.ElasticsearchSaveService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 新增类
*/
@RestController
@RequestMapping(“/elasticSearch/save”)
public class ElasticsearchSaveController {
@Autowired
private ElasticsearchSaveService elasticsearchSaveService;
/**
}
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 新增
*/
public interface ElasticsearchSaveService {
/**
import com.it.mhh.elasticsearch.service.ElasticsearchSaveService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.stereotype.Service;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 新增
*/
@Service
public class ElasticsearchSaveServiceImp implements ElasticsearchSaveService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
- 根据入参对象 ES注解 创建索引及映射
import com.it.mhh.elasticsearch.controller.ElasticsearchSaveController;
import com.it.mhh.elasticsearch.entity.Headmaster;
import com.it.mhh.elasticsearch.entity.Student;
import com.it.mhh.elasticsearch.entity.Teacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.completion.Completion;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 测试
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSaveControllerTest {
@Autowired
private ElasticsearchSaveController elasticsearchSaveController;
/**
}
- save(Iterable entities):可传集合 批量添加(同种类对象集合)
- save(T entity) :单个对象
- save(T… entities) : 可变参
save 添加文档测试
- 入参entities :有重载 可传集合 单个 或者 可变参
import com.it.mhh.elasticsearch.service.ElasticsearchSaveService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 新增类
*/
@RestController
@RequestMapping(“/elasticSearch/save”)
public class ElasticsearchSaveController {
@Autowired
private ElasticsearchSaveService elasticsearchSaveService;
/**
/**
}
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 新增
*/
public interface ElasticsearchSaveService {
/**
/**
public <T> T save(T t);
}
import com.it.mhh.elasticsearch.service.ElasticsearchSaveService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.stereotype.Service;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 新增
*/
@Service
public class ElasticsearchSaveServiceImp implements ElasticsearchSaveService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
}
/**
@Override
public <T> T save(T t) {
return elasticsearchRestTemplate.save(t);
}
}
- 根据被ES注解引用的入参对象存入相应索引中
import com.it.mhh.elasticsearch.controller.ElasticsearchSaveController;
import com.it.mhh.elasticsearch.entity.Headmaster;
import com.it.mhh.elasticsearch.entity.Student;
import com.it.mhh.elasticsearch.entity.Teacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.completion.Completion;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 测试
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSaveControllerTest {
@Autowired
private ElasticsearchSaveController elasticsearchSaveController;
/**
@param entities : 入参对象(必须是被@Document注解注释的)【入参 可传单个 T,集合Iterable<T>,可变参 T… 】
@return T 反参对象
@explain : 添加文档
@Author Mhh
@Date 2021/12/27 10:55
*/
@Test
public void savesStudentES() {
Headmaster headmaster = new Headmaster(“1”, “小白主任”, “山东”, 7500, Arrays.asList(“一班”, “二班”), new Date());//班主任
List<String> colorList = new ArrayList<>();//颜色
colorList.add(“red”);
colorList.add(“white”);
colorList.add(“black”);
List<Teacher> teacherList = new ArrayList<>();//所有科目老师
teacherList.add(new Teacher(“2”, “小黑”, “black”, “一班”, “山东省济南市历下区”, 13, new Date()));
teacherList.add(new Teacher(“2”, “小蓝”, “blue”, “二班”, “山东省菏泽市单县”, 14, new Date()));
Student student = new Student(“1”, “mhh”, “济南”, 12, new Date(), headmaster, new String[]{“语文”, “数学”, “英语”}, colorList, teacherList);
Student save = elasticsearchSaveController.save(student);
System.out.println(save);
}
/**
@param entities : 入参对象(必须是被@Document注解注释的)【入参 可传单个 T,集合Iterable<T>,可变参 T… 】
@return T 反参对象
@explain : 添加文档
@Author Mhh
@Date 2021/12/27 10:55
*/
@Test
public void savesTeacherES() {
Teacher teacher = new Teacher(“1”, “小黑老师”, “black”, “一班”, “河北省保定市莲池区”, 14, new Date());
Teacher save = elasticsearchSaveController.save(teacher);
System.out.println(save);
}
/**
@param entities : 入参对象(必须是被@Document注解注释的)【入参 可传单个 T,集合Iterable<T>,可变参 T… 】
@return T 反参对象
@explain : 添加文档
@Author Mhh
@Date 2021/12/27 10:55
*/
@Test
public void savesTeacherListES() {
List<Teacher> teacherList = new ArrayList<>();
teacherList.add(new Teacher(“1”, “小黑老师”, “black”, “一班”, “山东省泰安市岱岳区”,“且随疾风前行,身后亦须留心”, 15, new Date(), new Completion(new String[]{“山东省泰安市岱岳区”})));
teacherList.add(new Teacher(“2”, “小白老师”, “white”, “二班”, “山东省济南市历下区”, “此剑之势,愈斩愈烈”,17, new Date(), new Completion(new String[]{“山东省济南市历下区”})));
teacherList.add(new Teacher(“3”, “小黄老师”, “yellow”, “二班”, “河北省保定市莲池区”, “荣耀存于心,而非流于形”,18, new Date(), new Completion(new String[]{“河北省保定市莲池区”})));
teacherList.add(new Teacher(“5”, “小蓝教授”, “blue”, “二班”, “山东省济南市高新区”, “吾之初心,永世不忘”,21, new Date(), new Completion(new String[]{“山东省济南市高新区”})));
teacherList.add(new Teacher(“4”, “小绿教授”, “green”, “一班”, “山西省太原市杏花岭区”, “有些失误无法犯两次”,24, new Date(), new Completion(new String[]{“山西省太原市杏花岭区”})));
Iterable<Teacher> save = elasticsearchSaveController.save(teacherList);
System.out.println(save);
}
}
- deleteIndex :删除索引(数据库)
deleteIndex 删除索引测试
- 入参classType : 删除的哪个索引(从传入对象中的@Document注解中indexName属性获取)
import com.it.mhh.elasticsearch.service.ElasticsearchDeleteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 删除类
*/
@RestController
@RequestMapping(“/elasticSearch/delete”)
public class ElasticsearchDeleteController {
@Autowired
private ElasticsearchDeleteService elasticsearchDeleteService;
/**
import org.springframework.data.elasticsearch.core.query.Query;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchDeleteService {
/**
import com.it.mhh.elasticsearch.service.ElasticsearchDeleteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.stereotype.Service;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchDeleteServiceImp implements ElasticsearchDeleteService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
public Boolean deleteIndex(Class<?> clazz) {
return elasticsearchRestTemplate.deleteIndex(clazz);
}
}
- 根据入参对象 ES@Document注解中indexName属性值删除索引
- 如果删除的 索引名不存在 返回false
import com.it.mhh.elasticsearch.controller.ElasticsearchDeleteController; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermsQueryBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.test.context.junit4.SpringRunner;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchDeleteControllerTest {
@Autowired
private ElasticsearchDeleteController elasticsearchDeleteController;
/**
@Test
public void deleteIndex() {
Boolean aBoolean = elasticsearchDeleteController.deleteIndex(Teacher.class);
System.err.println(aBoolean);
}
}
- delete(Object entity):通过入参对象主键删除
- delete(String id, Class<?> entityType) id表示要删除的主键,entityType表示 被@Document注解且有indexName属性值(indexName属性值为索引名)
delete(通过主键删除) 删除文档测试
- 两种入参形式 都是通过主键id删除,即使入参对象其它属性设置了值也是通过主键id删除
import com.it.mhh.elasticsearch.service.ElasticsearchDeleteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 删除类
*/
@RestController
@RequestMapping(“/elasticSearch/delete”)
public class ElasticsearchDeleteController {
@Autowired
private ElasticsearchDeleteService elasticsearchDeleteService;
/**
/**
}
import org.springframework.data.elasticsearch.core.query.Query;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchDeleteService {
/**
/**
public Boolean delete(Object entity);
}
import com.it.mhh.elasticsearch.service.ElasticsearchDeleteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.stereotype.Service;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchDeleteServiceImp implements ElasticsearchDeleteService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
/**
- 根据入参对象 ES@Document注解中indexName属性值查询删除哪哥索引中文档
- 如果删除的指定索引名中的文档 索引名不存在 报错
- 即使删除的文档不存在 ES依旧返回删除的主键
import com.it.mhh.elasticsearch.controller.ElasticsearchDeleteController; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermsQueryBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.test.context.junit4.SpringRunner;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchDeleteControllerTest {
@Autowired
private ElasticsearchDeleteController elasticsearchDeleteController;
/**
- delete(Query query, Class<?> clazz, IndexCoordinates index)
query:查询语法 包含(term查询、terms查询、match查询、范围查询、模糊查询…)
clazz: 来指定查询字段和结果后返回的实例对象
indx: 指定索引
delete(删除通过query所搜索到的所有数据) 删除文档测试
- 删除掉被query所搜索到的所有数据。
import com.it.mhh.elasticsearch.service.ElasticsearchDeleteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 删除类
*/
@RestController
@RequestMapping(“/elasticSearch/delete”)
public class ElasticsearchDeleteController {
@Autowired
private ElasticsearchDeleteService elasticsearchDeleteService;
/**
@PostMapping(“deleteByQuery”)
public Boolean delete(Query query, Class<?> entity) {
return elasticsearchDeleteService.delete(query, entity);
}
}
import org.springframework.data.elasticsearch.core.query.Query;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchDeleteService {
/**
public Boolean delete(Query query, Class<?> entity);
}
import com.it.mhh.elasticsearch.service.ElasticsearchDeleteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.stereotype.Service;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchDeleteServiceImp implements ElasticsearchDeleteService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
public Boolean delete(Query query, Class<?> entity) {
elasticsearchRestTemplate.delete(query, entity, elasticsearchRestTemplate.getIndexCoordinatesFor(entity));
return true;
}
}
- 通过match词条分词 取并集查询后 结果被删除
- ik_smart: 粗粒度分词 山东省济南市 被分为(“山东省”,“济南市”)
- 只要tAddress字段里包含 山东省和济南市 就会被查询到 然后被删除
import com.it.mhh.elasticsearch.controller.ElasticsearchDeleteController; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermsQueryBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.test.context.junit4.SpringRunner;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchDeleteControllerTest {
@Autowired
private ElasticsearchDeleteController elasticsearchDeleteController;
/**
- update(UpdateQuery query, IndexCoordinates index)
query:要修改参数的主键及修改值
index:指定索引- bulkUpdate(
List<UpdateQuery>
queries, IndexCoordinates index):批量修改
update 修改文档(根据主键修改)测试
- id: 表示要修改哪个文档(_id)
- object:要修改的值对象(这里的对象最终都会转成Map)注意 这里属性值如果是空或null的话 那么也会被更新
用的 hutool-core转map(版本高一些,低版本的 如果传的就是Map类型,在map转map时会报错 – 5.7.13)- classType : 修改的哪个索引实体 (主要是获取对象里面@Document注解indexName属性获取修改哪个索引里的数据
import com.it.mhh.elasticsearch.service.ElasticsearchUpdateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 修改类
*/
@RestController
@RequestMapping(“/elasticSearch/update”)
public class ElasticsearchUpdateController {
@Autowired
private ElasticsearchUpdateService elasticsearchUpdateService;
/**
@PostMapping(“update”)
public Boolean update(@RequestParam(“id”) String id, @RequestBody Object object, Class<?> classType) {
return elasticsearchUpdateService.update(id, object, classType);
}
}
/** * @创建人: Mhh * @创建时间: 2021/12/10 * @描述: 修改 */ public interface ElasticsearchUpdateService {/** * @param id : 主键id(es里的_id) * @param object : 要修改的数据(这里的值都会转成Map)注意 这里入参属性值如果是空或null的话 那么也会被更新 * // 用的 hutool-core转map(版本高一些,低版本的 如果传的就是Map类型,在map转map时会报错 -- 5.7.13) * @param classType : 修改的哪个索引实体 (主要是获取对象里面@Document注解indexName属性获取修改哪个索引里的数据 * @return java.lang.Boolean * @explain : 根据主键修改数据 * @Author Mhh * @Date 2021/12/27 11:16 */ Boolean update(String id, Object object, Class<?> classType);
}
import cn.hutool.core.bean.BeanUtil; import com.it.mhh.elasticsearch.service.ElasticsearchUpdateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.UpdateQuery; import org.springframework.data.elasticsearch.core.query.UpdateResponse; import org.springframework.stereotype.Service;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 修改
*/
@Service
public class ElasticsearchUpdateServiceImp implements ElasticsearchUpdateService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
@Override
public Boolean update(String id, Object object, Class<?> classType) {
UpdateQuery.Builder builder = UpdateQuery.builder(id)
.withDocument(org.springframework.data.elasticsearch.core.document.Document.from(BeanUtil.beanToMap(object)));
IndexCoordinates of = IndexCoordinates.of(classType.getAnnotation(Document.class).indexName());
UpdateResponse update = elasticsearchRestTemplate.update(builder.build(), of);
return true;
}
}
- 据主键id修改(这里如果修改时没找到主键id就会报错)
- 这里可以复用es的插入(存入时es里已经有此主键就是修改,没有就是新增)
- 注意属性值是否为null值 es不会分辨 只会更新(即使是null值也会更新)
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.controller.ElasticsearchUpdateController; import com.it.mhh.elasticsearch.entity.Teacher; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchUpdateControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;
@Autowired
private ElasticsearchUpdateController elasticsearchUpdateController;
/*
@Test
public void updateByIdES() {
Teacher teacher = elasticsearchSelectController.get(“2”, Teacher.class);
//Teacher(tId=2, tName=小白老师, tEnglishName=white, tClassName=二班, tAddress=山东省济南市历下区, tFamous=此剑之势,愈斩愈烈, tAge=17, tCreateTime=Wed Dec 29 14:20:50 CST 2021, completion=org.springframework.data.elasticsearch.core.completion.Completion@6cd2cb2)
System.err.println(teacher);
Map<String, Object> map = new HashMap<>();
map.put(“tName”, “mhh”);
map.put(“tAge”, 11);
map.put(“tAddress”,null);
Boolean aBoolean = elasticsearchUpdateController.update(“2”, map, Teacher.class);//这里建议用map形式的(主要是它不能像sql智能,校验null:如果传了字段是null,那么就会修改成为null)
System.err.println(aBoolean);//true
teacher = elasticsearchSelectController.get(“2”, Teacher.class);
//Teacher(tId=2, tName=mhh, tEnglishName=white, tClassName=二班, tAddress=null, tFamous=此剑之势,愈斩愈烈, tAge=11, tCreateTime=Wed Dec 29 14:20:50 CST 2021, completion=org.springframework.data.elasticsearch.core.completion.Completion@1d1cfe4)
System.err.println(teacher);
}
}
- get(String id, Class<?> clazz)
id:文档主键
clazz:类对象(用来获取这个对象中的@Document注解中indexName属性[表示查询那个索引])
get 查询文档(根据主键查询)测试
- 根据主键id查询数据
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
}
- 根据主键id查询结果
- 获取不到就是null
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
@Test
public void selectByIdES() {
Teacher teacher = elasticsearchSelectController.get(“2”, Teacher.class);
System.err.println(teacher);//Teacher(tId=2, tName=小白老师, tAge=17, tCreateTime=Sat Dec 04 10:43:34 CST 2021)
Student student = elasticsearchSelectController.get("1", Student.class);
System.err.println(student);
/*Student
(sId=1, sName=mhh, sAddress=济南, sAge=12, sCreateTime=Sat Dec 04 10:34:14 CST 2021,
sHeadmaster=Headmaster(hId=1, hName=小白主任, hAddress=山东, hSalary=7500, hClass=[一班, 二班], hCreateTime=Sat Dec 04 10:34:14 CST 2021),
sCourseList=[语文, 数学, 英语],
sColorList=[red, white, black],
sTeacherList=[Teacher(tId=2, tName=小黑, tAge=13, tCreateTime=Sat Dec 04 10:34:14 CST 2021), Teacher(tId=2, tName=小蓝, tAge=14, tCreateTime=Sat Dec 04 10:34:14 CST 2021)])*/
Student nullStudent = elasticsearchSelectController.get("11", Student.class);//测试没有此主键的查询
System.err.println(nullStudent); // null
}
}
- exists(String id, Class<?> clazz)
id:文档主键
clazz:类对象(用来获取这个对象中的@Document注解中indexName属性[表示查询那个索引])- exists(String id, IndexCoordinates index)
id:文档主键
index:指定索引
exists 查询文档(判断主键id是否存在)测试
- 根据主键id查询索引中是否存在此文档
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
@PostMapping(“exists”)
public Boolean exists(String id, Class<?> classType) {
return elasticsearchSelectService.exists(id, classType);
}
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
public Boolean exists(String id, Class<?> classType);
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
@Override
public Boolean exists(String id, Class<?> classType) {
return elasticsearchRestTemplate.exists(id, classType);
}
}
- 根据主键id判断此索引中是否有此文档(有返回true,没有返回false)
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
}
- queryForPage(Query query, Class<?> clazz, IndexCoordinates index)
query:查询语法 包含(term查询、terms查询、match查询、范围查询、模糊查询…)
clazz:类对象(用来获取这个对象中的@Document注解中indexName属性[表示查询那个索引])
index:指定索引
queryForPage 查询文档(分页查询)测试
- NativeSearchQueryBuilder withPageable(Pageable pageable)
- 构建Pageable 对象 进行分页
- 添加分页信息 不设置 默认10条(用查询条件构建器)
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
@param pageNum : 分页查询的页数
@param pageSize : 分页查询返回的每页个数
@param key : 查询es的字段名
@param value : 要查询字段名中的值
@param classType : 返回的类类型
@return List<T></T>
@throws
@Author Mhh
@Date 2021/12/9 10:36
*/
public <T> List<T> selectFindPage(Integer pageNum, Integer pageSize, String key, String value, Class<T> classType) {
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(key, value).analyzer(“ik_smart”);//分词查询(这里指定分词只是分传过来的参数)[它是要和es索引里的分词后数据一一对应才能返回]
/例子:前台传了 (山东省:粗粒度分为山东省 细粒度分为:山东省,山东,省)
es索引库里(山东省济南市 粗粒度分为 山东省,济南市 细粒度分为:山东省,山东,省,济南市,济南,市)
只有当前台分的词和后台分的词能有一个匹配上就可以/
nativeSearchQueryBuilder.withQuery(matchQueryBuilder);
nativeSearchQueryBuilder.withPageable(PageRequest.of(pageNum == null || pageNum == 0 ? 0 : pageNum - 1, pageSize));
//4.构建查询对象
NativeSearchQuery query = nativeSearchQueryBuilder.build();
IndexCoordinates of = IndexCoordinates.of(classType.getAnnotation(Document.class).indexName());
AggregatedPage<T> page = elasticsearchRestTemplate.queryForPage(query, classType, of);
long totalElements = page.getTotalElements(); // 总记录数
int totalPages = page.getTotalPages(); // 总页数
int pageNumber = page.getPageable().getPageNumber(); // 当前页号
List<T> beanList = page.toList(); // 当前页数据集
Set<T> beanSet = page.toSet(); // 当前页数据集
return page.getContent();
}
}
- match分词查询 默认并集 ES索引中可看出符合的有两条,最终通过分页返回一条
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
}
- tokenizer: 选择粗细粒度分词器
- text: 需要分词的内容
AnalyzeRequest (根据入参内容返回分词后结果)测试
- 粗粒度分词器:ik_smart
粗粒度分词—北京天安门:[北京,天安门]- 细粒度分词器:ik_max_word
细粒度分词—北京天安门:[北京,天安门,天安,门]
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
}
- 通过查询分词后结果来校验 分词后查询文档返回的解果是否正确
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
}
- termsQuery(String name, Object… values)
- termsQuery(String name, String… values)
- termsQuery(String name, long… values)
- termsQuery(String name, int… values)
- …
name: 域(字段名)
values:一域多值, 查询的值
termQuery 查询文档(入参不分词,精确匹配)测试
- 入参不分词 会查询ES索引中文档(如果ES中索引文档被分词.只要匹配到词条就可以返回)
- values:一域多值(只要匹配到一个值就可以返回)
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
}
}
- term查询,查询text类型字段时,只有其中的单词相匹配都会查到,text字段会对数据进行分词
- term查询,查询keyword类型字段时,只有完全匹配才会查到,keyword字段不会对数据进行分词
- term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword 、numeric、date
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
}
- matchQuery(String name, Object text)
name: 域(字段名)
text: 要查询的入参值
match查询文档(会对查询条件进行分词)测试
- 会对查询条件进行分词。
- 将分词后的查询条件和词条进行等值匹配
- 默认取并集(OR)
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
Operator.AND(交集) 分的词全部满足的数据返回
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
Operator.AND(交集) 分的词全部满足的数据返回
public <T> List<T> matchQuery(Operator operator, String analyzer, String key, Class<T> classType, String text);
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
Operator.AND(交集) 分的词全部满足的数据返回
}
}
- match query知道分词器的存在。并且理解是如何被分词的
- Operator:
Operator.AND:交集
(表示被分词后的值都包含
在域(字段)中就返回此数据)
Operator.OR:并集
(表示被分词后的值只要有一个
被包含在域(字段)中就返回此数据)
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
Operator.AND(交集) 分的词全部满足的数据返回
}
- matchAllQuery():匹配所有文档的查询。
matchAllQuery 查询文档(查询此索引下所有文档)测试
- 查询指定索引下所有文档 (默认十条)
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
}
}
- 通过入参对象里面@Document注解indexName属性获取查询哪个索引
- 返回指定索引下所有文档
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
- wildcardQuery(String name, String query)
name: 域名(字段名称)
query: 通配符查询字符串
wildcardQuery 查询文档(模糊查询)测试
- wildcard查询:会对查询条件进行分词,还可以使用通配符
?
: 任意单个字符*
: 0个或多个字符
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/*
山?省: 山东省 或者 山西省 等等 ?包含一个字符
??省: 山东省 或者 吉林省 等等 ?包含一个字符
???: 你好啊 或者 早上好 等等 ?包含一个字符
济南*: 济南市 或者 济南市历下区..... *表示0个或多个字符
*剑 : 长虹剑 或者 冰魄长虹倚天剑.... *表示0个或多个字符
/**
@PostMapping(“wildcardQuery”)
public <T> List<T> wildcardQuery(String key, String value, Class<T> classType) {
return elasticsearchSelectService.wildcardQuery(key, value, classType);
}
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/*
山?省: 山东省 或者 山西省 等等 ?包含一个字符
??省: 山东省 或者 吉林省 等等 ?包含一个字符
???: 你好啊 或者 早上好 等等 ?包含一个字符
济南*: 济南市 或者 济南市历下区..... *表示0个或多个字符
*剑 : 长虹剑 或者 冰魄长虹倚天剑.... *表示0个或多个字符
/**
public <T> List<T> wildcardQuery(String key, String value, Class<T> classType);
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/*
山?省: 山东省 或者 山西省 等等 ?包含一个字符
??省: 山东省 或者 吉林省 等等 ?包含一个字符
???: 你好啊 或者 早上好 等等 ?包含一个字符
济南*: 济南市 或者 济南市历下区..... *表示0个或多个字符
*剑 : 长虹剑 或者 冰魄长虹倚天剑.... *表示0个或多个字符
/**
public <T> List<T> wildcardQuery(String key, String value, Class<T> classType) {
//查询条件(词条查询:对应ES query里的wildcard)
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery(key, value);
//创建查询条件构建器SearchSourceBuilder(对应ES外面的大括号)
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(wildcardQueryBuilder);
//查询,获取查询结果
SearchHits<T> search = elasticsearchRestTemplate.search(nativeSearchQuery, classType);
//获取总记录数
long totalHits = search.getTotalHits();
//获取值返回
return search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
}
}
"*华*"
包含华字的"华*"
华字后边零个或多个字符"华?"
华字后边一个字符"*华"
或"?华"
会引发全表(全索引)扫描 注意效率问题
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
@Test
public void wildcardQuery() {
List<Teacher> teacherList = elasticsearchSelectController.wildcardQuery(
“tAddress”,
“山?省”,
Teacher.class);
teacherList.forEach(System.err::println);
}
}
- prefixQuery(String name, String prefix)
name: 域名(字段名称)
prefix: 前缀值查询
prefixQuery 查询文档(前缀查询)测试
- 前缀查询 对keyword类型支持比较好
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
@PostMapping(“prefixQuery”)
public <T> List<T> prefixQuery(String key, String value, Class<T> classType) {
return elasticsearchSelectService.prefixQuery(key, value, classType);
}
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
@param key : es里索引的域(字段名)
@param value : 查询的值
@param classType : 返回的list里的对象并且通过对象里面@Document注解indexName属性获取查询哪个索引
@return java.util.List<T>
@explain : prefixQuery 前缀查询 对keyword类型支持比较好(text也能用:索引库字段分词后 分的词前缀要是能匹配也是可以返回此数据)
@Author Mhh
@Date 2021/12/10 16:49
*/
public <T> List<T> prefixQuery(String key, String value, Class<T> classType) {
//查询条件(词条查询:对应ES query里的prefixQuery)
PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery(key, value);
//创建查询条件构建器SearchSourceBuilder(对应ES外面的大括号)
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(prefixQueryBuilder);
//查询,获取查询结果
SearchHits<T> search = elasticsearchRestTemplate.search(nativeSearchQuery, classType);
//获取总记录数
long totalHits = search.getTotalHits();
//获取值返回
return search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
}
}
- text也能用:索引库字段分词后 分的词前缀要是能匹配也是可以返回此数据
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
- regexpQuery(String name, String regexp)
name: 域名(字段名称)
regexp: 正则表达式
regexpQuery 查询文档(正则表达式查询)测试
- 匹配包含具有指定正则表达式的术语的文档的查询
- 菜鸟教程-正则表达式: 正则表达式 – 语法 | 菜鸟教程.
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
- 正则查询取决于正则表达式的效率
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
}
- rangeQuery(String name).from(Object from,boolean includeLower).to(Object to,boolean includeLower)
name: 域名(字段名称)
from: 范围查询的from部分, Null 表示无界
includeLower:是否包含 默认为true[包含]
to: 范围查询的to部分, Null 表示无界
includeLower:是否包含 默认为true[包含]
rangeQuery 查询文档(范围查询) 测试
- 支持范围查询的字段类型有:数值、日期、地址的IP范围。
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
// 如果from==null 那么就是<=to的
// 如果to==null 那么就是 >=from的
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
@param name :es里索引的域(字段名)
@param from :范围查询值 1 from(Object from, boolean includeLower) includeLower:是否包含 默认为true[包含]
// 如果from==null 那么就是<=to的
@param to :范围查询值 2 to(Object to, boolean includeUpper) includeLower:是否包含 默认为true[包含]
// 如果to==null 那么就是 >=from的
@param classType :返回的list里的对象并且通过对象里面@Document注解indexName属性获取查询哪个索引
@return java.util.List<T>
@explain : rangeQuery 范围查询
@Author Mhh
@Date 2021/12/12 18:13
*/
public <T> List<T> rangeQuery(String name, Integer from, Integer to, Class<T> classType) {
//查询条件(词条查询:对应ES query里的rangeQuery)
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(name).from(from).to(to);
//创建查询条件构建器SearchSourceBuilder(对应ES外面的大括号)
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(rangeQueryBuilder);
//查询,获取查询结果
SearchHits<T> search = elasticsearchRestTemplate.search(nativeSearchQuery, classType);
//获取总记录数
long totalHits = search.getTotalHits();
//获取值返回
return search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
}
}
includeLower=true;(是否包含 默认为true[包含])
- 如果from==null 那么就是<=to的
- 如果to==null 那么就是 >=from的
- 如果 from!=null && to!=null 那么就是from>=to的
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
// 如果from==null 那么就是<=to的
// 如果to==null 那么就是 >=from的
}
- addSort(Sort sort): 添加排序
by(Sort.Direction direction, String… properties)
direction : 排序 Sort.Direction.ASC:升序 || Sort.Direction.DESC:降序
properties : 域名(可变参数,可传1至多个)
Sort 查询文档 (排序) 测试
- 在排序的过程中,只能使用可排序的属性进行排序(数字、日期)
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/**
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/**
}
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.*; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.*; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.suggest.*; import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@Service
public class ElasticsearchSelectServiceImp implements ElasticsearchSelectService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
}
- ES支持多级排序—sort : 将查询结果首先按第一个自定义条件排序,当第一个 sort 值完全相同时,再按照第二个条件进行排序,以此类推。
import com.it.mhh.elasticsearch.controller.ElasticsearchSelectController; import com.it.mhh.elasticsearch.entity.Student; import com.it.mhh.elasticsearch.entity.Teacher; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchSelectControllerTest {
@Autowired
private ElasticsearchSelectController elasticsearchSelectController;//es的查询
/**
}
- queryStringQuery(String queryString).fields(Map<String, Float> fields).analyzer(String analyzer).defaultOperator(Operator defaultOperator)
queryString : 要运行的查询字符串
fields : 添加多个域(字段)以及针对特定权重进行查询。
analyzer : 选择分词器(对查询的只进行分词)
defaultOperator : Operator.OR 并集 || Operator.AND 交集
queryStringQuery 查询文档 (多条件查询 一值多域) 测试
- queryString 多条件查询
- 会对查询条件进行分词。
- 将分词后的查询条件和词条进行等值匹配
- 默认取并集(OR)
- 可以指定多个查询字段
- query_string:识别query中的连接符(or 、and)
import com.it.mhh.elasticsearch.service.ElasticsearchSelectService; import org.elasticsearch.client.indices.AnalyzeResponse; import org.elasticsearch.index.query.Operator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询类
*/
@RestController
@RequestMapping(“/elasticSearch/select”)
public class ElasticsearchSelectController {
@Autowired
private ElasticsearchSelectService elasticsearchSelectService;
/*
多域查询的交并集理解:
OR: 只要有一个域中包含入参value被分词后的"一个值"时就返回
AND: 只要有一个域中包含入参value被分词后的"所有值"时返回
boost: 参数被用来提升一个语句的相对权重( boost 值大于 1 )或降低相对权重( boost 值处于 0 到 1 之间),但是这种提升或降低并不是线性的,换句话说,如果一个 boost 值为 2 ,并不能获得两倍的评分 _score 。
Operator.AND(交集) 分的词全部满足的数据返回
@PostMapping(“queryStringQuery”)
public <T> List<T> queryStringQuery(Map<String, Float> fields, String queryString, String analyzer, Operator operator, Class<T> classType) {
return elasticsearchSelectService.queryStringQuery(fields, queryString, analyzer, operator, classType);
}
}
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.Operator;
import org.springframework.data.domain.Sort;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@创建人: Mhh
@创建时间: 2021/12/10
@描述: 查询
*/
public interface ElasticsearchSelectService {
/*
多域查询的交并集理解:
OR: 只要有一个域中包含入参value被分词后的"一个值"时就返回
AND: 只要有一个域中包含入参value被分词后的"所有值"时返回
boost: 参数被用来提升一个语句的相对权重( boost 值大于 1 )或降低相对权重( boost 值处于 0 到 1 之间),但是这种提升或降低并不是线性的,换句话说,如果一个 boos
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。