class ComplianceRepositoryImpl implements ComplianceRepository, Constants {

    public JsonArray getRuleDetailsByEnvironmentFromES(String assetGroup, String ruleId, String application,
            String searchText,String targetType) throws DataException {
        String responseJson = null;
        JsonParser jsonParser;
        JsonObject resultJson;
        StringBuilder requestBody = null;
        StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/")
                .append(SEARCH);
        
if (ruleId.contains(TAGGIG_POLICY)) {
        	
        	List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(",")));

            String body = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\""+ruleId+"\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}";

            //Added resourceType to the Query
            char ch='"';
            targetType = ch+targetType+ch;
            
            body = body + ",{\"terms\":{\"targetType.keyword\":["+targetType+"]}}";
            if(application!=null){
            	  body = body + ",{\"match\":{\"tags.Application.keyword\":\""+application+"\"}}";
            }
            body = body + "]";
            if (!tagsList.isEmpty()) {
                body = body + ",\"should\":[";

                for (String tag : tagsList) {
                    body = body + "{\"match_phrase_prefix\":{\"missingTags\":\"" + tag + "\"}},";
                }
                body = body.substring(0, body.length() - 1);
                body = body + "]";
                body = body + ",\"minimum_should_match\":1";
            }
            body = body + "}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000000}}}}";
            requestBody = new StringBuilder(body);
           
        }else{

        if (StringUtils.isEmpty(searchText)) {
            requestBody = new StringBuilder(
                    "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\""
                            + ruleId
                            + "\"}}},{\"term\":{\"tags.Application.keyword\":{\"value\":\""
                            + application
                            + "\"}}}],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"exempted\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000}}}}");
        } else {
            requestBody = new StringBuilder(
                    "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\""
                            + ruleId
                            + "\"}}},{\"term\":{\"tags.Application.keyword\":{\"value\":\""
                            + application
                            + "\"}}},{\"match_phrase_prefix\":{\"_all\":\""
                            + searchText
                            + "\"}}],\"should\":[{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"exempted\"}}}],\"minimum_should_match\":1}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000}}}}");
        }
    }
        try {
            responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString());
        } catch (Exception e) {
            logger.error(ERROR_IN_US, e);
            throw new DataException(e);
        }
        jsonParser = new JsonParser();
        resultJson = (JsonObject) jsonParser.parse(responseJson);
        JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString());
        return aggsJson.getAsJsonObject("NAME").getAsJsonArray(BUCKETS);
    }

}