GET users/_search GET /users/_search { "query": { "match_all": {} } }
GET users/_search
GET /users/_search
{
"query": {
"match_all": {}
}
}
{ "took" : 1, "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" : 16, "gender" : "女", "username" : "Rose" } } ] } }
{
"took" : 1,
"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" : 16,
"gender" : "女",
"username" : "Rose"
}
}
]
}
}
package com.alatus.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; import static com.alibaba.nacos.api.common.Constants.TOKEN; @Configuration public class MallElasticSearchConfig { 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(); } // 编写配置 // 给容器中注入一个RestHighLevelClient @Bean public RestHighLevelClient esRestClient(){ return new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.56.10",9200,"http") ) ); } }
package com.alatus.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; import static com.alibaba.nacos.api.common.Constants.TOKEN; @Configuration public class MallElasticSearchConfig { 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(); } // 编写配置 // 给容器中注入一个RestHighLevelClient @Bean public RestHighLevelClient esRestClient(){ return new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.56.10",9200,"http") ) ); } }
package com.alatus.search; import com.alatus.search.config.MallElasticSearchConfig; import com.alibaba.fastjson.JSON; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; @RunWith(SpringRunner.class) @SpringBootTest public class TestLoad { @Autowired private RestHighLevelClient client; @Test public void load(){ System.out.println(client); } @Test public void index() throws IOException { IndexRequest request = new IndexRequest("users"); request.id("1"); request.source("username","Jack","age",18,"gender","男"); String jsonString = JSON.toJSONString(new User("Rose", 18, "女")); request.source(jsonString, XContentType.JSON); // 执行操作 IndexResponse index = client.index(request, MallElasticSearchConfig.COMMON_OPTIONS); System.out.println(index); } @Data @AllArgsConstructor @NoArgsConstructor class User{ private String username; private Integer age; private String gender; } }
package com.alatus.search; import com.alatus.search.config.MallElasticSearchConfig; import com.alibaba.fastjson.JSON; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; @RunWith(SpringRunner.class) @SpringBootTest public class TestLoad { @Autowired private RestHighLevelClient client; @Test public void load(){ System.out.println(client); } @Test public void index() throws IOException { IndexRequest request = new IndexRequest("users"); request.id("1"); request.source("username","Jack","age",18,"gender","男"); String jsonString = JSON.toJSONString(new User("Rose", 18, "女")); request.source(jsonString, XContentType.JSON); // 执行操作 IndexResponse index = client.index(request, MallElasticSearchConfig.COMMON_OPTIONS); System.out.println(index); } @Data @AllArgsConstructor @NoArgsConstructor class User{ private String username; private Integer age; private String gender; } }