使用 java 连接 Elasticsearch
Connect Elasticsearch using java
我是 java
的新手,来自 python
。我知道有很多答案可以将 ElasticSearch
与 java
联系起来。但是我很难理解,有些已经过时了。在 python 中,我可以轻松导入 elasticsearch
模块并连接到它。
这是 python 中的代码:
from elasticsearch import Elasticsearch
es = Elasticsearch('localhost', port=9200, http_auth=('username', 'password'), scheme="http")
但是在 java
中,我在 pom.xml
中包含了 elasticsearch
maven 依赖项。我想连接到 elasticsearch
。我开始知道 RestHighLevelClient
可以胜任这项工作。我找到了这段代码。但是不知道如何让它连接到Elastic Search。
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 连接 Elastic Search。在python中,分两行完成。帮我 java
.
对于当前版本 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 客户端:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.2</version>
</dependency>
所有API功能都在客户网站指定:搜索、多搜索、索引等
"Java Low Level REST Client" 基本身份验证定义 here:
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);
}
});
要使用 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")));
}
}
elasticsearch配置:
host: localhost
port: 9200
您甚至可以按照此 documentation
中的说明进行操作
你需要在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
的新手,来自 python
。我知道有很多答案可以将 ElasticSearch
与 java
联系起来。但是我很难理解,有些已经过时了。在 python 中,我可以轻松导入 elasticsearch
模块并连接到它。
这是 python 中的代码:
from elasticsearch import Elasticsearch
es = Elasticsearch('localhost', port=9200, http_auth=('username', 'password'), scheme="http")
但是在 java
中,我在 pom.xml
中包含了 elasticsearch
maven 依赖项。我想连接到 elasticsearch
。我开始知道 RestHighLevelClient
可以胜任这项工作。我找到了这段代码。但是不知道如何让它连接到Elastic Search。
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 连接 Elastic Search。在python中,分两行完成。帮我 java
.
对于当前版本 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 客户端:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.2</version>
</dependency>
所有API功能都在客户网站指定:搜索、多搜索、索引等
"Java Low Level REST Client" 基本身份验证定义 here:
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);
}
});
要使用 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")));
}
}
elasticsearch配置:
host: localhost
port: 9200
您甚至可以按照此 documentation
中的说明进行操作你需要在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")));
}