当前位置:
首页 >
RestClient 访问elasticsearch
发布时间:2023/12/18
31
豆豆
生活随笔
收集整理的这篇文章主要介绍了
RestClient 访问elasticsearch
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1。引入依赖
1)引入es的RestHighLevelClient依赖:
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency>2)因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
<properties><java.version>1.8</java.version><elasticsearch.version>7.12.1</elasticsearch.version> </properties>3)初始化RestHighLevelClient:
初始化的代码如下:
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.150.101:9200") ));2.创建索引库和删除索引库
存储的是新增索引库的语句
package cn.itcast.hotel;public class HotelConstants {public static final String MAPPING_TEMPLATE = "{\n" +" \"mappings\": {\n" +" \"properties\": {\n" +" \"id\": {\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"name\":{\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_max_word\",\n" +" \"copy_to\": \"all\"\n" +" },\n" +" \"address\":{\n" +" \"type\": \"keyword\",\n" +" \"index\": false\n" +" },\n" +" \"price\":{\n" +" \"type\": \"integer\"\n" +" },\n" +" \"score\":{\n" +" \"type\": \"integer\"\n" +" },\n" +" \"brand\":{\n" +" \"type\": \"keyword\",\n" +" \"copy_to\": \"all\"\n" +" },\n" +" \"city\":{\n" +" \"type\": \"keyword\",\n" +" \"copy_to\": \"all\"\n" +" },\n" +" \"starName\":{\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"business\":{\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"location\":{\n" +" \"type\": \"geo_point\"\n" +" },\n" +" \"pic\":{\n" +" \"type\": \"keyword\",\n" +" \"index\": false\n" +" },\n" +" \"all\":{\n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_max_word\"\n" +" }\n" +" }\n" +" }\n" +"}"; }新增和删除索引库操作
import org.apache.http.HttpHost; import org.apache.http.client.methods.HttpPost; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.common.xcontent.XContentType; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;import java.io.IOException;@SpringBootTest class HotelSuoYinTest {private RestHighLevelClient client;@BeforeEachvoid setup() {this.client =new RestHighLevelClient(RestClient.builder(HttpHost.create("http://101.43.94.150:9200")));}@AfterEachvoid shutdown() throws IOException {this.client.close();} // indices() 有这个,就是索引库的操作@Testvoid test() throws IOException { // 创建request对象CreateIndexRequest request = new CreateIndexRequest("hotel"); // 准备请求的参数 dsl语句request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON); // 发送请求client.indices().create(request, RequestOptions.DEFAULT);}@Testvoid deltest() throws IOException { // 创建request对象DeleteIndexRequest request = new DeleteIndexRequest("hotel"); // 发送请求client.indices().delete(request, RequestOptions.DEFAULT);} }判断索引库是不是存在
@Test void testExistsHotelIndex() throws IOException {// 1.创建Request对象GetIndexRequest request = new GetIndexRequest("hotel");// 2.发送请求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 3.输出System.err.println(exists ? "索引库已经存在!" : "索引库不存在!"); }索引库操作的基本步骤:
-
初始化RestHighLevelClient
-
创建XxxIndexRequest。XXX是Create、Get、Delete
-
准备DSL( Create时需要,其它是无参)
-
发送请求。调用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete
3.删除文档 修改文档 新增文档 批量添加文档
因为mysql查询出来的hotel信息经纬度信息是分开的,但是eleasticsearch的地理坐标要求是一个String类型,中间用逗号分开,所以要转为hotelDoc
//文档,相当于mysql中的行 //#查询一条文档(记录)的 dsl语句 // GET /heima/_doc/1 @SpringBootTest public class DocumentTest {private RestHighLevelClient client;@AutowiredHotelService hotelService;// 插入新数据@Testpublic void testadd() throws IOException {Hotel hotel = hotelService.getById(61083L);HotelDoc hotelDoc = new HotelDoc(hotel); // 创建request,并指明要处理的索引是hotel,记录号是 hotelDoc.getId()IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);client.index(request, RequestOptions.DEFAULT);}// 查询某条数据@Testpublic void testquery() throws IOException {GetRequest request = new GetRequest("hotel", "61083");GetResponse response = client.get(request, RequestOptions.DEFAULT);String json = response.getSourceAsString();HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);}//更新某条数据@Testpublic void testupdate() throws IOException {Hotel hotel = hotelService.getById(61083L);HotelDoc hotelDoc = new HotelDoc(hotel);UpdateRequest request = new UpdateRequest("hotel", "61083");request.doc("price", "952");request.doc("address", "北京饭店");client.update(request, RequestOptions.DEFAULT);}// 删除某条数据@Testvoid testDeleteDocument() throws IOException {// 1.准备RequestDeleteRequest request = new DeleteRequest("hotel", "61083");// 2.发送请求client.delete(request, RequestOptions.DEFAULT);}@Testvoid testpiliangadd() throws IOException {List<Hotel> hotels = hotelService.list();BulkRequest request = new BulkRequest();for (Hotel hotel : hotels) {HotelDoc hotelDoc = new HotelDoc(hotel);request.add(new IndexRequest("hotel").id(hotelDoc.getId() + "").source(JSON.toJSONString(hotelDoc), XContentType.JSON));}client.bulk(request, RequestOptions.DEFAULT);}@BeforeEachvoid setup() {this.client =new RestHighLevelClient(RestClient.builder(HttpHost.create("http://101.43.94.150:9200")));}@AfterEachvoid shutdown() throws IOException {this.client.close();} }4.查询文档
package cn.itcast.hotel;import cn.itcast.hotel.pojo.HotelDoc; import com.alibaba.fastjson.JSON; import org.apache.http.HttpHost; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.sort.SortOrder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;import java.io.IOException; import java.util.Map;@SpringBootTest public class QueryTest {private RestHighLevelClient client;//查询全部文档@Testvoid testmatchall() throws IOException { // 1. 准备requestSearchRequest request = new SearchRequest("hotel"); // 2. 准备dslrequest.source().query(QueryBuilders.matchAllQuery()); // 3.发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);handleResponse(response);}//全文检索查询@Testvoid testquerycheck() throws IOException { // 1. 准备requestSearchRequest request = new SearchRequest("hotel");// 2. 准备dslrequest.source().query(QueryBuilders.matchQuery("all", "如家")); // 3.发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);handleResponse(response);}//boolean复合查询@Testvoid testBool() throws IOException {// 1.准备RequestSearchRequest request = new SearchRequest("hotel");// 2.准备DSL// 2.1.准备BooleanQueryBoolQueryBuilder boolQuery = QueryBuilders.boolQuery();// 2.2.添加termboolQuery.must(QueryBuilders.termQuery("city", "上海"));// 2.3.添加rangeboolQuery.filter(QueryBuilders.rangeQuery("price").lte(250)); // 关联查询条件request.source().query(boolQuery);// 3.发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4.解析响应handleResponse(response);}//查询后排序和按页查询 el的分页查询是查询所有结果之后再分页。@Testvoid testpageandsort() throws IOException { // 页码,每页大小int pagenum = 1, pagesize = 5;// 1.准备RequestSearchRequest request = new SearchRequest("hotel");request.source().query(QueryBuilders.matchAllQuery());request.source().sort("price", SortOrder.ASC); // 前端传过来的是页码和每页大小 ,算一下从第多少条后开始分页request.source().from((pagenum - 1) * pagesize); // 页大小request.source().size(pagesize);// 3.发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4.解析响应handleResponse(response);}//查询后高亮显示@Testvoid testhightlight() throws IOException {// 1.准备RequestSearchRequest request = new SearchRequest("hotel");request.source().query(QueryBuilders.matchQuery("brand", "如家")); // requireFieldMatch(false) 搜索条件brand和高亮的属性name设为不一致request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));// 3.发送请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);handleHeightLightResponse(response);}// 高亮查询private void handleHeightLightResponse(SearchResponse response) {// 4.解析响应SearchHits searchHits = response.getHits();// 4.1.获取总条数long total = searchHits.getTotalHits().value;System.out.println("共搜索到" + total + "条数据");// 4.2.文档数组SearchHit[] hits = searchHits.getHits();// 4.3.遍历for (SearchHit hit : hits) {// 获取文档sourceString json = hit.getSourceAsString();// 反序列化HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); // 获取高亮Map<String, HighlightField> highlightFields = hit.getHighlightFields();HighlightField highlightField = highlightFields.get("name");String name = highlightField.getFragments()[0].toString();hotelDoc.setName(name);System.out.println(hotelDoc);}} // 解析响应函数private void handleResponse(SearchResponse response) {// 4.解析响应SearchHits searchHits = response.getHits();// 4.1.获取总条数long total = searchHits.getTotalHits().value;System.out.println("共搜索到" + total + "条数据");// 4.2.文档数组SearchHit[] hits = searchHits.getHits();// 4.3.遍历for (SearchHit hit : hits) {// 获取文档sourceString json = hit.getSourceAsString();// 反序列化HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class); // System.out.println("hotelDoc = " + hotelDoc);}}@BeforeEachvoid setup() {this.client =new RestHighLevelClient(RestClient.builder(HttpHost.create("http://101.43.94.150:9200")));}@AfterEachvoid shutdown() throws IOException {this.client.close();} }总结
以上是生活随笔为你收集整理的RestClient 访问elasticsearch的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 轮子王-原创数据结构-V2.0--内存/
- 下一篇: 物联网助力智慧农业