欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Spring-Boot整合ElasticSearch的简单使用

时间:2023-05-04

ElasticSearch 是一个分布式搜索服务,提供 Restful API,底层基于 Lucene,采用多 shard(分片)的方式保证数据安全,并提供自动 resharding 的功能


ElasticSearch对比MYSQL 索引》数据库        类型》表文档》行属性》列DOCKER创建

// docker 创建 ES 容器// -e ES_JAVA_OPTS="-Xms256m -Xmx256m" 限制 ES 内存使用大小docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES 镜像 id

依赖

// spring boot默认使用springData ES模块进行操作 org.springframework.boot spring-boot-starter-data-elasticsearch

配置 

spring: elasticsearch: rest: uris: http://localhost:9200 data: elasticsearch: repositories: enabled: true client: reactive: endpoints: http://localhost:9200

实现

@AutowiredRestHighLevelClient restHighLevelClient ;public void saveOne()throws IOException{ IndexRequest indexRequest = new IndexRequest("city"); City city = new City(8,"武汉",0,1); indexRequest.id(city.getCid()+"");// 指定ID indexRequest.source(JSON.toJSonString(city), XContentType.JSON); // 转换为JSON格式 // 执行 restHighLevelClient.index(indexRequest , RequestOptions.DEFAULT);}public void saveAll() throws IOException{ ArrayList list = new ArrayList<>(); BulkRequest bulkRequest = new BulkRequest(); for (Object o : list) { bulkRequest.add( new IndexRequest("city") .id("") // 指定ID .source(JSON.toJSonString(o),XContentType.JSON) ); } restHighLevelClient.bulk(bulkRequest , RequestOptions.DEFAULT);}public void getCity() throws IOException { SearchRequest request = new SearchRequest("city"); SearchSourceBuilder bulider = new SearchSourceBuilder(); BoolQueryBuilder query = QueryBuilders.boolQuery(); // 组合查询 query.must(QueryBuilders.matchAllQuery()); // 查找全部 bulider.query(query); // 分页 bulider.from((pageNum-1)*pageSize); // 开始位置 bulider.size(pageSize); // 显示条数 // 排序 DESC 倒叙 ASC 升序 bulider.sort("age", SortOrder.DESC); request.source(bulider); SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT); SearchHit[] hits = search.getHits().getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); }}public void findCity() throws IOException { SearchRequest request = new SearchRequest("city"); SearchSourceBuilder bulider = new SearchSourceBuilder(); BoolQueryBuilder query = QueryBuilders.boolQuery(); // 模糊检索 query.must(QueryBuilders.matchQuery("name","zhang")); // 高亮 HighlightBuilder high = new HighlightBuilder(); high.field("name"); // 添加高亮的字段 high.preTags(""); // 起始位置 high.postTags(""); // 结束位置 bulider.highlighter(high); bulider.query(query); request.source(bulider); SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT); SearchHit[] hits = search.getHits().getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); System.out.println(sourceAsString); }}

Elasticsearch官网https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。