关于文档的API操作
首先为了方便测试观看,删除全部的索引库,包括kabana自带的3个库
网络异常,图片无法展示
|
首先创建实体类
网络异常,图片无法展示
|
package com.wyh.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; /** * @program: SpringBoot_ElasticSearch * @description: User实体类 * @author: 魏一鹤 * @createDate: 2022-04-19 19:49 **/ @Data @Component @NoArgsConstructor @AllArgsConstructor public class User { private String name; private int age; }
添加文档
把对象转换为JSON,然后把JSON放入到请求中即可
package com.wyh; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.wyh.config.ElasticSearchClientConfig; import com.wyh.entity.User; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.suggest.completion.RegexOptions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import javax.annotation.Resource; import java.io.IOException; /** * 测试es7.6.1 高级客户端API操作 **/ @SpringBootTest class WyhApplicationTests { //注入bean @Resource private RestHighLevelClient client; @Test //添加文档 void testAddDocument() throws IOException { //创建对象 User user=new User("魏一鹤",23); //创建请求 IndexRequest indexRequest = new IndexRequest("wei_index"); //设置规则 //id indexRequest.id("1"); //过期规则超时时间 1秒 indexRequest.timeout(TimeValue.timeValueSeconds(1)); //由于只能引入json 把user对象转换为json String jsonString = JSON.toJSONString(user); //将数据放入到请求 json IndexRequest source = indexRequest.source(jsonString, XContentType.JSON); //客户端发送请求获取响应结果 IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT); //IndexResponse[index=wei_index,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}] System.out.println(index); System.out.println(index.toString()); System.out.println(index.status()); //对应命令返回的状态CREATED 如果是修改就是UPDATE } }
网络异常,图片无法展示
|
网络异常,图片无法展示
|
获取文档
package com.wyh; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.wyh.config.ElasticSearchClientConfig; import com.wyh.entity.User; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.suggest.completion.RegexOptions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import javax.annotation.Resource; import java.io.IOException; /** * 测试es7.6.1 高级客户端API操作 **/ @SpringBootTest class WyhApplicationTests { //注入bean @Resource private RestHighLevelClient client; //判断索引库是否存在 @Test void testIsExists() throws IOException { //首先判断文档是否存在,存在再进行 GetRequest index = new GetRequest("wei_index","1"); //不获取返回的_source上下文了 index.fetchSourceContext(new FetchSourceContext(false)); index.storedFields("_none_"); //判断索引库否存在 true或者false boolean exists = client.exists(index, RequestOptions.DEFAULT); System.out.println(exists); } @Test //获取文档信息 void testGetDocument() throws IOException { //首先判断文档是否存在,存在再进行 GetRequest index = new GetRequest("wei_index","1"); GetResponse getResponse = client.get(index, RequestOptions.DEFAULT); //打印文档的内容 //{"age":23,"name":"魏一鹤"} //{"_index":"wei_index","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"age":23,"name":"魏一鹤"}} System.out.println(getResponse.getSourceAsString()); //返回的全部内容和使用命令是一样的 System.out.println(getResponse); } }
网络异常,图片无法展示
|