跟随 sn-p 可能是一个起点。
String serverName = "localhost";
String indexName = "index_name";
String mappingName = "mapping_name";
String docId = "FooBarId";
String username = "JohnDoe";
String password = "secret";
String requestURL = String.format("http://%s:9200/%s/%s/%s",
serverName,
indexName,
mappingName,
docId
);
System.out.println("requestURL: " + requestURL);
URL url = new URL(requestURL);
System.out.println("URL: " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-Requested-With", "Curl");
connection.setRequestMethod("HEAD");
String credentials = username + ":" + password;
Base64.Encoder encoder = Base64.getEncoder();
String basicAuth = "Basic " + encoder.encodeToString(credentials.getBytes());
connection.setRequestProperty("Authorization", basicAuth);
connection.getHeaderFields()
.entrySet()
.forEach((Entry<String, List<String>> t) -> {
System.out.printf("%-20s : %s%n", t.getKey(), t.getValue());
});
将requestURL = "http://localhost:9200"; 与默认的elasticsearch 安装一起使用会返回
requestURL: http://localhost:9200
URL: http://localhost:9200
null : [HTTP/1.1 200 OK]
Content-Length : [0]
Content-Type : [text/plain; charset=UTF-8]
添加也许您可以尝试类似于以下步骤的操作。根据您的需要修改它们。也许你可以跳过第一步。
索引一些东西
curl -XPUT "http://localhost:9200/books/book/1" -d'
{
"title": "The Hitchhikers Guide to the Galaxy",
"author": "Douglas Adams",
"year": 1978
}'
从命令行查询
curl -X GET http://localhost:9200/books/book/1
输出
{"_index":"books","_type":"book","_id":"1","_version":1,"found":true,"_source":
{
"title": "The Hitchhikers Guide to the Galaxy",
"author": "Douglas Adams",
"year": 1978
}}
使用上述 Java sn-p 查询
String serverName = "localhost";
String indexName = "books";
String mappingName = "book";
String docId = "1";
String requestURL = String.format("http://%s:9200/%s/%s/%s",
serverName,
indexName,
mappingName,
docId
);
System.out.println("requestURL: " + requestURL);
URL url = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.getHeaderFields()
.entrySet()
.forEach((Entry<String, List<String>> t) -> {
System.out.printf("%-20s : %s%n", t.getKey(), t.getValue());
});
try (InputStream inputStream = connection.getInputStream()) {
for (int i = inputStream.read(); i > -1; i = inputStream.read()) {
System.out.print((char) i);
}
}
输出
requestURL: http://localhost:9200/books/book/1
null : [HTTP/1.1 200 OK]
Content-Length : [184]
Content-Type : [application/json; charset=UTF-8]
{"_index":"books","_type":"book","_id":"1","_version":1,"found":true,"_source":
{
"title": "The Hitchhikers Guide to the Galaxy",
"author": "Douglas Adams",
"year": 1978
}}
该示例使用默认的 elasticsearch 安装。
取决于您真正想要实现的目标。你最好使用elasticsearch TransportClient。
import java.net.InetAddress;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
public class GetDemo {
public static void main(String[] args) throws Exception {
InetAddress hostAddr = InetAddress.getByName("localhost");
InetSocketTransportAddress socketAddr =
new InetSocketTransportAddress(hostAddr, 9300);
try (Client client = TransportClient.builder().build()
.addTransportAddress(socketAddr)) {
GetRequestBuilder request = client.prepareGet("books", "book", "1");
GetResponse response = request.execute().actionGet();
response.getSource()
.forEach((k, v) -> System.out.printf("%-6s: %s%n", k, v));
}
}
}
输出
...
year : 1978
author: Douglas Adams
title : The Hitchhikers Guide to the Galaxy