Elastic Java Scroll API Exception

I am experiencing this error while using the Java Scroll API
"root_cause":[{"type":"exception","reason":"Trying to create too many scroll contexts

I do multiple searches multiple times inside different indexes, so I need to use the Scroll API, here's my code:

	    while (searchHits != null && searchHits.length > 0) {
			SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
			scrollRequest.scroll(scroll);
			searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
			scrollId = searchResponse.getScrollId();
			searchHits = searchResponse.getHits().getHits();

			// Este FOR Caminha nas Instituições
			int cont = 0;
			for (SearchHit docInstitution : searchHits) {
				Map<String, Object> institutionInfo = docInstitution.getSourceAsMap();
				String cod_institution = (String) institutionInfo.get("CO_IES");
				JsonObject jsonDoc = new JsonObject();

				SearchHits docsCurso = infoCurso(client, "CO_IES", cod_institution);

				// Este FOR Caminha nos Cursos da Instituição pelo seu Código
				for (SearchHit docCurso : docsCurso) {

					Map<String, Object> cursoInfo = docCurso.getSourceAsMap();
					String cod_curso = (String) cursoInfo.get("CO_CURSO");

					// Quantidade de alunos por cor neste curso desta Instituição
					int[] colorsInCourse = quantityOfStudentsByColorPerCourse(client, "CO_CURSO", cod_curso);
					jsonDoc.addProperty("qtdNaoDeclarado", colorsInCourse[0]);
					jsonDoc.addProperty("qtdBranca", colorsInCourse[1]);
					jsonDoc.addProperty("qtdPreta", colorsInCourse[2]);
					jsonDoc.addProperty("qtdParda", colorsInCourse[3]);
					jsonDoc.addProperty("qtdAmarela", colorsInCourse[4]);
					jsonDoc.addProperty("qtdIndigena", colorsInCourse[5]);
					jsonDoc.addProperty("qtdSemInformacao", colorsInCourse[6]);

					// Adiciona ao JSON os grupos de idade do curso.
					int[] grupoDeIdade = ageGroupsPerCourse(client, "CO_CURSO", cod_curso);
					jsonDoc.addProperty("qtdMatriculas_13_a_17", grupoDeIdade[0]);
					jsonDoc.addProperty("qtdMatriculas_18_a_24", grupoDeIdade[1]);
					jsonDoc.addProperty("qtdMatriculas_25_a_39", grupoDeIdade[2]);
					jsonDoc.addProperty("qtdMatriculas_40_a_59", grupoDeIdade[3]);
					jsonDoc.addProperty("qtdMatriculas_60_plus", grupoDeIdade[4]);

					// Adiciona ao JSON Todos Campos referentes ao Curso
					for (Entry<String, Object> entry : cursoInfo.entrySet()) {
						try {
							if (entry.getKey().toString().equals("CO_MUNICIPIO")
									&& !entry.getValue().toString().equals("")
									&& !entry.getValue().toString().toLowerCase().trim().equals("null")) {
								Map<String, Object> docMuni = EnsinoSuperior.infoMunicipio(client, "CodMunicipio6",
										(String) entry.getValue());
								jsonDoc.addProperty("CO_MUNICIPIO", (String) entry.getValue());
								try {
									jsonDoc.addProperty("CodMunicipio", (String) docMuni.get("CodMunicipio"));
									jsonDoc.addProperty("Mesorregiao", (String) docMuni.get("Mesorregiao"));
									jsonDoc.addProperty("Municipio", (String) docMuni.get("Municipio"));
									jsonDoc.addProperty("Microrregiao", (String) docMuni.get("Microrregiao"));
									jsonDoc.addProperty("Regiao", (String) docMuni.get("regiao"));
									jsonDoc.addProperty("location", (String) docMuni.get("location"));
									jsonDoc.addProperty("UF", (String) docMuni.get("UF"));
								} catch (Exception NullPointerException) {
									continue;
								}
							}
						} catch (Exception NullPointerException) {
							continue;
						}
						jsonDoc.addProperty(entry.getKey(), (String) cursoInfo.get(entry.getKey()));
					}

					// Adiciona ao JSON Todos Campos referentes a Instituição
					for (Entry<String, Object> entry : institutionInfo.entrySet()) {
						jsonDoc.addProperty(entry.getKey(), (String) institutionInfo.get(entry.getKey()));
					}
					IndexRequest indexRequest = new IndexRequest("ensino_superior_totalizado", "_doc")
							.source(jsonDoc.toString(), XContentType.JSON);
					IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
					cont += 1;
					if (cont % 100 == 0) {
						System.out.println("Quantidade de documentos importados: " + cont);
					}
				}

			}
		}
		ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
		clearScrollRequest.addScrollId(scrollId);
		ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
		boolean succeeded = clearScrollResponse.isSucceeded();

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.