ElasticSearch RestClient 响应 ResponseHits API 而不是 Response API 不工作
ElasticSearch RestClient response to ResponseHits API instead of Response API not working
我正在通过 RestClient
Java API 从 elasticsearch 获取文档并尝试在 ResponseHits
中获取响应,以便能够处理多个文档(作为响应) 使用下面的代码。
SearchHit[] contentSearchHits = contentSearchResponse.getHits().getHits();
但我无法在 ResponseHits
API 中设置 RestClient
响应。从 Eclipse 收到此消息 Type mismatch: cannot convert from Response to ResponseHits
.
如果我使用 RestHighLevelClient
我可以使用 ResponseHits
class 并且一切顺利。但是我没有使用它,因为 BUFFER_SIZE
默认限制为 100MB,我需要更多 BUFFER_SIZE
.
那么,如果 RestClient
响应在搜索时返回多个文档,是否有任何处理响应的方法。
请找到我用来从 ElasticSearch 获取文档的 Java 代码。
private final static String ATTACHMENT = "document_attachment";
private final static String TYPE = "doc";
static long BUFFER_SIZE = 520 * 1024 * 1024; // <---- set buffer to 520MB instead of 100MB
public static void main(String args[])
{
RestClient restClient = null;
Response contentSearchResponse=null;
String responseBody = null;
JSONObject source = null;
String path = null;
String filename = null;
int id = 0;
ResponseHits responseHits = null;
RestClientBuilder builder = 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);
Map<String, String> params = Collections.emptyMap();
HttpEntity entity = new NStringEntity(contentSearchSourceBuilder.toString(), ContentType.APPLICATION_JSON);
HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory =
new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory((int) BUFFER_SIZE);
try {
//contentSearchResponse = restClient.performRequest("GET", "/document_attachment/doc/_search", params, entity, consumerFactory); // this is working with single document, not sure how to handle if request is returning multiple documents while searching.
responseHits = restClient.performRequest("GET", "/document_attachment/doc/_search", params, entity, consumerFactory); // am not able to set the response to `ResponseHits` class
} catch (IOException e1) {
e1.printStackTrace();
}
try {
responseBody = EntityUtils.toString(contentSearchResponse.getEntity());
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Converting to JSON");
JSONObject jsonObject = new JSONObject(responseBody);
JSONObject hits = jsonObject.getJSONObject("hits");
JSONArray hitsArray=hits.getJSONArray("hits");
for(int i=0;i<hitsArray.length();i++) {
JSONObject obj= hitsArray.getJSONObject(i);
source = obj.getJSONObject("_source");
id = Integer.parseInt(source.opt("id").toString());
path = source.optString("path");
filename = source.optString("filename");
}
JSONObject jsonBody = new JSONObject();
jsonBody.put("id", id);
jsonBody.put("path", path);
jsonBody.put("filename", filename);
System.out.println("Response --->"+jsonBody.toString());
}
我正在使用 ElasticSearch 版本 6.2.3
我找到了处理来自 RestClient
响应
的多个文档的方法
JSONArray hitsArray;
JSONArray responseArray = new JSONArray();
JSONObject responseObj;
String fileRelativePath=null;
JSONObject json = new JSONObject(responseBody);
JSONObject hits = json.getJSONObject("hits");
hitsArray = hits.getJSONArray("hits");
for (int i=0; i<hitsArray.length(); i++) {
JSONObject h = hitsArray.getJSONObject(i);
JSONObject sourceJObj = h.getJSONObject("_source");
responseObj = new JSONObject();
fileRelativePath = sourceJObj.optString("path").toString().concat(sourceJObj.optString("filename").toString());
responseObj.put("id", Integer.parseInt(sourceJObj.opt("id").toString()));
responseObj.put("fileRelativePath", fileRelativePath);
responseArray.put(responseObj);
}
System.out.println("Response Length -->"+responseArray.length()+"Response --->"+responseArray.toString());
我正在通过 RestClient
Java API 从 elasticsearch 获取文档并尝试在 ResponseHits
中获取响应,以便能够处理多个文档(作为响应) 使用下面的代码。
SearchHit[] contentSearchHits = contentSearchResponse.getHits().getHits();
但我无法在 ResponseHits
API 中设置 RestClient
响应。从 Eclipse 收到此消息 Type mismatch: cannot convert from Response to ResponseHits
.
如果我使用 RestHighLevelClient
我可以使用 ResponseHits
class 并且一切顺利。但是我没有使用它,因为 BUFFER_SIZE
默认限制为 100MB,我需要更多 BUFFER_SIZE
.
那么,如果 RestClient
响应在搜索时返回多个文档,是否有任何处理响应的方法。
请找到我用来从 ElasticSearch 获取文档的 Java 代码。
private final static String ATTACHMENT = "document_attachment";
private final static String TYPE = "doc";
static long BUFFER_SIZE = 520 * 1024 * 1024; // <---- set buffer to 520MB instead of 100MB
public static void main(String args[])
{
RestClient restClient = null;
Response contentSearchResponse=null;
String responseBody = null;
JSONObject source = null;
String path = null;
String filename = null;
int id = 0;
ResponseHits responseHits = null;
RestClientBuilder builder = 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);
Map<String, String> params = Collections.emptyMap();
HttpEntity entity = new NStringEntity(contentSearchSourceBuilder.toString(), ContentType.APPLICATION_JSON);
HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory =
new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory((int) BUFFER_SIZE);
try {
//contentSearchResponse = restClient.performRequest("GET", "/document_attachment/doc/_search", params, entity, consumerFactory); // this is working with single document, not sure how to handle if request is returning multiple documents while searching.
responseHits = restClient.performRequest("GET", "/document_attachment/doc/_search", params, entity, consumerFactory); // am not able to set the response to `ResponseHits` class
} catch (IOException e1) {
e1.printStackTrace();
}
try {
responseBody = EntityUtils.toString(contentSearchResponse.getEntity());
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Converting to JSON");
JSONObject jsonObject = new JSONObject(responseBody);
JSONObject hits = jsonObject.getJSONObject("hits");
JSONArray hitsArray=hits.getJSONArray("hits");
for(int i=0;i<hitsArray.length();i++) {
JSONObject obj= hitsArray.getJSONObject(i);
source = obj.getJSONObject("_source");
id = Integer.parseInt(source.opt("id").toString());
path = source.optString("path");
filename = source.optString("filename");
}
JSONObject jsonBody = new JSONObject();
jsonBody.put("id", id);
jsonBody.put("path", path);
jsonBody.put("filename", filename);
System.out.println("Response --->"+jsonBody.toString());
}
我正在使用 ElasticSearch 版本 6.2.3
我找到了处理来自 RestClient
响应
JSONArray hitsArray;
JSONArray responseArray = new JSONArray();
JSONObject responseObj;
String fileRelativePath=null;
JSONObject json = new JSONObject(responseBody);
JSONObject hits = json.getJSONObject("hits");
hitsArray = hits.getJSONArray("hits");
for (int i=0; i<hitsArray.length(); i++) {
JSONObject h = hitsArray.getJSONObject(i);
JSONObject sourceJObj = h.getJSONObject("_source");
responseObj = new JSONObject();
fileRelativePath = sourceJObj.optString("path").toString().concat(sourceJObj.optString("filename").toString());
responseObj.put("id", Integer.parseInt(sourceJObj.opt("id").toString()));
responseObj.put("fileRelativePath", fileRelativePath);
responseArray.put(responseObj);
}
System.out.println("Response Length -->"+responseArray.length()+"Response --->"+responseArray.toString());