用于 ElasticSearch 的 Java HTTP 客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10441499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java HTTP Client for ElasticSearch
提问by James Ward
I'm trying to connect from Java to ElasticSearch but I can only connect over HTTP. I can't use the TransportClient
. Is there a Java client wrapper around the ElasticSearch REST APIs? If so, how do I use it?
我正在尝试从 Java 连接到 ElasticSearch,但我只能通过 HTTP 连接。我无法使用TransportClient
. ElasticSearch REST API 是否有 Java 客户端包装器?如果是这样,我该如何使用它?
采纳答案by dogukan sonmez
回答by imotov
A new "official" REST-based java client will be availablestarting with v5.0.0-alpha4.
从 v5.0.0-alpha4 开始,将提供一个新的“官方”基于 REST 的 Java 客户端。
回答by Bastian Voigt
We just open sourced Flummi, a Java HTTP/REST client for Elastic Search. It imitates the transport client's API as closely as possible, making it easy to port existing code. It also provides a better abstraction level than Jest, because it reports all the errors with Exceptions. Give it a try!
我们刚刚开源了Flummi,这是一个用于 Elastic Search 的 Java HTTP/REST 客户端。它尽可能地模仿传输客户端的 API,使得移植现有代码变得容易。它还提供了比 Jest 更好的抽象级别,因为它报告所有带有异常的错误。试一试!
Simple usage example:
简单使用示例:
Flummi flummi = new Flummi("http://elasticsearch.base.url:9200");
SearchResponse searchResponse = flummi
.prepareSearch("products")
.setQuery(
QueryBuilders.termQuery("color", "yellow").build()
)
.execute();
System.out.println("Found "
+ searchResponse.getHits().getTotalHits()
+ " products");
searchResponse.getHits()
.stream().map(hit -> hit.getSource().get("name").getAsString())
.forEach(name -> System.out.println("Name: " + name));
回答by H6.
Since version 5.6of the Elasticsearch Java SDK they provide a Java REST Client.
从Elasticsearch Java SDK 的5.6 版开始,它们提供了Java REST Client。
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")).build();
// for the RestHighLevelClient
RestHighLevelClient client =
new RestHighLevelClient(restClient);