@Test @SuppressWarnings("unchecked") public void getAccountsByMultiAggsTest2() throws Exception { final ElasticSearchRepository classUnderTest = PowerMockito.spy(new ElasticSearchRepository()); ReflectionTestUtils.setField(classUnderTest, "esUrl", "esUrl123"); mockStatic(Strings.class); mockStatic(PacHttpUtils.class); PowerMockito.when(Strings.isNullOrEmpty(anyString())).thenReturn(false); PowerMockito.when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(Exception.class); assertThatThrownBy(() -> classUnderTest.getAccountsByMultiAggs(eq("index"), eq("type"), anyMap(), anyMap(), anyObject(), anyMap(), eq(1)).get("key455")).isInstanceOf(Exception.class); }
public Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter, Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, Map<String, Object> aggsFilter, int size) throws Exception { StringBuilder requestBody = null; StringBuilder urlToQuery = null; Map<String, Object> matchFilters = Maps.newHashMap(); Map<String, String> distribution = new HashMap<String, String>(); if (filter == null) { matchFilters.put("match_all", new HashMap<String, String>()); } else { matchFilters.putAll(filter); } if (null != filter) { urlToQuery = new StringBuilder(esUrl).append(FORWARD_SLASH).append(index); urlToQuery.append(FORWARD_SLASH).append(_SEARCH); requestBody = new StringBuilder("{\"aggs\":{\"accountid\":{\"terms\":{\"field\":\""); requestBody.append("accountid.keyword"); requestBody.append("\",\"size\":" + size + "}," + "\"aggs\":{\"accountname\":{\"terms\":{\"field\":\"accountname.keyword\",\"size\":" + size + "}}}}}}"); } String responseDetails = null; Gson gson = new GsonBuilder().create(); try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); Map<String, Object> response = (Map<String, Object>) gson.fromJson(responseDetails, Map.class); Map<String, Object> aggregations = (Map<String, Object>) response.get("aggregations"); Map<String, Object> name = (Map<String, Object>) aggregations.get("accountid"); List<Map<String, Object>> buckets = (List<Map<String, Object>>) name.get("buckets"); for (int i = 0; i < buckets.size(); i++) { Map<String, Object> bucket = buckets.get(i); Map<String, Object> accName = (Map<String, Object>) bucket.get("accountname"); List<Map<String, Object>> bucketsName = (List<Map<String, Object>>) accName.get("buckets"); for (int j = 0; j < bucketsName.size(); j++) { Map<String, Object> bucketAccName = bucketsName.get(j); distribution.put(bucket.get("key").toString(), bucketAccName.get("key").toString()); } } } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } return distribution; }
ElasticSearchRepository implements Constants { public Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter, Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, Map<String, Object> aggsFilter, int size) throws Exception { StringBuilder requestBody = null; StringBuilder urlToQuery = null; Map<String, Object> matchFilters = Maps.newHashMap(); Map<String, String> distribution = new HashMap<String, String>(); if (filter == null) { matchFilters.put("match_all", new HashMap<String, String>()); } else { matchFilters.putAll(filter); } if (null != filter) { urlToQuery = new StringBuilder(esUrl).append(FORWARD_SLASH).append(index); urlToQuery.append(FORWARD_SLASH).append(_SEARCH); requestBody = new StringBuilder("{\"aggs\":{\"accountid\":{\"terms\":{\"field\":\""); requestBody.append("accountid.keyword"); requestBody.append("\",\"size\":" + size + "}," + "\"aggs\":{\"accountname\":{\"terms\":{\"field\":\"accountname.keyword\",\"size\":" + size + "}}}}}}"); } String responseDetails = null; Gson gson = new GsonBuilder().create(); try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); Map<String, Object> response = (Map<String, Object>) gson.fromJson(responseDetails, Map.class); Map<String, Object> aggregations = (Map<String, Object>) response.get("aggregations"); Map<String, Object> name = (Map<String, Object>) aggregations.get("accountid"); List<Map<String, Object>> buckets = (List<Map<String, Object>>) name.get("buckets"); for (int i = 0; i < buckets.size(); i++) { Map<String, Object> bucket = buckets.get(i); Map<String, Object> accName = (Map<String, Object>) bucket.get("accountname"); List<Map<String, Object>> bucketsName = (List<Map<String, Object>>) accName.get("buckets"); for (int j = 0; j < bucketsName.size(); j++) { Map<String, Object> bucketAccName = bucketsName.get(j); distribution.put(bucket.get("key").toString(), bucketAccName.get("key").toString()); } } } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } return distribution; } }
ElasticSearchRepository implements Constants { public Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter, Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, Map<String, Object> aggsFilter, int size) throws Exception { StringBuilder requestBody = null; StringBuilder urlToQuery = null; Map<String, Object> matchFilters = Maps.newHashMap(); Map<String, String> distribution = new HashMap<String, String>(); if (filter == null) { matchFilters.put("match_all", new HashMap<String, String>()); } else { matchFilters.putAll(filter); } if (null != filter) { urlToQuery = new StringBuilder(esUrl).append(FORWARD_SLASH).append(index); urlToQuery.append(FORWARD_SLASH).append(_SEARCH); requestBody = new StringBuilder("{\"aggs\":{\"accountid\":{\"terms\":{\"field\":\""); requestBody.append("accountid.keyword"); requestBody.append("\",\"size\":" + size + "}," + "\"aggs\":{\"accountname\":{\"terms\":{\"field\":\"accountname.keyword\",\"size\":" + size + "}}}}}}"); } String responseDetails = null; Gson gson = new GsonBuilder().create(); try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); Map<String, Object> response = (Map<String, Object>) gson.fromJson(responseDetails, Map.class); Map<String, Object> aggregations = (Map<String, Object>) response.get("aggregations"); Map<String, Object> name = (Map<String, Object>) aggregations.get("accountid"); List<Map<String, Object>> buckets = (List<Map<String, Object>>) name.get("buckets"); for (int i = 0; i < buckets.size(); i++) { Map<String, Object> bucket = buckets.get(i); Map<String, Object> accName = (Map<String, Object>) bucket.get("accountname"); List<Map<String, Object>> bucketsName = (List<Map<String, Object>>) accName.get("buckets"); for (int j = 0; j < bucketsName.size(); j++) { Map<String, Object> bucketAccName = bucketsName.get(j); distribution.put(bucket.get("key").toString(), bucketAccName.get("key").toString()); } } } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } return distribution; }  }
ElasticSearchRepository implements Constants { public Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter, Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, Map<String, Object> aggsFilter, int size) throws Exception { StringBuilder requestBody = null; StringBuilder urlToQuery = null; Map<String, Object> matchFilters = Maps.newHashMap(); Map<String, String> distribution = new HashMap<String, String>(); if (filter == null) { matchFilters.put("match_all", new HashMap<String, String>()); } else { matchFilters.putAll(filter); } if (null != filter) { urlToQuery = new StringBuilder(esUrl).append(FORWARD_SLASH).append(index); urlToQuery.append(FORWARD_SLASH).append(_SEARCH); requestBody = new StringBuilder("{\"aggs\":{\"accountid\":{\"terms\":{\"field\":\""); requestBody.append("accountid.keyword"); requestBody.append("\",\"size\":" + size + "}," + "\"aggs\":{\"accountname\":{\"terms\":{\"field\":\"accountname.keyword\",\"size\":" + size + "}}}}}}"); } String responseDetails = null; Gson gson = new GsonBuilder().create(); try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); Map<String, Object> response = (Map<String, Object>) gson.fromJson(responseDetails, Map.class); Map<String, Object> aggregations = (Map<String, Object>) response.get("aggregations"); Map<String, Object> name = (Map<String, Object>) aggregations.get("accountid"); List<Map<String, Object>> buckets = (List<Map<String, Object>>) name.get("buckets"); for (int i = 0; i < buckets.size(); i++) { Map<String, Object> bucket = buckets.get(i); Map<String, Object> accName = (Map<String, Object>) bucket.get("accountname"); List<Map<String, Object>> bucketsName = (List<Map<String, Object>>) accName.get("buckets"); for (int j = 0; j < bucketsName.size(); j++) { Map<String, Object> bucketAccName = bucketsName.get(j); distribution.put(bucket.get("key").toString(), bucketAccName.get("key").toString()); } } } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } return distribution; }  @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromES(String dataSource, String targetType, Map<String, Object> mustFilter,
			final Map<String, Object> mustNotFilter, final HashMultimap<String, Object> shouldFilter,
			List<String> fields, Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromES(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			List<Map<String, Object>> sortFieldMapList); Map<String, Object> buildAggs(String distributionName, int size, String aggsName, Map<String, Object> nestedAggs); Map<String, Object> buildQuery(final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, final String searchText,
			final Map<String, Object> mustTermsFilter,Map<String, List<String>> matchPhrasePrefix); String buildScrollRequest(String scrollId, String esPageScrollTtl); String processResponseAndSendTheScrollBack(String responseDetails, List<Map<String, Object>> results); @SuppressWarnings("unchecked") long getTotalDocumentCountForIndexAndType(String index, String type, Map<String, Object> mustFilter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter); Map<String, Long> getTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, Map<String, Object> mustTermsFilter); String buildAggsURL(String url, String index, String type); List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup); @Deprecated List<Map<String, Object>> getDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); List<Map<String, Object>> getSortedDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter, List<Map<String, Object>> sortFieldMapList); @SuppressWarnings("unchecked") Boolean updatePartialDataToES(String dataSource, String targetType, String id, String routing, String parent,
			Map<String, Object> partialDocument); @SuppressWarnings("unchecked") Boolean saveExceptionDataToES(String dataSource, String routing, Map<String, Object> data); Map<String, Long> getTotalDistributionForIndexAndTypeBySize(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, int from, String searchText); Map<String, Long> getDateHistogramForIndexAndTypeByInterval(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String dateFieldName, String interval); Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter,
			Map<String, Object> aggsFilter, int size); List<LinkedHashMap<String, Object>> getDetailsFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter,
			List<Map<String, Object>> sortFieldMapList); long getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") long getTotalDistributionForIndexAndTypeWithMatchPhrase(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter,
			Map<String, List<String>> matchPhrasePrefix); String buildESURL(String url, String index, String type, int size, int from); Map<String, Object> getEnvAndTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, Map<String, Object> nestedaggs, int size, Map<String, Object> mustTermsFilter); }
