简体   繁体   English

Java Rest弹性搜索JSON文档

[英]Java rest elastic search json documents

I am trying to sends json documents to elasticsearch by using java rest . 我正在尝试使用java rest将json文档发送到elasticsearch。

I just need to know how to initialize the variable "entities[i]" and put json documents in its.I have try many ways but still do not get something which work. 我只需要知道如何初始化变量“ entities [i]”并将json文档放入其中。我已经尝试了很多方法,但仍然没有得到有效的解决方法。

here is the code from elastticsearch website: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_example_requests.html 这是elastticsearch网站上的代码: https ://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_example_requests.html

int numRequests = 10;
final CountDownLatch latch = new CountDownLatch(numRequests);

for (int i = 0; i < numRequests; i++) {
    restClient.performRequestAsync(
        "PUT",
        "/twitter/tweet/" + i,
        Collections.<String, String>emptyMap(),
        //assume that the documents are stored in an entities array
        entities[i],
        new ResponseListener() {
            @Override
            public void onSuccess(Response response) {
                System.out.println(response);
                latch.countDown();
            }

            @Override
            public void onFailure(Exception exception) {
                latch.countDown();
            }
        }
    );
}

//wait for all requests to be completed
latch.await();

Thanks you 谢谢

    int numRequests = 1;
    final CountDownLatch latch = new CountDownLatch(numRequests);

    HttpEntity entity = new NStringEntity(
            "{\n" +
                    "    \"user\" : \"kimchy\",\n" +
                    "    \"post_date\" : \"2009-11-15T14:12:12\",\n" +
                    "    \"message\" : \"trying out Elasticsearch\"\n" +
                    "}", ContentType.APPLICATION_JSON);

    List<HttpEntity> entities = asList(entity);

    for (int i = 0; i < numRequests; i++) {
        restClient.performRequestAsync(
                "PUT",
                "/twitter/tweet/" + i,
                Collections.<String, String>emptyMap(),
                entities.get(i),
                new ResponseListener() {
                    @Override
                    public void onSuccess(Response response) {
                        System.out.println(response);
                        latch.countDown();
                    }

                    @Override
                    public void onFailure(Exception exception) {
                        latch.countDown();
                    }
                }
        );
    }

    latch.await();

The entity is a type HttpEntity.You need to create list of HttpEntity objects in the list and use them. 该实体是HttpEntity类型。您需要在列表中创建HttpEntity对象列表并使用它们。

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

相关问题 Elastic Search中的嵌套文档 - Nested documents in Elastic Search 使用 Java 高级 REST 客户端动态更改弹性文档 - Change elastic documents dynamically using Java High Level REST client 使用Java RestClient API从Elastic Search处理多个文档 - Handling multiple documents from Elastic Search using Java RestClient API Elastic Search未索引文档的“ **” - Elastic Search not indexing **some** of the documents 弹性搜索,通过Rest Java Client使用源和设置创建索引 - Elastic Search, creating index with sources and settings by Rest Java Client Spring 数据弹性搜索与 Java 高级 REST 客户端 - Spring Data Elastic Search vs Java High Level REST Client JEST 弹性搜索 rest java API 中的 SSL 证书合并问题 - Issue with SSL certificate incorporation in JEST elastic search rest java API 为什么在弹性搜索中引入 Java 高级 REST 客户端? - Why Java High Level REST Client got introduced in Elastic search? Elasticsearch Java API MoreLike与“ _search”其余端点相比,此文档不返回文档 - Elasticsearch Java API MoreLikeThis not returning documents compared to “_search” rest endpoint 如何使用弹性搜索RestHighLevelClient在java中编写json映射? - How to write json mapping in java using elastic search RestHighLevelClient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM