智能建设网站,微信商户平台,如何做背景不动的网站,有哪些做mg动画的素材网站说明#xff1a;es操作索引库、文档#xff0c;除了使用它们自带的命令外#xff08;参考#xff1a;http://t.csdn.cn/4zpmi#xff09;#xff0c;在IDEA中可以添加相关的依赖#xff0c;使用对应的API来操作。
准备工作
搭建一个SpringBoot项目#xff0c;DAO使用…说明es操作索引库、文档除了使用它们自带的命令外参考http://t.csdn.cn/4zpmi在IDEA中可以添加相关的依赖使用对应的API来操作。
准备工作
搭建一个SpringBoot项目DAO使用的MyBatis-Plus对数据库中的学生表进行操作。 pom.xml文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.10.RELEASE/versionrelativePath//parentgroupIdorg.hzy/groupIdartifactIdes_essay_demo/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetelasticsearch.version7.12.1/elasticsearch.version/propertiesdependencies!--spring web依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--mybatis-plus依赖--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.2/version/dependency!--数据库连接驱动--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependency!--lombok依赖--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency!--测试依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scopeexclusionsexclusiongroupIdorg.junit.vintage/groupIdartifactIdjunit-vintage-engine/artifactId/exclusion/exclusions/dependency!--FastJson--dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.71/version/dependency!--commons依赖--dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactId/dependency/dependencies
/project在此之上导入RestHighLevelClient依赖并指定版本不然会使用SpringBoot自带版本依靠此依赖实现对ES的一系列操作。 propertieselasticsearch.version7.12.1/elasticsearch.version/properties!--RestHighLevelClient依赖--dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactId/dependency编写与学生类相关的索引库DSL语句注意这里特意把学生类中的创建日期、入学日期合并成一个joinInfo字段后面添加文档时需要考虑到这点
PUT /student
{mappings: {properties: {id: {type: keyword},username:{type: keyword,copy_to: all},password:{type: keyword,index: false},name:{type: text,analyzer: ik_max_word,copy_to: all},gender:{type: keyword},image:{type: keyword},job:{type: integer},joinInfo:{type: keyword},updateTime:{type: keyword},all:{type: text,analyzer: ik_max_word}}}
}将上面索引库的DSL语句在IDEA中用一个常量来存储以便后面使用注意前面“PUT /student”需要去掉只需要最外层花括号内的信息即可
public class Constants {public static final String CREATE_INDEX_STRING {\n \mappings\: {\n \properties\: {\n \id\: {\n \type\: \keyword\\n },\n \n \username\:{\n \type\: \keyword\,\n \copy_to\: \all\\n },\n \n \password\:{\n \type\: \keyword\,\n \index\: false\n },\n \n \name\:{\n \type\: \text\,\n \analyzer\: \ik_max_word\,\n \copy_to\: \all\\n },\n \n \gender\:{\n \type\: \keyword\\n },\n \n \image\:{\n \type\: \keyword\\n },\n \n \job\:{\n \type\: \integer\\n },\n \t \n \t \joinInfo\:{\n \t\t\type\: \keyword\\n \t },\n \n \updateTime\:{\n \type\: \keyword\\n },\n \n \n \all\:{\n \type\: \text\,\n \analyzer\: \ik_max_word\\n }\n }\n }\n };
}
索引库index操作
为了后面操作方便把RestHighLevelClient的定义、创建、关闭放在方法外面以下操作都是在测试类中实现 /*** 定义连接*/private RestHighLevelClient client;/*** 初始化客户端*/BeforeEachpublic void init(){client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.126.128:9200)));}/*** 关闭客户端* throws IOException*/AfterEachpublic void close() throws IOException {client.close();}创建索引库 /*** 创建索引*/Testpublic void addIndex() throws IOException {// 1.创建请求CreateIndexRequest createIndexRequest new CreateIndexRequest(student);// 2.编写DSL语句createIndexRequest.source(CREATE_INDEX_STRING, XContentType.JSON);// 3.发起请求client.indices().create(createIndexRequest, RequestOptions.DEFAULT);}执行成功 到ikbana上看下索引库已经创建完成 获取索引库 /*** 获取索引库* throws IOException*/Testpublic void getIndex() throws IOException {// 1.创建请求GetIndexRequest getIndexRequest new GetIndexRequest(student);// 2.发起请求boolean exists client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);System.out.println(exists exists);}此方法返回结果为boolean类型仅可知该索引库是否存在 删除索引库 /*** 删除索引库* throws IOException*/Testpublic void deleteIndex() throws IOException {// 1. 创建请求DeleteIndexRequest deleteIndexRequest new DeleteIndexRequest(student);// 2. 发起请求client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);}删除索引库成功 再次获取索引库返回false 小结
索引库没有修改操作
文档document操作
再提一次文档操作等同于数据库的数据操作即类比数据的CRUD操作
新增文档
注意因为数据库中查询到的记录与ES的文档的字段并不是一一对应的需要再创建一个对象来进行拼凑
Student类
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;import java.io.Serializable;Data
TableName(tb_student)
public class Student implements Serializable {private Integer id;private String username;private String password;private String name;private Integer gender;private String image;private Integer job;private String entryDate;private String createTime;private String updateTime;
}StudentDoc类与ES文档一一对应
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;Data
NoArgsConstructor
public class StudentDoc implements Serializable {private Integer id;private String username;private String password;private String name;private Integer gender;private String image;private Integer job;private String joinInfo;private String updateTime;public StudentDoc(Student student) {this.id student.getId();this.username student.getUsername();this.password student.getPassword();this.name student.getName();this.gender student.getGender();this.image student.getImage();this.job student.getJob();this.joinInfo [创建日期 student.getCreateTime() ], [加入日期 student.getEntryDate() ];this.updateTime student.getUpdateTime();}
}/*** 新增文档数据来自数据库然后新增到ES*/Testpublic void addDoc() throws IOException {// 1.查询数据ID为1的记录Student student studentService.getById(1);// 1.1 拼凑文档StudentDoc studentDoc new StudentDoc(student);// 2.创建请求IndexRequest request new IndexRequest(student).id(student.getId().toString());// 3.编写DSL语句request.source(JSON.toJSONString(studentDoc),XContentType.JSON);// 4.发送请求client.index(request,RequestOptions.DEFAULT);}执行完成 查看kibana平台输入命令可以查到对应的文档说明新增文档成功 查询文档 /*** 查询文档*/Testpublic void getDoc() throws IOException {// 1.创建请求查询ID为1的文档GetRequest request new GetRequest(student).id(1);// 2.发送请求GetResponse response client.get(request, RequestOptions.DEFAULT);// 3.获取返回值String json response.getSourceAsString();// 4.解析返回值为java对象StudentDoc studentDoc JSON.parseObject(json, StudentDoc.class);System.out.println(studentDoc studentDoc);}查询完成 删除文档 /*** 删除文档*/Testpublic void delDoc() throws IOException {// 1.创建请求删除ID为1的文档DeleteRequest request new DeleteRequest(student).id(1);// 2.发起请求client.delete(request, RequestOptions.DEFAULT);}执行完成 再次查询结果为null注意并不会报错 更新文档
更新文档由两种方式第一种是全局更新其实也就是新增文档代码是一样的第二种是局部更新代码如下 /*** 更新文档方式二局部更新*/Testpublic void updateDoc() throws IOException {// 1.创建请求修改ID为1的文档UpdateRequest request new UpdateRequest(student,1);// 2.指定要修改的字段request.doc(name,徐志摩,username,xuzhimo);// 3.发送请求client.update(request,RequestOptions.DEFAULT);}执行完成 再次查找ID为1的文档可以看到文档已更新 批量导入文档
批量将数据库中的对象数据导入到ES上 /*** 批量导入文档数据从数据库中查询获取*/Testpublic void addDocs() throws IOException {// 1.获取所有学生的数据ListStudent students studentService.list();// 2.创建Bulk请求BulkRequest bulkRequest new BulkRequest();// 3.添加数据到Bulk中for (Student student : students) {// 3.1 创建Doc对象StudentDoc studentDoc new StudentDoc(student);// 3.2 创建新增文档请求并将Doc对象放入bulkRequest.add(new IndexRequest(student).id(studentDoc.getId().toString()).source(JSON.toJSONString(studentDoc),XContentType.JSON));}// 4.发送请求client.bulk(bulkRequest,RequestOptions.DEFAULT);}执行完成没有报错 随便查找两条记录可以看到都已经添加了进来 总结
对于索引库的操作请求是XxxIndexRequest(“索引库名”)创建是Create删除是Delete获取是Get发起请求方法名是exists
对于文档的操作请求是XxxRequest(“索引库名”,“ID”)新增是Index删除是Delete查询是Get修改是Update另外还有个批量的操作是BulkRequest