文章目录

  • 10、集成Spring Boot
    • 10.1、入门环境搭建
    • 10.2、新建索引
    • 10.3、删除索引
    • 10.4、查找索引
    • 10.5、新建文档
    • 10.6、删除文档
    • 10.7、修改文档
    • 10.8、查找文档
    • 10.9、批量插入
    • 10.10、复杂查询
  • 11、项目实战
    • 11.1、环境搭建
    • 11.2、爬虫数据
    • 11.3、数据展示
    • 11.4、高亮显示

该篇博文根据B站UP主遇见狂神说的课程——【狂神说Java】ElasticSearch7.6.x最新完整教程通俗易懂整理而出


10、集成Spring Boot

10.1、入门环境搭建

1、创建Spring Boot项目

2、选择依赖时,需要勾选Spring Date Elasticsearch依赖

3、修改配置

由于我们本地安装的ES版本为7.6.1,IDEA中的环境需要与之匹配,所以修改对应的版本信息,配置如图示:

4、创建配置类

@Configuration
public class ElasticSearchClientConfig {@Beanpublic RestHighLevelClient restHighLevelClient() {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));return client;}
}

注意:ES的核心配置类为RestClientAutoConfiguration,但其本质是RestClientConfigurations

至此,基本的ES环境搭建已经完成,现在只需要 编写具体的业务测试类即可

10.2、新建索引

测试创建索引,

等同于直接使用命令:PUT mobian_test_api

测试代码:

@SpringBootTest
class DemoApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testvoid createIndex() throws IOException {// 1、创建索引请求CreateIndexRequest request = new CreateIndexRequest("mobian_test_api");// 2、客户端执行对应的请求IndicesClient ,请求后获得响应CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);System.out.println(createIndexResponse);}
}

测试结果:

使用Kibana查看我们创建的索引信息

10.3、删除索引

测试代码:

@SpringBootTest
class DemoApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic void deleteIndex() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("mobian_test_api2");AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);System.out.println(delete);}}

测试结果:

10.4、查找索引

判断对应索引是否存在

测试代码:

@SpringBootTest
class DemoApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic void getIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("mobian_test_api");boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);System.out.println("mobian_test_api 索引是否存在:" + exists);request = new GetIndexRequest("mobian_test_api3");exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);System.out.println("mobian_test_api3 索引是否存在:" + exists);}}

测试结果:

获取对应的索引信息

@SpringBootTest
class DemoApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic void findIndex() throws IOException {GetIndexRequest request = new GetIndexRequest("mobian_test_api");GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);System.out.println(getIndexResponse);System.out.println(getIndexResponse.getIndices());}
}

测试结果:

10.5、新建文档

由于涉及到对象和JSON字符串之间的转换,可以引入fastjson依赖

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.66</version>
</dependency>

测试代码:

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic void addDocument() throws IOException {// 1.创建对象User user = new User("mobian", 4);// 2.创建请求IndexRequest request = new IndexRequest("mobian_test_api");// 类比理解 put /mobian_test_api/_doc/1request.id("1");request.timeout("1s");// 3.将我们的数据以json字符串的格式放入请求中request.source(JSON.toJSONString(user), XContentType.JSON);// 4.客户端发送对应的请求,获取具体的响应结果IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);System.out.println(index.toString());System.out.println(index.status());}
}

测试结果:

10.6、删除文档

测试代码:

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 删除文档@Testpublic void deleteDocument() throws IOException {DeleteRequest request = new DeleteRequest("mobian_test_api", "1");DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);System.out.println(delete);System.out.println(delete.status());}}

测试结果:

10.7、修改文档

测试代码:

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 修改文档@Testpublic void updateDocument() throws IOException {// 构建新的对象UpdateRequest updateRequest = new UpdateRequest("mobian_test_api", "1");User user = new User("默辨", 18);updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);System.out.println(update);System.out.println(update.status());}
}

测试结果:

10.8、查找文档

判断对应的文档是否存在

测试代码:

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic void findDocument() throws IOException {GetRequest getRequest = new GetRequest("mobian_test_api", "1");// 设置不返回 _source的上下文,该处不设置不影响查询结果getRequest.fetchSourceContext(new FetchSourceContext(false));getRequest.storedFields("_none_");boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);System.out.println("mobian_test_api索引中是否存在文档为1的记录:" + exists);getRequest = new GetRequest("mobian_test_api", "3");exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);System.out.println("mobian_test_api索引中是否存在文档为3的记录:" + exists);}
}

