https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-requests.html#java-rest-low-usage-request-options
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html
package com.atguigu.gulimall.search.config; import org.apache.http.HttpHost; import org.elasticsearch.client.HttpAsyncResponseConsumerFactory; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 1、导入依赖 * 2、编写配置,给容器中注入一个RestHighLevelClient * 3、参照API https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/index.html */ @Configuration public class GulimallElasticSearchConfig { public static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); // builder.addHeader("Authorization", "Bearer " + TOKEN); // builder.setHttpAsyncResponseConsumerFactory( // new HttpAsyncResponseConsumerFactory // .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024)); COMMON_OPTIONS = builder.build(); } @Bean public RestHighLevelClient esRestClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.56.10", 9200, "http"))); return client; } }
package com.atguigu.gulimall.search; import com.alibaba.fastjson.JSON; import com.atguigu.gulimall.search.config.GulimallElasticSearchConfig; import lombok.Data; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.io.IOException; @RunWith(SpringRunner.class) @SpringBootTest public class GulimallSearchApplicationTests { @Resource private RestHighLevelClient client; /** * 测试存储数据到es */ @Test public void indexData() throws IOException { IndexRequest indexRequest = new IndexRequest("users");//向users下存数据 indexRequest.id("1");//不设会自动生成 // indexRequest.source("userName", "zhangsan","age", 18,"gender", "男");//数据内容形式,k-v形式 User user= new User(); user.setUserName("zhangsan"); user.setAge(18); user.setGender("男"); String jsonString = JSON.toJSONString(user); indexRequest.source(jsonString, XContentType.JSON);//数据内容形式,json形式 //执行操作。索引数据不存在数据就生成,存在就更新 IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS); System.out.println(index); } @Data static class User{ private String userName; private Integer age; private String gender; } @Test public void contextLoads() { System.out.println(client); } }
结果查询:
get users/_search
结果:
{ "took" : 393, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "users", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "age" : 18, "gender" : "男", "userName" : "zhangsan" } } ] } }