简体   繁体   English

使用 java 连接 Elasticsearch

[英]Connect Elasticsearch using java

I am new to java coming from python .我是来自python java新手。 I know there are lot of answers out there to connect ElasticSearch with java .我知道有很多答案可以将ElasticSearchjava连接ElasticSearch But it is difficult for me to understand and some are outdated.但是我很难理解,有些已经过时了。 In python, I can easily import elasticsearch module and connect to it.在python中,我可以轻松导入elasticsearch模块并连接到它。

Here's the Code in python:这是python中的代码:

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

But in java , i have included the elasticsearch maven dependency in pom.xml .但是在java ,我在pom.xml包含了elasticsearch maven 依赖项。 I want to connect to elasticsearch .我想连接到elasticsearch I came to know RestHighLevelClient can do this job.我开始知道RestHighLevelClient可以完成这项工作。 I found this code.我找到了这个代码。 But don't know how to make it connect to 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);
}

Any one can help me or show me some sample code to connect with Elastic Search with java.任何人都可以帮助我或向我展示一些示例代码,以使用 java 连接 Elastic Search。 In python, it was done in two lines.在python中,它分两行完成。 Help me with java .帮我看看java

For connecting elasticsearch using java you can use the below code:要使用 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")));
}
}

elasticsearchConfiguration:弹性搜索配置:

host: localhost

port: 9200
  • For more information you can see this and this .有关更多信息,您可以查看thisthis

You can even follow instructions from this documentation您甚至可以按照本文档中的说明进行操作

You need to add this dependency in pom.xml您需要在pom.xml添加此依赖项

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

Update 1更新 1

Instead of separate config file, in order to add host and port inside code itself you can use below mentioned code:而不是单独的配置文件,为了在代码本身中添加主机和端口,您可以使用下面提到的代码:

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")));
}

For the current version Java REST Client version 7.5, follow the instructions at ElasticSearch Client :对于当前版本的 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 : Maven 存储库,ElasticSearch 客户端:

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

All the API functions are specified at the client website: search, multi-search, index, etc.所有的API功能都在客户端网站上指定:搜索、多搜索、索引等。

The "Java Low Level REST Client" Basic Authentication is defined here : “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);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM