[英]ElasticSearch RestClient response to ResponseHits API instead of Response API not working
[英]ElasticSearch JavaAPI RestClient not giving response
我正在使用RestClient JavaAPI從彈性搜索中獲取文檔。 但是沒有收到我的請求的回復。
我正在使用QueryBuilder
形成我的請求。
請找到我的Java代碼,該代碼用於從彈性搜索中獲取文檔
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());
}
請查找有關使用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字節長的響應。 因此,您只需要從響應中讀取數據即可:
String responseBody = EntityUtils.toString(response.getEntity());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.