测试结果:由于我们只添加了文档为1的记录,所以当记录的id为3时,查询状态对false。

查询具体的文档信息

测试代码:

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 查找对应的文档信息@Testpublic void getDocument() throws IOException {GetRequest getRequest = new GetRequest("mobian_test_api", "1");GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);// 想要查询什么信息,直接调用对应的方法即可System.out.println(documentFields.getSource());//返回的全部内容和使用命令返回的全部内容是一样的System.out.println(documentFields);}
}

测试结果:

总结步骤:

1、封装我们想要完成操作的对象
2、使用调用RestHighLevelClient类中对应操作的方法

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;}

10.9、批量插入

测试代码:

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 批量新增文档@Testpublic void batchDocument() throws IOException {BulkRequest bulkRequest = new BulkRequest();bulkRequest.timeout("10s");ArrayList<User> users = new ArrayList<>();users.add(new User("mobian1", 22));users.add(new User("mobian2", 22));users.add(new User("mobian3", 22));users.add(new User("mobian4", 22));users.add(new User("mobian5", 22));users.add(new User("mobian6", 22));// 批量处理for (int i = 0; i < users.size(); i++) {// 如果想要完成修改操作,只需要修改该步即可bulkRequest.add(new IndexRequest("mobian_test_api").id("" + (i + 1)).source(JSON.toJSONString(users.get(i)), XContentType.JSON));}BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);System.out.println(bulk);System.out.println(bulk.status());}
}

测试结果:

10.10、复杂查询

测试代码:

@SpringBootTest
public class DocumentApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 复杂条件查询@Testpublic void searchDocument() throws IOException {SearchRequest searchRequest = new SearchRequest("mobian_test_api");// 构建搜索条件,这里有点类似于MyBatis-Plus中的wrapper条件构造器SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();// 精确匹配TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "22");// 匹配所有// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();sourceBuilder.query(termQueryBuilder);sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));// 高亮// sourceBuilder.highlighter();// 分页// sourceBuilder.from();// sourceBuilder.size();searchRequest.source(sourceBuilder);SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(JSON.toJSONString(search.getHits()));for (SearchHit documentFields : search.getHits().getHits()) {System.out.println(documentFields.getSourceAsMap());}}
}

测试结果:


11、项目实战

该部分主要分为三部分,爬取指定界面的数据,展示爬取出来的数据,高亮展示爬取出来的数据。

此处的案例:爬取京东商城以python为关键字的数据信息(标题、价格、图片),再获取ES索引库中对应的数据信息,并且完成关键字(python)的高亮显示。

11.1、环境搭建

1、创建一个Spring Boot项目

2、引入主要的maven依赖

<dependencies><!-- ES组件 --><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><!-- thymeleaf组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- 热部署组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!-- thymeleaf组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><!-- lombok组件 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- 测试组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- fastjson组件 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.66</version></dependency><!-- jsoup组件 --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.13.1</version></dependency>
</dependencies>

11.2、爬虫数据

1、创建实体类,与我们的目标数据相对应

Content.java

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content {private String img;private String title;private String price;
}

2、创建一个解析工具类,

HtmlParseUtil.java

