使用Java RestClient API从Elastic Search处理多个文档 [英] Handling multiple documents from Elastic Search using Java RestClient API

查看:134
本文介绍了使用Java RestClient API从Elastic Search处理多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am使用Java API从弹性搜索中获取文档。我只能从 responseBody 正确地获取一个文档。

Am fetching documents from elastic search using Java API. I am able to fetch only one document from the responseBody properly.

如果我得到多个文档,我该如何处理

How can i handle if i get multiple documents as response.

以前我使用 RestHighLevelClient 和该API可以在<$的帮助下处理多个文档c $ c> SearchHit [] searchHits = searchResponse.getHits()。getHits(); 。

Earlier i used RestHighLevelClient with that API i able to handle multiple documents with the help of SearchHit[] searchHits = searchResponse.getHits().getHits();.

使用 RestClient API,无法执行此操作。

With RestClient API, am not able to do that.,

请找到我下面的代码,该代码能够从弹性搜索中获取文档并将其解析为JSON对象。 (适用于单个文档)

Please find my below code, that am able to fetch document from elastic search and parsing it as JSON Object. ( working properly for single document)

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);
        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);


        try {
            contentSearchResponse = restClient.performRequest("GET", "/document_attachment/doc/_search", params, entity, consumerFactory);
        } 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());

        }


推荐答案

使用 scroll API。当结果集很大时,这将很有用。

Use scroll api. which will useful when resultset is large.

来自文档


虽然搜索请求返回单个页面结果,scroll API可用于从单个搜索请求中检索大量结果(甚至所有结果),其方式与在传统数据库中使用光标的方式几乎相同。

While a search request returns a single "page" of results, the scroll API can be used to retrieve large numbers of results (or even all results) from a single search request, in much the same way as you would use a cursor on a traditional database.

相似链接

弹性搜索滚动行为

文档

并行扫描&滚动Elasticsearch索引

这篇关于使用Java RestClient API从Elastic Search处理多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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