【问题标题】:Elasticsearch not able to fetch more than 10 documents using Java API QueryElasticsearch 无法使用 Java API 查询获取超过 10 个文档
【发布时间】:2018-07-12 11:57:44
【问题描述】:

我正在从该文件路径中名为documents 的一个索引读取文件路径,并使用java 代码读取文件并将这些文件内容索引到另一个名为documents_attachment 的索引中。

所以,在第一个过程中,我一次无法获取超过 10 的记录,它只提供来自 10 的记录 document 索引。我的doucment 索引中有多个100000 记录。

如何一次获取所有 100000 记录。

我已经尝试使用searchSourceBuilder.size(10000);,然后它的索引直到10K 记录不超过此,并且这种方法不允许我提供超过10000 的大小。

请在下面找到我正在使用的 java 代码。

public class DocumentIndex {

private final static String INDEX = "documents";  
private final static String ATTACHMENT = "document_attachment"; 
private final static String TYPE = "doc";
private static final Logger logger = Logger.getLogger(Thread.currentThread().getStackTrace()[0].getClassName());

public static void main(String args[]) throws IOException {


    RestHighLevelClient restHighLevelClient = null;
    Document doc=new Document();

    logger.info("Started Indexing the Document.....");

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


    //Fetching Id, FilePath & FileName from Document Index. 
    SearchRequest searchRequest = new SearchRequest(INDEX); 
    searchRequest.types(TYPE);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    QueryBuilder qb = QueryBuilders.matchAllQuery();
    searchSourceBuilder.query(qb);
    //searchSourceBuilder.size(10000); 
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = null;
    try {
         searchResponse = restHighLevelClient.search(searchRequest);
    } catch (IOException e) {
        e.getLocalizedMessage();
    }

    SearchHit[] searchHits = searchResponse.getHits().getHits();
    long totalHits=searchResponse.getHits().totalHits;
    logger.info("Total Hits --->"+totalHits);


    File all_files_path = new File("d:\\All_Files_Path.txt");
    File available_files = new File("d:\\Available_Files.txt");
    File missing_files = new File("d:\\Missing_Files.txt");
    all_files_path.deleteOnExit();
    available_files.deleteOnExit();
    missing_files.deleteOnExit();
    all_files_path.createNewFile();
    available_files.createNewFile();
    missing_files.createNewFile();

    int totalFilePath=1;
    int totalAvailableFile=1;
    int missingFilecount=1;

    Map<String, Object> jsonMap ;
    for (SearchHit hit : searchHits) {

        String encodedfile = null;
        File file=null;

        Map<String, Object> sourceAsMap = hit.getSourceAsMap();


        if(sourceAsMap != null) {  
            doc.setId((int) sourceAsMap.get("id"));
            doc.setApp_language(String.valueOf(sourceAsMap.get("app_language")));
        }

        String filepath=doc.getPath().concat(doc.getFilename());



        try(PrintWriter out = new PrintWriter(new FileOutputStream(all_files_path, true))  ){
            out.println("FilePath Count ---"+totalFilePath+":::::::ID---> "+doc.getId()+"File Path --->"+filepath);
        }

        file = new File(filepath);
        if(file.exists() && !file.isDirectory()) {
            try {
                  try(PrintWriter out = new PrintWriter(new FileOutputStream(available_files, true))  ){
                        out.println("Available File Count --->"+totalAvailableFile+":::::::ID---> "+doc.getId()+"File Path --->"+filepath);
                        totalAvailableFile++;
                    }
                FileInputStream fileInputStreamReader = new FileInputStream(file);
                byte[] bytes = new byte[(int) file.length()];
                fileInputStreamReader.read(bytes);
                encodedfile = new String(Base64.getEncoder().encodeToString(bytes));
                fileInputStreamReader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        else
        {
            PrintWriter out = new PrintWriter(new FileOutputStream(missing_files, true));
            out.close();
            missingFilecount++;
        }

        jsonMap = new HashMap<>();
        jsonMap.put("id", doc.getId());
        jsonMap.put("app_language", doc.getApp_language());
        jsonMap.put("fileContent", encodedfile);

        String id=Long.toString(doc.getId());

        IndexRequest request = new IndexRequest(ATTACHMENT, "doc", id )
                .source(jsonMap)
                .setPipeline(ATTACHMENT);

        PrintStream printStream = new PrintStream(new File("d:\\exception.txt"));
        try {
            IndexResponse response = restHighLevelClient.index(request);

        } catch(ElasticsearchException e) {
            if (e.status() == RestStatus.CONFLICT) {
            }
            e.printStackTrace(printStream);
        }

        totalFilePath++;


    }

    logger.info("Indexing done.....");
}

}

【问题讨论】:

    标签: java elasticsearch elastic-stack


    【解决方案1】:

    如果您有足够的内存,请将索引设置index.max_result_window 从 10000 增加到您需要的数字。

    https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#dynamic-index-settings

    但是请注意,这不会无限扩展。搜索请求占用堆内存和时间与 from + size 成正比。此设置用于限制该内存,如果设置得太高,您将耗尽内存。

    最简单的设置方法是通过 REST API:

    PUT /my-index/_settings
    {
        "index" : {
            "max_result_window" : 150000
        }
    }
    

    【讨论】:

    • 谢谢。对我来说,这是一次活动。只有一次我必须获取100K 记录并且必须使用另一个名称进行索引。所以我可以遵循这种方法仪式。?我可以在哪里设置index.max_result_window: 150000 这个? .在elasticsearch.yml 文件中。 ?
    • 我已经用如何设置的示例更新了答案。
    • 注意 - 如果要复制到另一个索引,您可以考虑使用重新索引 API:elastic.co/guide/en/elasticsearch/reference/current/…
    • 实际上,我必须从第一个索引(Oracle DB 中可用的文件路径)读取filepath,然后在filepath 的帮助下读取本地驱动器中的实际文件,然后我必须将文件内容附加在同一索引中(如果可能)。我是 ES 新手,因此正在创建另一个索引并将文件内容附加到新的(第二个)索引中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多