@Component
public class HtmlParseUtil {public static List<Content> parseJD(String keyWord) throws IOException {// 指定对应的路径,这里去爬取京东商城界面String url = "https://search.jd.com/Search?keyword=" + keyWord;// 解析对应网络地址的网页信息Document document = Jsoup.parse(new URL(url), 30000);// 获取对应的id节点Element element = document.getElementById("J_goodsList");// 获取对应节点内的对应tagElements lis = element.getElementsByTag("li");ArrayList<Content> goodsList = new ArrayList<>();// 遍历每一个tag,获取tag标签内部具体的信息for (Element li : lis) {// 京东书籍界面,设置了懒加载,所以需要设置对应的懒加载标签String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");String price = li.getElementsByClass("p-price").eq(0).text();String title = li.getElementsByClass("p-name").eq(0).text();goodsList.add(new Content(img, title, price));}return goodsList;}
}

3、编写service层业务方法

ContentService.java

@Service
public class ContentService {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 解析网页数据,并保存到es数据库中public Boolean parseContent(String keyWord) throws IOException {List<Content> contents = HtmlParseUtil.parseJD(keyWord);BulkRequest bulkRequest = new BulkRequest();bulkRequest.timeout("30s");for (int i = 0; i < contents.size(); i++) {// 使用默认的id生成策略bulkRequest.add(new IndexRequest("jd_goods").source(JSON.toJSONString(contents.get(i)), XContentType.JSON));}BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);System.out.println(bulk);System.out.println(bulk.status());// 失败返回true,成功返回falsereturn !bulk.hasFailures();}}

4、编写controller层接口

ContentController.java

@RestController
public class ContentController {@Autowiredprivate ContentService contentService;@GetMapping("/parse/{keyWords}")public Boolean parseContent(@PathVariable("keyWords") String keyWords) throws IOException {return contentService.parseContent(keyWords);}
}

测试结果:

当我们在浏览器地址栏中输入:http://localhost:8080/parse/python时,会将京东商城界面中指定的数据信息(名字、价格、图片),添加到我们ES的jd_goods索引库中。并打印出我们的指定的返回结果。具体的数据信息,我们可以在ES库中查询

11.3、数据展示

1、编写service层业务方法

ContentService.java

@Service
public class ContentService {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 搜索es数据库中的信息public List<Map<String, Object>> searchPage(String keyWord, int pageNo, int pageSize) throws IOException {// 条件搜索SearchRequest searchRequest = new SearchRequest("jd_goods");SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();// 精准匹配TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyWord);sourceBuilder.query(termQueryBuilder);sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));// 分页sourceBuilder.from(pageNo);sourceBuilder.size(pageSize);// 执行搜索searchRequest.source(sourceBuilder);SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);// 解析结果ArrayList<Map<String, Object>> list = new ArrayList<>();for (SearchHit hit : search.getHits().getHits()) {list.add(hit.getSourceAsMap());}return list;}
}

2、编写controller层接口

ContentController.java

@RestController
public class ContentController {@Autowiredprivate ContentService contentService;@GetMapping("/search/{keyWord}/{pageNo}/{pageSize}")public List<Map<String, Object>> search(@PathVariable("keyWord") String keyWord,@PathVariable("pageNo") int pageNo,@PathVariable("pageSize") int pageSize) throws IOException {return contentService.searchPage(keyWord,pageNo,pageSize);}
}

测试结果:

在查询对应的数据前,一定要确保我们ES索引库中包含有与我们关键字对应的数据信息,具体的数据添加操作,请查看上一小节。此处的测试为,输入关键字python,界面展示出我们ES索引库中对应的与python相关的书记的名字、价格、图片地址。

11.4、高亮显示

1、编写service层业务方法

ContentService.java

@Service
public class ContentService {@Autowiredprivate RestHighLevelClient restHighLevelClient;// 高亮搜索public List<Map<String, Object>> searchHighLightPage(String keyWord, int pageNo, int pageSize) throws IOException {// 条件搜索SearchRequest searchRequest = new SearchRequest("jd_goods");SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();// 精准匹配TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyWord);sourceBuilder.query(termQueryBuilder);sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));// 分页sourceBuilder.from(pageNo);sourceBuilder.size(pageSize);// 高亮HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field("title");highlightBuilder.requireFieldMatch(false);highlightBuilder.preTags("<span style='color:red'>");highlightBuilder.postTags("</span>");sourceBuilder.highlighter(highlightBuilder);// 执行搜索searchRequest.source(sourceBuilder);SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);ArrayList<Map<String, Object>> list = new ArrayList<>();// 解析结果for (SearchHit hit : search.getHits().getHits()) {Map<String, HighlightField> highlightFields = hit.getHighlightFields();HighlightField title = highlightFields.get("title");Map<String, Object> sourceAsMap = hit.getSourceAsMap();if (title != null) {Text[] fragments = title.fragments();String newTitle = "";for (Text text : fragments) {newTitle += text;}sourceAsMap.put("title", newTitle);}list.add(hit.getSourceAsMap());}return list;}
}

2、编写controller层接口

ContentController.java

@RestController
public class ContentController {@Autowiredprivate ContentService contentService;@GetMapping("/search2/{keyWord}/{pageNo}/{pageSize}")public List<Map<String, Object>> searchHighLight(@PathVariable("keyWord") String keyWord,@PathVariable("pageNo") int pageNo,@PathVariable("pageSize") int pageSize) throws IOException {return contentService.searchHighLightPage(keyWord,pageNo,pageSize);}
}

测试结果:

service层业务代码的逻辑为,对搜索的关键字进行高亮处理,所以理论上的测试结果应该在title属性的关键字上添加对应的span标签,如下图的测试情况:

Elasticsearch(三)相关推荐

  1. ElasticSearch三master节点集群状态查看方法

    ES集群的状态实际上也是使用RESTful的接口,一般用的是GET方法 http://[主机IP]:[ES端口] 通常我们启动服务器之后,就可以通过这个简单的方式来验证服务器是否启动成功. 从下面返加 ...