ElasticSearchRepository implements Constants { public Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter, Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, Map<String, Object> aggsFilter, int size) throws Exception { StringBuilder requestBody = null; StringBuilder urlToQuery = null; Map<String, Object> matchFilters = Maps.newHashMap(); Map<String, String> distribution = new HashMap<String, String>(); if (filter == null) { matchFilters.put("match_all", new HashMap<String, String>()); } else { matchFilters.putAll(filter); } if (null != filter) { urlToQuery = new StringBuilder(esUrl).append(FORWARD_SLASH).append(index); urlToQuery.append(FORWARD_SLASH).append(_SEARCH); requestBody = new StringBuilder("{\"aggs\":{\"accountid\":{\"terms\":{\"field\":\""); requestBody.append("accountid.keyword"); requestBody.append("\",\"size\":" + size + "}," + "\"aggs\":{\"accountname\":{\"terms\":{\"field\":\"accountname.keyword\",\"size\":" + size + "}}}}}}"); } String responseDetails = null; Gson gson = new GsonBuilder().create(); try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); Map<String, Object> response = (Map<String, Object>) gson.fromJson(responseDetails, Map.class); Map<String, Object> aggregations = (Map<String, Object>) response.get("aggregations"); Map<String, Object> name = (Map<String, Object>) aggregations.get("accountid"); List<Map<String, Object>> buckets = (List<Map<String, Object>>) name.get("buckets"); for (int i = 0; i < buckets.size(); i++) { Map<String, Object> bucket = buckets.get(i); Map<String, Object> accName = (Map<String, Object>) bucket.get("accountname"); List<Map<String, Object>> bucketsName = (List<Map<String, Object>>) accName.get("buckets"); for (int j = 0; j < bucketsName.size(); j++) { Map<String, Object> bucketAccName = bucketsName.get(j); distribution.put(bucket.get("key").toString(), bucketAccName.get("key").toString()); } } } catch (Exception e) { LOGGER.error(ERROR_RETRIEVING_INVENTORY_FROM_ES, e); throw e; } return distribution; }  @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromES(String dataSource, String targetType, Map<String, Object> mustFilter,
			final Map<String, Object> mustNotFilter, final HashMultimap<String, Object> shouldFilter,
			List<String> fields, Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromES(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			List<Map<String, Object>> sortFieldMapList); Map<String, Object> buildAggs(String distributionName, int size, String aggsName, Map<String, Object> nestedAggs); Map<String, Object> buildQuery(final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, final String searchText,
			final Map<String, Object> mustTermsFilter,Map<String, List<String>> matchPhrasePrefix); String buildScrollRequest(String scrollId, String esPageScrollTtl); String processResponseAndSendTheScrollBack(String responseDetails, List<Map<String, Object>> results); @SuppressWarnings("unchecked") long getTotalDocumentCountForIndexAndType(String index, String type, Map<String, Object> mustFilter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter); Map<String, Long> getTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, Map<String, Object> mustTermsFilter); String buildAggsURL(String url, String index, String type); List<Map<String, Object>> getUtilizationByAssetGroup(String asseGroup); @Deprecated List<Map<String, Object>> getDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); List<Map<String, Object>> getSortedDataFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter, List<Map<String, Object>> sortFieldMapList); @SuppressWarnings("unchecked") Boolean updatePartialDataToES(String dataSource, String targetType, String id, String routing, String parent,
			Map<String, Object> partialDocument); @SuppressWarnings("unchecked") Boolean saveExceptionDataToES(String dataSource, String routing, Map<String, Object> data); Map<String, Long> getTotalDistributionForIndexAndTypeBySize(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, int size, int from, String searchText); Map<String, Long> getDateHistogramForIndexAndTypeByInterval(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String dateFieldName, String interval); Map<String, String> getAccountsByMultiAggs(String index, String type, Map<String, Object> filter,
			Map<String, Object> mustNotFilter, HashMultimap<String, Object> shouldFilter,
			Map<String, Object> aggsFilter, int size); List<LinkedHashMap<String, Object>> getDetailsFromESBySize(String dataSource, String targetType,
			final Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, int from, int size, String searchText,
			final Map<String, Object> mustTermsFilter); @SuppressWarnings("unchecked") @Deprecated List<Map<String, Object>> getDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") List<Map<String, Object>> getSortedDataFromESWithMustNotTermsFilter(String dataSource, String targetType,
			Map<String, Object> mustFilter, final Map<String, Object> mustNotFilter,
			final HashMultimap<String, Object> shouldFilter, List<String> fields, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter,
			List<Map<String, Object>> sortFieldMapList); long getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText, Map<String, Object> mustTermsFilter,
			Map<String, Object> mustNotTermsFilter, Map<String, Object> mustNotWildCardFilter); @SuppressWarnings("unchecked") long getTotalDistributionForIndexAndTypeWithMatchPhrase(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String searchText,
			Map<String, Object> mustTermsFilter,
			Map<String, List<String>> matchPhrasePrefix); String buildESURL(String url, String index, String type, int size, int from); Map<String, Object> getEnvAndTotalDistributionForIndexAndType(String index, String type,
			Map<String, Object> mustFilter, Map<String, Object> mustNotFilter,
			HashMultimap<String, Object> shouldFilter, String aggsFilter, Map<String, Object> nestedaggs, int size, Map<String, Object> mustTermsFilter);  }