ElasticSearch JavaAPI RestClient未给出响应 [英] ElasticSearch JavaAPI RestClient not giving response

查看:233
本文介绍了ElasticSearch JavaAPI RestClient未给出响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RestClient JavaAPI从弹性搜索中获取文档。

Am using RestClient JavaAPI to fetch documents from elastic search. But am not getting response for my request.

使用 QueryBuilder 形成我的请求。

请找到我的Java代码,该代码用于从弹性搜索中获取文档

Please find my java code which am using to fetch documents from elastic search

private final static String ATTACHMENT = "document_attachment";
private final static String TYPE = "doc";
static long BUFFER_SIZE = 520 * 1024 * 1024;   //  <---- set buffer to 120MB instead of 100MB

public static void main(String args[])
{
    RestClient restClient = null;
    Response contentSearchResponse=null;
    try {

    restClient = RestClient.builder(
                    new HttpHost("localhost", 9200, "http"),
                    new HttpHost("localhost", 9201, "http")).build();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    SearchRequest contentSearchRequest = new SearchRequest(ATTACHMENT); 
    SearchSourceBuilder contentSearchSourceBuilder = new SearchSourceBuilder();
    contentSearchRequest.types(TYPE);
    QueryBuilder attachmentQB = QueryBuilders.matchQuery("attachment.content", "activa");
    contentSearchSourceBuilder.query(attachmentQB);
    contentSearchSourceBuilder.size(50);
    contentSearchRequest.source(contentSearchSourceBuilder);
    System.out.println("Request --->"+contentSearchRequest.toString());

    Map<String, String> params = Collections.emptyMap();
    HttpEntity entity = new NStringEntity(contentSearchSourceBuilder.toString(), ContentType.APPLICATION_JSON);
    HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory =
            new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory((int) BUFFER_SIZE);

    System.out.println("Printing Entity---->"+entity.toString());

    try {
        contentSearchResponse = restClient.performRequest("GET", "/document_attachment/doc/_search", params, entity, consumerFactory);
    } catch (IOException e1) {
        e1.printStackTrace();
    } 
    System.out.println("Entity Response --->"+contentSearchResponse.getEntity());
    }

请找到有关我的Reqest,Entity&使用Sysout打印的响应。

Please find the details about my Reqest, Entity & Response that am printing using Sysout.

Request --->SearchRequest{searchType=QUERY_THEN_FETCH, indices=[document_attachment], indicesOptions=IndicesOptions[id=38, ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, expand_wildcards_closed=false, allow_aliases_to_multiple_indices=true, forbid_closed_indices=true, ignore_aliases=false], types=[doc], routing='null', preference='null', requestCache=null, scroll=null, maxConcurrentShardRequests=0, batchedReduceSize=512, preFilterShardSize=128, source={"size":50,"query":{"match":{"id":{"query":"87740","operator":"OR","prefix_length":0,"max_expansions":50,"fuzzy_transpositions":true,"lenient":false,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"boost":1.0}}}}}
Printing Entity---->[Content-Type: application/json; charset=UTF-8,Content-Length: 233,Chunked: false]
Entity Response --->[Content-Length: 929382,Chunked: false]


推荐答案

您会收到一个长度为929382字节的响应。因此,您只需要从响应中读取数据即可:

You do get a response which is 929382 bytes long. So you simply need to read the data from the response:

String responseBody = EntityUtils.toString(response.getEntity());

这篇关于ElasticSearch JavaAPI RestClient未给出响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