  2. Elasticsearch(三) Python 使用 elasticsearch 的基本操作

    参考文章:https://cuiqingcai.com/6214.html 一. python 安装 elasticsearch标准库 1. pip install elasticsearch 2. ...

  3. Elasticsearch(三)--Metric(指标)

    说明:该博客对应的Elasticsearch 的版本为7.8.0;测试工具为postman 1,概念 理解四个关键字:Metric(指标),Bucketing(桶),Matrix(矩阵),Pipeli ...

  4. 谷粒商城微服务分布式高级篇ElasticSearch三——Dcoker安装ES及ES检索语法

    文章目录 Docker安装ES&kibban Elasticsearch安装 kibban安装 IK分词器安装 Nginx安装 检索 1.查看es中有哪些索引 2.增加一个索引(库) 3.删除 ...

  5. [ElasticSearch] 三种方式进行ES检索评分控制

    一.概要 在使用ES进行搜索时,评分的控制是非常关键的. 而如何对搜索评分进行定制化控制,让其更符合我们想要的评分结果呢? 对boost参数进行控制 通过rescore对查询结果的评分进行二次控制 使 ...

  6. ElasticSearch三种分页对比

    1.from-size浅分页 from表示初始偏移量,默认为0.size表示单页返回最大文档条数,默认为10.假设我们在有5个主分片的索引中搜索,查询第一页数据,即前10条数据,那么es会从每个分片中 ...

  7. Elasticsearch 三种分页方式

    欢迎关注方志朋的博客,回复"666"获面试宝典 from + size 浅分页 "浅"分页可以理解为简单意义上的分页.它的原理很简单,就是查询前20条数据,然后 ...

  8. Elasticsearch(三) 使用kibana 操作ES

    文档中包含语句 1,索引(新增 查询 删除) 2, mapping 创建 3,文档(新增,修改,删除,批量新增) 4,文档查询(基本查询,高级查询,分页,高亮,排序) 1,使用kibana 新增 查询 ...

  9. linux下载python的es库,Elasticsearch py客户端库安装及使用方法解析

    一.介绍 elasticsearch-py是一个官方提供的low-level的elasticsearch python客户端库.为什么说它是一个low-level的客户端库呢?因为它只是对elasti ...

  10. ElasticSearch优化系列一:集群节点规划

    节点职责单一,各司其职 elasticSearch的配置文件中有2个参数:node.master和node.data.这两个参 数搭配使用时,能够帮助提供服务器性能. 数据节点node.master: ...

最新文章

  1. SGU 282 Isomorphism
  2. CkEditor批量上传图片(java)
  3. 第1章 计算机系统概述小节
  4. CentOS安装和配置Rsync进行文件同步
  5. [JavaWeb-MySQL]MySQL登陆和退出
  6. 使用Spring Security的多租户应用程序的无状态会话
  7. 头部ct能检查出什么_【安全用药】做CT检查时应注意什么?
  8. 笔记:《幸福的方法》
  9. 最短路径问题(信息学奥赛一本通-T1342)
  10. 派单o2o全开源版 v11.6.0 全新UI版小程序模块
  11. 三星Galaxy Note 20系列将首发Exynos 992:性能超骁龙865
  12. 在手语世界里,健听人、数字人与听障人的交织
  13. 【动态规划】完全背包问题:构成m的最少个数
  14. latex 数学符号
  15. 自然资源部标准地图底图转矢量Shapefile并配准
  16. matlab对信号DTFT,【MATLAB】离散傅里叶变换DTFT和IDTFT
  17. PHP - 下载/传输远程服务器上的文件到本地服务器
  18. 计算机辅助设计学哪个软件,高校有必要进行计算机辅助设计软件的教学.pdf
  19. 100部未看过的电影【20181121更新】
  20. 网络协议分析(3):FTP

热门文章

  1. Git提交代码相关命令
  2. vue3的语法使用总结api
  3. matlab多图形相交,用MATLAB作出柱面x^2+y^2=4 和柱面x^2+z^2=4 相交的图形.
  4. 求和函数计算机语言,在 Excel 中,计算求和的函数是 ____。
  5. android 自定义set,Android使用AttributeSet自定义控件的方法
  6. Introduction to Computer Networking学习笔记(七):ICMP协议
  7. 一、传统应用带来的问题
  8. poj2054 Color a Tree
  9. 图解对比MySQL索引为什么要用B+树
  10. miniprogrampatch 提供 watch 和 computed 特性