使用 java 连接 Elasticsearch



我是来自pythonjava新手。我知道有很多答案可以将ElasticSearchjava联系起来。但我很难理解,有些已经过时了。在python中,我可以轻松地导入elasticsearch模块并连接到它。

这是 python 中的代码:

from elasticsearch import Elasticsearch
es = Elasticsearch('localhost', port=9200, http_auth=('username', 'password'), scheme="http")

但是在java中,我已经在pom.xml中包含了elasticsearchmaven依赖。我想连接到elasticsearch。我开始知道RestHighLevelClient可以胜任这项工作。我找到了这个代码。但不知道如何让它连接到弹性搜索。

public RestHighLevelClient createESRestClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esUserName, esPassword));
RestClientBuilder restClientBuilder = RestClient
.builder(new HttpHost(esRestclientHost, 9200, "http"));
// Use this one if your ElasticSearch server is setup to use username & password authentication
if (esAuthentication) {
restClientBuilder.setHttpClientConfigCallback(h -> h.setDefaultCredentialsProvider(credentialsProvider));
}
return new RestHighLevelClient(restClientBuilder);
}

任何人都可以帮助我或向我展示一些示例代码,以便使用 java 连接弹性搜索。在python中,它是分两行完成的。帮我java.

要使用 java 连接 elasticsearch,您可以使用以下代码:

public class ElasticsearchClient {
//private static final Logger log = LoggerFactory.getLogger(ElasticsearchClient.class);
private final RestHighLevelClient client;
public ElasticsearchClient(ElasticsearchConfig elasticsearchConfig) {
client = new RestHighLevelClient(RestClient.builder(new HttpHost(elasticsearchConfig.getHost(),
elasticsearchConfig.getPort(), "http")));
}
}

弹性搜索配置:

host: localhost
port: 9200
  • 有关更多信息,您可以看到这个和这个。

您甚至可以按照本文档中的说明进行操作

您需要在pom.xml中添加此依赖项

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${es.client.version}</version>
</dependency>

更新 1

为了在代码本身中添加主机和端口,您可以使用下面提到的代码,而不是单独的配置文件:

public class ElasticsearchClient {
private static final Logger log = LoggerFactory.getLogger(ElasticsearchClient.class);
private final RestHighLevelClient client;

public ElasticsearchClient(ElasticsearchConfig elasticsearchConfig) {
client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
}

对于当前版本的 Java REST Client 版本 7.5,请按照 ElasticSearch Client 中的说明进行操作:

RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));

Maven repository, ElasticSearch Client :

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.2</version>
</dependency>

所有 API 功能都在客户端网站上指定:搜索、多重搜索、索引等。

"Java 低级 REST 客户机"基本认证定义如下:

final CredentialsProvider credentialsProvider =
new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("user", "password"));
RestClientBuilder builder = RestClient.builder(
new HttpHost("localhost", 9200))
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider);
}
});

相关内容

  • 没有找到相关文章

最新更新