<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.5.2</version>
</dependency>
package com.zby;
import java.io.IOException;
import java.net.InetAddress;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
/**
* @author zby
* @title ElasticSearchApiDemo
* @date 2020年7月1日
* @description
*/
@SuppressWarnings({"deprecation", "resource"})
public class ElasticSearchApiDemo {
public static void main(String[] args) throws IOException {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
GetResponse response = client.prepareGet("customer", "_doc", "1").get();
System.out.println(response);
// on shutdown
client.close();
}
}
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.5.2</version>
</dependency>
package com.zby;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
/**
* @author zby
* @title ElasticSearchLowLevelApiDemo
* @date 2020年7月1日
* @description
*/
public class ElasticSearchLowLevelApiDemo {
public static void main(String[] args) throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
Response response = restClient.performRequest(new Request("GET", "/customer/_doc/1"));
System.out.println(EntityUtils.toString(response.getEntity()));
restClient.close();
}
}
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.2</version>
</dependency>
package com.zby;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @author zby
* @title ElasticSearchHighLevelApiDemo
* @date 2020年7月1日
* @description
*/
public class ElasticSearchHighLevelApiDemo {
public static void main(String[] args) throws IOException {
RestHighLevelClient client =
new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
GetResponse getResponse = client.get(new GetRequest("customer", "1"), RequestOptions.DEFAULT);
System.out.println(getResponse);
client.close();
}
}
原文:https://www.cnblogs.com/zby9527/p/13221377.html