elasticsearch java简单应用

文章描述了一个使用EsClient类创建ElasticsearchREST客户端并执行查询的操作,展示了如何设置连接超时和发送JSON请求。同时提到了未启用Elasticsearch安全导致的警告。
摘要由CSDN通过智能技术生成

(1)依赖

compile('org.elasticsearch.client:elasticsearch-rest-client:6.8.15')

(2)EsClient类

@Component
public class EsClient {

	public RestClient create() {
		HttpHost[] hosts= new HttpHost[] {new HttpHost("127.0.0.1", 9201, "http")};
        RestClientBuilder builder = RestClient.builder(hosts);
        builder = builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

			@Override
			public Builder customizeRequestConfig(Builder requestConfigBuilder) {
				return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000);
			}
			
        });
        Header[] defaultHeaders = new Header[] { new BasicHeader("Accept", "application/json"), new BasicHeader("Content-type", "application/json") };
        builder.setDefaultHeaders(defaultHeaders);
        RestClient restClient = builder.build();
        return restClient;
	}
	
}

(3)测试和运行结果

JSONObject queryJson = new JSONObject();
JSONObject match = new JSONObject();
JSONObject matchWrapper = new JSONObject();
matchWrapper.put("match_all", match);
queryJson.put("query", matchWrapper);
queryJson.put("from", 0);
queryJson.put("size", 2);
String body = queryJson.toString();
log.info("{}", body);
StringEntity entity = new StringEntity(body, ContentType.APPLICATION_JSON);
String query = "/test/_search";
log.info(query);
Request request = new Request("GET", query);
request.setEntity(entity);
RestClient restClient = esClient.create();
Response rsp = restClient.performRequest(request);
String result = EntityUtils.toString(rsp.getEntity());
log.info("{}", result);
2024-01-17 16:09:28 [restartedMain] INFO  cn.hwd.TestRunner - body:{"size":2,"query":{"match_all":{}},"from":0} 
2024-01-17 16:09:28 [restartedMain] INFO  cn.hwd.TestRunner - query:/test/_search 
2024-01-17 16:09:28 [I/O dispatcher 1] WARN  org.elasticsearch.client.RestClient - request [GET http://127.0.0.1:9201/test/_search] returned 1 warnings: [299 Elasticsearch-7.17.6-f65e9d338dc1d07b642e14a27f338990148ee5b6 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."] 
2024-01-17 16:09:28 [restartedMain] INFO  cn.hwd.TestRunner - result:{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"test231208","_type":"_doc","_id":"1","_score":1.0,"_source":{
	"name": "tom",
	"age": 2,
	"gmt_create": "20231208100612"
}},{"_index":"test231208","_type":"_doc","_id":"2","_score":1.0,"_source":{
	"name": "a jerry",
	"age": 4,
	"gmt_create": "20231208100612"
}}]}}
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值