当前位置: 首页 > 知识库问答 >
问题:

如何使用JAVA高级REST客户端创建弹性搜索索引?

段哲圣
2023-03-14

通过以下命令,我可以查看弹性搜索部署的endpoint,并且从Postman那里没有任何问题:GET https://:@d97215aee2.us-east-1.aws.found.io:9243

我也可以使用邮递员的这个命令创建索引...将https://el弹力:4yqimxfosz9mxpgy1fj7t5bu@d97218f74f6d48489b355dd7d665aee2.us-east-1.aws.found.io:9243/。但是,我不能从Java代码中做同样的事情。

下面是我的Java代码的状态。它基本上是这些教程页面中的代码。

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-start-initialization.html

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-high-document-index.html

import java.io.IOException;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

@Path("/elasticsearch")
public class ElasticSearchService {

    @POST
    public void createElasticIndex() throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                    new HttpHost("d9<deleted a bunch of characters for privacy>7d665aee2.us-east-1.aws.found.io", 9243, "https")));


        IndexRequest request = new IndexRequest(
                "posts",
                "doc",
                "1");
        String jsonString = "{" +
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";
        request.source(jsonString, XContentType.JSON);

        client.close();
    }    
}

我也尝试过用我们的用户名和密码更新URL地址,正如这篇文章所建议的:ElasticSearch与ElasticCloud的身份验证错误?

        RestClient.builder(
                new HttpHost(
                        "<my user name>:<my password>@d97218<hidden characters>d665aee2.us-east-1.aws.found.io",
                        9243, "https")));

这对我不起作用。我猜这个人没有使用新的弹性高级别REST客户端。我收到此错误:

org.glassfish.jersey.server.internal.process.mappableException:java.io.ioException::@d97265aee2.us-east-1.aws.found.io:无效的IPv6地址

共有1个答案

董和泽
2023-03-14

在这里找到答案:在这里输入链接描述

运行得更新代码:

import java.io.IOException;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;

@Path("/elasticsearch")
public class ElasticSearchService {
    private static final String ELASTIC_SEARCH_USER_NAME = <my elastic search username>;
    private static final String ELASTIC_SEARCH_PASSWORD = <my elastic search password>;
    private static final String ELASTIC_SEARCH_ENDPOINT_URL = <my elastic search endpoint url>
    private static final Integer ELASTIC_SEARCH_PORT = 9243;

    @POST
    public void createElasticIndex() throws IOException {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(ELASTIC_SEARCH_USER_NAME, ELASTIC_SEARCH_PASSWORD));

        RestClientBuilder builder = RestClient
                .builder(new HttpHost(
                        ELASTIC_SEARCH_ENDPOINT_URL,
                        ELASTIC_SEARCH_PORT, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        RestHighLevelClient client = new RestHighLevelClient(builder);

        IndexRequest request = new IndexRequest(
                "contacts",
                "doc",
                "1");
        String jsonString = "{" +
                "\"user\":\"frank\"," +
                "\"postDate\":\"2020-03-02\"," +
                "\"message\":\"created this document from Java\"" +
                "}";
        request.source(jsonString, XContentType.JSON);

        try {
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            System.out.println(response);

        } catch (ElasticsearchException e) {
            if (e.status() == RestStatus.CONFLICT) {
            }
        }

        client.close();
    }

}

此代码创建一个名为contacts的索引,并将文档添加到该索引中。

 类似资料:
  • 我有一个在spring boot应用程序中创建弹性搜索索引的代码。目前使用的客户端是transport客户端,它现在根据弹性搜索文档进行折旧,现在被高级Rest客户端取代。 用于使用高级Rest客户端创建索引。我见过这个代码。 这里的fieldsMapping是一个json文件,它包含有关analyzer、tokenizer和filter的详细信息,并作为字符串传递给这个方法。我无法在java r

  • 我正在尝试将我的弹性搜索(6.6.1)、spring boot(2.1.3)应用程序从Java8迁移到Java11。之前,我使用高级java rest客户端创建和搜索索引。因为存在一个问题(https://github.com/elastic/elasticsearch/issues/38299)在模块化高级rest客户端api时,我试图使用低级rest客户端,但无法获得任何搜索结果。 请看一些代

  • 我正在使用java高级rest客户端在我的应用程序中集成elasticsearch,但无法创建索引 在某个地方,我发现要执行请求,我们需要使用index(请求)方法(我在代码中已注释),但它表明index(请求)方法已从RestHighLevelClient类型中弃用。 这是我的代码:

  • 我们不允许寻求书籍、工具、软件库等推荐的问题。你可以编辑这个问题,以便用事实和引用来回答。 我是弹性搜索的初学者,正在寻找将弹性搜索与Spring boot相结合的最佳方式。 我不确定使用以下哪一项:- > Spring数据弹性搜索 开玩笑 谢谢

  • 在es中搜索时,如何使用spring的rest模板或elasticsearch自己的高/低rest客户端,我左右为难。与spring rest模板相比,es客户端是否提供了HTTP连接池、性能等方面的任何优势。这两个模板中的哪一个在从服务器获取响应时花费的时间更少。请有人解释一下?