@Test public void testCreateFacetDSLQuery() throws InvalidParseOperationException, InvalidCreateOperationException { final Map<String, Object> queryMap = new HashMap(); List<FacetDateRangeItem> ranges = Arrays.asList(new FacetDateRangeItem("800", "2018")); queryMap.put("titleAndDescription", "Arch"); List<FacetFiltersItem> titlesfilters = Arrays.asList(new FacetFiltersItem("title_fr", QueryHelper.exists("Title_.fr").getCurrentObject()), new FacetFiltersItem("title_en", QueryHelper.exists("Title_.en").getCurrentObject()) ); List<FacetFiltersItem> descriptionfilters = Arrays .asList(new FacetFiltersItem("Description_fr", QueryHelper.exists("Description_.fr").getCurrentObject()), new FacetFiltersItem("Description_en", QueryHelper.exists("Description_.en").getCurrentObject()) ); List<FacetFiltersItem> objectfilters = Arrays .asList(new FacetFiltersItem("ExistsObject", QueryHelper.exists("#object").getCurrentObject()), new FacetFiltersItem("MissingObject", QueryHelper.missing("#object").getCurrentObject()) ); List<FacetItem> facetItems = Arrays.asList( new FacetItem("NestedFacet", FacetType.TERMS, "#qualifiers.versions.FormatIdentification.FormatLitteral",100, FacetOrder.ASC, null, null, null, Optional.empty()), new FacetItem("DescriptionLevelFacet", FacetType.TERMS, "DescriptionLevel", 100, FacetOrder.ASC, null, null, null, Optional.empty()), new FacetItem("OriginatingAgencyFacet", FacetType.TERMS, "#originating_agency", 100, FacetOrder.ASC, null, null, null, Optional.empty()), new FacetItem("StartDateFacet", FacetType.DATE_RANGE, "StartDate", null, null, "yyyy", ranges, null, Optional.empty()), new FacetItem("endDateFacet", FacetType.DATE_RANGE, "EndDate", null, null, "yyyy", ranges, null, Optional.empty()), new FacetItem("LanguageTitleFacet", FacetType.FILTERS, null, null, null, null, null, titlesfilters, Optional.empty()), new FacetItem("LanguageFacet", FacetType.TERMS, "Language", 100, FacetOrder.ASC, null, null, null, Optional.empty()), new FacetItem("LanguageDescFacet", FacetType.FILTERS, null, null, null, null, null, descriptionfilters, Optional.empty()), new FacetItem("ObjectFacet", FacetType.FILTERS, null, null, null, null, null, objectfilters, Optional.empty()) ); queryMap.put("facets", facetItems); final HashMap<String, String> sortSetting = new HashMap(); sortSetting.put("field", "date"); sortSetting.put("sortType", "DESC"); queryMap.put("orderby", sortSetting); queryMap.put("projection_", "#id"); queryMap.put(UiConstants.SELECT_BY_ID.toString(), "1"); final JsonNode selectRequest = dslQueryHelper.createSelectElasticsearchDSLQuery(queryMap); assertNotNull(selectRequest); final RequestParserMultiple selectParser = RequestParserHelper.getParser(selectRequest); assertTrue(selectParser instanceof SelectParserMultiple); assertTrue(selectParser.getRequest().getNbQueries() == 1); assertTrue(selectParser.getRequest().getFilter().get("$orderby") != null); assertTrue(selectParser.getRequest().getProjection().size() == 1); assertTrue(selectParser.getRequest().getFacets().size() == 9); assertTrue(selectParser.getRequest().getFacets().get(0).toString().contains( "{\"$name\":\"NestedFacet\",\"$terms\":{\"$field\":\"#qualifiers.versions.FormatIdentification.FormatLitteral\",\"$subobject\":\"#qualifiers.versions\",\"$size\":100,\"$order\":\"ASC\"}}")); assertTrue(selectParser.getRequest().getFacets().get(1).toString().contains( "{\"$name\":\"DescriptionLevelFacet\",\"$terms\":{\"$field\":\"DescriptionLevel\",\"$size\":100,\"$order\":\"ASC\"}}")); assertTrue(selectParser.getRequest().getFacets().get(2).toString().contains( "{\"$name\":\"OriginatingAgencyFacet\",\"$terms\":{\"$field\":\"#originating_agency\",\"$size\":100,\"$order\":\"ASC\"}}")); assertTrue(selectParser.getRequest().getFacets().get(3).toString().contains( "{\"$name\":\"StartDateFacet\",\"$date_range\":{\"$field\":\"StartDate\",\"$format\":\"yyyy\",\"$ranges\":[{\"$from\":\"800\",\"$to\":\"2018\"}]}}")); assertTrue(selectParser.getRequest().getFacets().get(4).toString().contains( "{\"$name\":\"endDateFacet\",\"$date_range\":{\"$field\":\"EndDate\",\"$format\":\"yyyy\",\"$ranges\":[{\"$from\":\"800\",\"$to\":\"2018\"}]}}")); assertTrue(selectParser.getRequest().getFacets().get(5).toString().contains( "{\"$name\":\"LanguageTitleFacet\",\"$filters\":{\"$query_filters\":[{\"$name\":\"title_fr\",\"$query\":{\"$exists\":\"Title_.fr\"}},{\"$name\":\"title_en\",\"$query\":{\"$exists\":\"Title_.en\"}}]}}")); assertTrue(selectParser.getRequest().getFacets().get(6).toString().contains( "{\"$name\":\"LanguageFacet\",\"$terms\":{\"$field\":\"Language\",\"$size\":100,\"$order\":\"ASC\"}}")); assertTrue(selectParser.getRequest().getFacets().get(7).toString().contains( "{\"$name\":\"LanguageDescFacet\",\"$filters\":{\"$query_filters\":[{\"$name\":\"Description_fr\",\"$query\":{\"$exists\":\"Description_.fr\"}},{\"$name\":\"Description_en\",\"$query\":{\"$exists\":\"Description_.en\"}}]}}")); assertTrue(selectParser.getRequest().getFacets().get(8).toString().contains( "{\"$name\":\"ObjectFacet\",\"$filters\":{\"$query_filters\":[{\"$name\":\"ExistsObject\",\"$query\":{\"$exists\":\"#object\"}},{\"$name\":\"MissingObject\",\"$query\":{\"$missing\":\"#object\"}}]}}")); }
public JsonNode createSelectElasticsearchDSLQuery(Map<String, Object> searchCriteriaMap) throws InvalidParseOperationException, InvalidCreateOperationException { final SelectMultiQuery select = new SelectMultiQuery(); final BooleanQuery andQuery = and(); BooleanQuery nestedSubQuery = null; final BooleanQuery booleanQueries = or(); boolean advancedFacetQuery = false; String startDate = null; String endDate = null; String advancedSearchFlag = ""; String ruleCategory = null; String ruleFinalAction = null; String ruleEndDate = null; String fileSize = null; String fileSizeOperator = null; for (final Entry<String, Object> entry : searchCriteriaMap.entrySet()) { final String searchKeys = entry.getKey(); final Object searchValue = entry.getValue(); if (searchKeys.isEmpty() || searchValue == null) { throw new InvalidParseOperationException("Parameters should not be empty or null"); } if (searchKeys.startsWith(PROJECTION_PREFIX)) { select.addUsedProjection((String) searchValue); continue; } if (searchKeys.startsWith(FACETS_PREFIX)) { List<FacetItem> facetSettings = (List<FacetItem>) searchValue; for (int i = 0; i < facetSettings.size(); i++) { FacetItem facetItem = JsonHandler .getFromString(JsonHandler.writeAsString(facetSettings.get(i)), FacetItem.class); if (facetItem.getFacetType() != null) { switch (facetItem.getFacetType()) { case TERMS: select.addFacets(new TermsFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getSize(), facetItem.getOrder())); break; case DATE_RANGE: List<RangeFacetValue> ranges = facetItem.getRanges().stream() .map(range -> new RangeFacetValue(range.getDateMin(), range.getDateMax())) .collect(Collectors.toList()); select.addFacets( new DateRangeFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getFormat(), ranges)); break; case FILTERS: Map<String, Query> filters = new HashMap<>(); facetItem.getFilters().forEach(filter -> { if (filter.getQuery().get(EXISTS) != null) { try { filters.put(filter.getName(), QueryHelper.exists(filter.getQuery().get(EXISTS).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } else if (filter.getQuery().get(MISSING) != null) { try { filters.put(filter.getName(), QueryHelper.missing(filter.getQuery().get(MISSING).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } }); select.addFacets(new FiltersFacet(facetItem.getName(), filters)); break; default: break; } } } continue; } if (searchKeys.equals(ORDER_BY)) { Map<String, String> sortSetting = (Map<String, String>) searchValue; String sortField = sortSetting.get(SORT_FIELD_ENTRY); String sortType = sortSetting.get(SORT_TYPE_ENTRY); if (ASC_SORT_TYPE.equalsIgnoreCase(sortType)) { select.addOrderByAscFilter(sortField); } else { select.addOrderByDescFilter(sortField); } continue; } if (searchKeys.equals(UiConstants.SELECT_BY_ID.toString())) { select.addRoots((String) searchValue); continue; } if (searchKeys.equalsIgnoreCase(UNITUPS)) { andQuery.add(in(UiConstants.UNITUPS.getResultCriteria(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ROOTS)) { List<String> list = (List) searchValue; String[] roots = list.toArray(new String[list.size()]); select.addRoots(roots); continue; } if (searchKeys.equals(TITLE_AND_DESCRIPTION)) { booleanQueries.add(match(TITLE, (String) searchValue)); booleanQueries.add(match(TITLE_FR, (String) searchValue)); booleanQueries.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(UiConstants.ID.getReceivedCriteria())) { BooleanQuery idQuery = or().add(eq(UiConstants.ID.getResultCriteria(), (String) searchValue)); idQuery.add(eq("FilePlanPosition",(String) searchValue)); idQuery.add(eq("OriginatingSystemId",(String) searchValue)); idQuery.add(eq("OriginatingAgencySystemId",(String) searchValue)); idQuery.add(eq("TransferringAgencySystemId",(String) searchValue)); idQuery.add(eq("ArchivalAgencySystemId",(String) searchValue)); andQuery.add(idQuery); continue; } if (searchKeys.equals(ELIMINATION_OPERATION_ID)) { andQuery.add(eq(VitamFieldsHelper.elimination() + ".OperationId", (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(TITLE)) { andQuery.add(or().add(match(TITLE, (String) searchValue)).add(match(TITLE_FR, (String) searchValue))); continue; } if (searchKeys.equalsIgnoreCase(DESCRIPTION)) { andQuery.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCY)) { andQuery.add(eq(VitamFieldsHelper.originatingAgency(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCIES)) { andQuery.add(eq(VitamFieldsHelper.originatingAgencies(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_FORMAT_ID)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_FORMAT_ID_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_USAGE)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_USAGE_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE)) { fileSize = (String) searchValue; if (fileSizeOperator != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, (String) searchValue)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, (String) searchValue)); } } continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE_OPERATOR)) { fileSizeOperator = (String) searchValue; if (fileSize != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, fileSize)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, fileSize)); } } continue; } if (searchKeys.equalsIgnoreCase(RULE_CATEGORY)) { ruleCategory = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_DATE_SUP)) { ruleEndDate = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_FINAL_ACTION)) { ruleFinalAction = (String) searchValue; } if (searchKeys.startsWith(START_PREFIX)) { startDate = (String) searchValue; continue; } if (searchKeys.startsWith(END_PREFIX)) { endDate = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(ADVANCED_SEARCH_FLAG)) { advancedSearchFlag = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(REQUEST_FACET_PREFIX)) { RequestFacetItem requestFacetItem = JsonHandler .getFromString(JsonHandler.writeAsString(searchValue), RequestFacetItem.class); if (requestFacetItem != null && requestFacetItem.getField() != null && requestFacetItem.getValue() != null) { switch (requestFacetItem.getField()) { case ELIMINATION_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_NON_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_GLOBAL_STATUS_TAG: case ELIMINATION_EXTENDED_INFO_TYPE_TAG: case ORIGINATING_AGENCY_TAG: case DESCRIPTION_LEVEL_TAG: advancedFacetQuery = true; andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); break; case START_DATE: case END_DATE: final String[] values = requestFacetItem.getValue().split("-"); if (values.length == 2) { advancedFacetQuery = true; andQuery.add(gte(requestFacetItem.getField(), values[0]), lte(requestFacetItem.getField(), values[1])); } break; default: if (requestFacetItem.getField().startsWith(TITLE) || requestFacetItem.getField().startsWith(DESCRIPTION)) { andQuery.add(exists(requestFacetItem.getField())); } if (requestFacetItem.getField().startsWith(LANGUAGE)) { andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); } if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITH_OBJECT)) { andQuery.add(exists(requestFacetItem.getField())); } else if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITHOUT_OBJECT)) { andQuery.add(missing(requestFacetItem.getField())); } advancedFacetQuery = true; break; } continue; } } if (searchKeys.equalsIgnoreCase("fieldArchiveUnit") || searchKeys.equalsIgnoreCase("valueArchiveUnit")) { continue; } booleanQueries.add(match(searchKeys, (String) searchValue)); } if (searchCriteriaMap.get("fieldArchiveUnit") != null) { andQuery.add(eq(((String) searchCriteriaMap.get("fieldArchiveUnit")), ((String) searchCriteriaMap.get("valueArchiveUnit")))); } if (!Strings.isNullOrEmpty(endDate) && !Strings.isNullOrEmpty(startDate)) { andQuery.add(createSearchUntisQueryByDate(startDate, endDate)); } if (!Strings.isNullOrEmpty(ruleCategory)) { String managmentRuleCategory = VitamFieldsHelper.management() + "." + ruleCategory; if (!Strings.isNullOrEmpty(ruleFinalAction)) { andQuery.add(eq(managmentRuleCategory + ".FinalAction", ruleFinalAction)); } if (!Strings.isNullOrEmpty(ruleEndDate)) { andQuery.add(lte(managmentRuleCategory + ".Rules.EndDate", ruleEndDate)); } } if (nestedSubQuery != null) { andQuery.add(nestedSearch("#qualifiers.versions", nestedSubQuery.getCurrentQuery())); } boolean noRoots = select.getRoots() == null || select.getRoots().isEmpty(); if (advancedFacetQuery) { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } andQuery.add(booleanQueries); } if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (advancedSearchFlag.equalsIgnoreCase(YES)) { if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } select.addQueries(booleanQueries); } } } return select.getFinalSelect(); }
DslQueryHelper { public JsonNode createSelectElasticsearchDSLQuery(Map<String, Object> searchCriteriaMap) throws InvalidParseOperationException, InvalidCreateOperationException { final SelectMultiQuery select = new SelectMultiQuery(); final BooleanQuery andQuery = and(); BooleanQuery nestedSubQuery = null; final BooleanQuery booleanQueries = or(); boolean advancedFacetQuery = false; String startDate = null; String endDate = null; String advancedSearchFlag = ""; String ruleCategory = null; String ruleFinalAction = null; String ruleEndDate = null; String fileSize = null; String fileSizeOperator = null; for (final Entry<String, Object> entry : searchCriteriaMap.entrySet()) { final String searchKeys = entry.getKey(); final Object searchValue = entry.getValue(); if (searchKeys.isEmpty() || searchValue == null) { throw new InvalidParseOperationException("Parameters should not be empty or null"); } if (searchKeys.startsWith(PROJECTION_PREFIX)) { select.addUsedProjection((String) searchValue); continue; } if (searchKeys.startsWith(FACETS_PREFIX)) { List<FacetItem> facetSettings = (List<FacetItem>) searchValue; for (int i = 0; i < facetSettings.size(); i++) { FacetItem facetItem = JsonHandler .getFromString(JsonHandler.writeAsString(facetSettings.get(i)), FacetItem.class); if (facetItem.getFacetType() != null) { switch (facetItem.getFacetType()) { case TERMS: select.addFacets(new TermsFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getSize(), facetItem.getOrder())); break; case DATE_RANGE: List<RangeFacetValue> ranges = facetItem.getRanges().stream() .map(range -> new RangeFacetValue(range.getDateMin(), range.getDateMax())) .collect(Collectors.toList()); select.addFacets( new DateRangeFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getFormat(), ranges)); break; case FILTERS: Map<String, Query> filters = new HashMap<>(); facetItem.getFilters().forEach(filter -> { if (filter.getQuery().get(EXISTS) != null) { try { filters.put(filter.getName(), QueryHelper.exists(filter.getQuery().get(EXISTS).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } else if (filter.getQuery().get(MISSING) != null) { try { filters.put(filter.getName(), QueryHelper.missing(filter.getQuery().get(MISSING).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } }); select.addFacets(new FiltersFacet(facetItem.getName(), filters)); break; default: break; } } } continue; } if (searchKeys.equals(ORDER_BY)) { Map<String, String> sortSetting = (Map<String, String>) searchValue; String sortField = sortSetting.get(SORT_FIELD_ENTRY); String sortType = sortSetting.get(SORT_TYPE_ENTRY); if (ASC_SORT_TYPE.equalsIgnoreCase(sortType)) { select.addOrderByAscFilter(sortField); } else { select.addOrderByDescFilter(sortField); } continue; } if (searchKeys.equals(UiConstants.SELECT_BY_ID.toString())) { select.addRoots((String) searchValue); continue; } if (searchKeys.equalsIgnoreCase(UNITUPS)) { andQuery.add(in(UiConstants.UNITUPS.getResultCriteria(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ROOTS)) { List<String> list = (List) searchValue; String[] roots = list.toArray(new String[list.size()]); select.addRoots(roots); continue; } if (searchKeys.equals(TITLE_AND_DESCRIPTION)) { booleanQueries.add(match(TITLE, (String) searchValue)); booleanQueries.add(match(TITLE_FR, (String) searchValue)); booleanQueries.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(UiConstants.ID.getReceivedCriteria())) { BooleanQuery idQuery = or().add(eq(UiConstants.ID.getResultCriteria(), (String) searchValue)); idQuery.add(eq("FilePlanPosition",(String) searchValue)); idQuery.add(eq("OriginatingSystemId",(String) searchValue)); idQuery.add(eq("OriginatingAgencySystemId",(String) searchValue)); idQuery.add(eq("TransferringAgencySystemId",(String) searchValue)); idQuery.add(eq("ArchivalAgencySystemId",(String) searchValue)); andQuery.add(idQuery); continue; } if (searchKeys.equals(ELIMINATION_OPERATION_ID)) { andQuery.add(eq(VitamFieldsHelper.elimination() + ".OperationId", (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(TITLE)) { andQuery.add(or().add(match(TITLE, (String) searchValue)).add(match(TITLE_FR, (String) searchValue))); continue; } if (searchKeys.equalsIgnoreCase(DESCRIPTION)) { andQuery.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCY)) { andQuery.add(eq(VitamFieldsHelper.originatingAgency(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCIES)) { andQuery.add(eq(VitamFieldsHelper.originatingAgencies(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_FORMAT_ID)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_FORMAT_ID_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_USAGE)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_USAGE_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE)) { fileSize = (String) searchValue; if (fileSizeOperator != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, (String) searchValue)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, (String) searchValue)); } } continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE_OPERATOR)) { fileSizeOperator = (String) searchValue; if (fileSize != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, fileSize)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, fileSize)); } } continue; } if (searchKeys.equalsIgnoreCase(RULE_CATEGORY)) { ruleCategory = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_DATE_SUP)) { ruleEndDate = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_FINAL_ACTION)) { ruleFinalAction = (String) searchValue; } if (searchKeys.startsWith(START_PREFIX)) { startDate = (String) searchValue; continue; } if (searchKeys.startsWith(END_PREFIX)) { endDate = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(ADVANCED_SEARCH_FLAG)) { advancedSearchFlag = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(REQUEST_FACET_PREFIX)) { RequestFacetItem requestFacetItem = JsonHandler .getFromString(JsonHandler.writeAsString(searchValue), RequestFacetItem.class); if (requestFacetItem != null && requestFacetItem.getField() != null && requestFacetItem.getValue() != null) { switch (requestFacetItem.getField()) { case ELIMINATION_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_NON_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_GLOBAL_STATUS_TAG: case ELIMINATION_EXTENDED_INFO_TYPE_TAG: case ORIGINATING_AGENCY_TAG: case DESCRIPTION_LEVEL_TAG: advancedFacetQuery = true; andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); break; case START_DATE: case END_DATE: final String[] values = requestFacetItem.getValue().split("-"); if (values.length == 2) { advancedFacetQuery = true; andQuery.add(gte(requestFacetItem.getField(), values[0]), lte(requestFacetItem.getField(), values[1])); } break; default: if (requestFacetItem.getField().startsWith(TITLE) || requestFacetItem.getField().startsWith(DESCRIPTION)) { andQuery.add(exists(requestFacetItem.getField())); } if (requestFacetItem.getField().startsWith(LANGUAGE)) { andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); } if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITH_OBJECT)) { andQuery.add(exists(requestFacetItem.getField())); } else if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITHOUT_OBJECT)) { andQuery.add(missing(requestFacetItem.getField())); } advancedFacetQuery = true; break; } continue; } } if (searchKeys.equalsIgnoreCase("fieldArchiveUnit") || searchKeys.equalsIgnoreCase("valueArchiveUnit")) { continue; } booleanQueries.add(match(searchKeys, (String) searchValue)); } if (searchCriteriaMap.get("fieldArchiveUnit") != null) { andQuery.add(eq(((String) searchCriteriaMap.get("fieldArchiveUnit")), ((String) searchCriteriaMap.get("valueArchiveUnit")))); } if (!Strings.isNullOrEmpty(endDate) && !Strings.isNullOrEmpty(startDate)) { andQuery.add(createSearchUntisQueryByDate(startDate, endDate)); } if (!Strings.isNullOrEmpty(ruleCategory)) { String managmentRuleCategory = VitamFieldsHelper.management() + "." + ruleCategory; if (!Strings.isNullOrEmpty(ruleFinalAction)) { andQuery.add(eq(managmentRuleCategory + ".FinalAction", ruleFinalAction)); } if (!Strings.isNullOrEmpty(ruleEndDate)) { andQuery.add(lte(managmentRuleCategory + ".Rules.EndDate", ruleEndDate)); } } if (nestedSubQuery != null) { andQuery.add(nestedSearch("#qualifiers.versions", nestedSubQuery.getCurrentQuery())); } boolean noRoots = select.getRoots() == null || select.getRoots().isEmpty(); if (advancedFacetQuery) { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } andQuery.add(booleanQueries); } if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (advancedSearchFlag.equalsIgnoreCase(YES)) { if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } select.addQueries(booleanQueries); } } } return select.getFinalSelect(); } }
DslQueryHelper { public JsonNode createSelectElasticsearchDSLQuery(Map<String, Object> searchCriteriaMap) throws InvalidParseOperationException, InvalidCreateOperationException { final SelectMultiQuery select = new SelectMultiQuery(); final BooleanQuery andQuery = and(); BooleanQuery nestedSubQuery = null; final BooleanQuery booleanQueries = or(); boolean advancedFacetQuery = false; String startDate = null; String endDate = null; String advancedSearchFlag = ""; String ruleCategory = null; String ruleFinalAction = null; String ruleEndDate = null; String fileSize = null; String fileSizeOperator = null; for (final Entry<String, Object> entry : searchCriteriaMap.entrySet()) { final String searchKeys = entry.getKey(); final Object searchValue = entry.getValue(); if (searchKeys.isEmpty() || searchValue == null) { throw new InvalidParseOperationException("Parameters should not be empty or null"); } if (searchKeys.startsWith(PROJECTION_PREFIX)) { select.addUsedProjection((String) searchValue); continue; } if (searchKeys.startsWith(FACETS_PREFIX)) { List<FacetItem> facetSettings = (List<FacetItem>) searchValue; for (int i = 0; i < facetSettings.size(); i++) { FacetItem facetItem = JsonHandler .getFromString(JsonHandler.writeAsString(facetSettings.get(i)), FacetItem.class); if (facetItem.getFacetType() != null) { switch (facetItem.getFacetType()) { case TERMS: select.addFacets(new TermsFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getSize(), facetItem.getOrder())); break; case DATE_RANGE: List<RangeFacetValue> ranges = facetItem.getRanges().stream() .map(range -> new RangeFacetValue(range.getDateMin(), range.getDateMax())) .collect(Collectors.toList()); select.addFacets( new DateRangeFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getFormat(), ranges)); break; case FILTERS: Map<String, Query> filters = new HashMap<>(); facetItem.getFilters().forEach(filter -> { if (filter.getQuery().get(EXISTS) != null) { try { filters.put(filter.getName(), QueryHelper.exists(filter.getQuery().get(EXISTS).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } else if (filter.getQuery().get(MISSING) != null) { try { filters.put(filter.getName(), QueryHelper.missing(filter.getQuery().get(MISSING).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } }); select.addFacets(new FiltersFacet(facetItem.getName(), filters)); break; default: break; } } } continue; } if (searchKeys.equals(ORDER_BY)) { Map<String, String> sortSetting = (Map<String, String>) searchValue; String sortField = sortSetting.get(SORT_FIELD_ENTRY); String sortType = sortSetting.get(SORT_TYPE_ENTRY); if (ASC_SORT_TYPE.equalsIgnoreCase(sortType)) { select.addOrderByAscFilter(sortField); } else { select.addOrderByDescFilter(sortField); } continue; } if (searchKeys.equals(UiConstants.SELECT_BY_ID.toString())) { select.addRoots((String) searchValue); continue; } if (searchKeys.equalsIgnoreCase(UNITUPS)) { andQuery.add(in(UiConstants.UNITUPS.getResultCriteria(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ROOTS)) { List<String> list = (List) searchValue; String[] roots = list.toArray(new String[list.size()]); select.addRoots(roots); continue; } if (searchKeys.equals(TITLE_AND_DESCRIPTION)) { booleanQueries.add(match(TITLE, (String) searchValue)); booleanQueries.add(match(TITLE_FR, (String) searchValue)); booleanQueries.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(UiConstants.ID.getReceivedCriteria())) { BooleanQuery idQuery = or().add(eq(UiConstants.ID.getResultCriteria(), (String) searchValue)); idQuery.add(eq("FilePlanPosition",(String) searchValue)); idQuery.add(eq("OriginatingSystemId",(String) searchValue)); idQuery.add(eq("OriginatingAgencySystemId",(String) searchValue)); idQuery.add(eq("TransferringAgencySystemId",(String) searchValue)); idQuery.add(eq("ArchivalAgencySystemId",(String) searchValue)); andQuery.add(idQuery); continue; } if (searchKeys.equals(ELIMINATION_OPERATION_ID)) { andQuery.add(eq(VitamFieldsHelper.elimination() + ".OperationId", (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(TITLE)) { andQuery.add(or().add(match(TITLE, (String) searchValue)).add(match(TITLE_FR, (String) searchValue))); continue; } if (searchKeys.equalsIgnoreCase(DESCRIPTION)) { andQuery.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCY)) { andQuery.add(eq(VitamFieldsHelper.originatingAgency(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCIES)) { andQuery.add(eq(VitamFieldsHelper.originatingAgencies(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_FORMAT_ID)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_FORMAT_ID_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_USAGE)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_USAGE_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE)) { fileSize = (String) searchValue; if (fileSizeOperator != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, (String) searchValue)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, (String) searchValue)); } } continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE_OPERATOR)) { fileSizeOperator = (String) searchValue; if (fileSize != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, fileSize)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, fileSize)); } } continue; } if (searchKeys.equalsIgnoreCase(RULE_CATEGORY)) { ruleCategory = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_DATE_SUP)) { ruleEndDate = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_FINAL_ACTION)) { ruleFinalAction = (String) searchValue; } if (searchKeys.startsWith(START_PREFIX)) { startDate = (String) searchValue; continue; } if (searchKeys.startsWith(END_PREFIX)) { endDate = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(ADVANCED_SEARCH_FLAG)) { advancedSearchFlag = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(REQUEST_FACET_PREFIX)) { RequestFacetItem requestFacetItem = JsonHandler .getFromString(JsonHandler.writeAsString(searchValue), RequestFacetItem.class); if (requestFacetItem != null && requestFacetItem.getField() != null && requestFacetItem.getValue() != null) { switch (requestFacetItem.getField()) { case ELIMINATION_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_NON_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_GLOBAL_STATUS_TAG: case ELIMINATION_EXTENDED_INFO_TYPE_TAG: case ORIGINATING_AGENCY_TAG: case DESCRIPTION_LEVEL_TAG: advancedFacetQuery = true; andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); break; case START_DATE: case END_DATE: final String[] values = requestFacetItem.getValue().split("-"); if (values.length == 2) { advancedFacetQuery = true; andQuery.add(gte(requestFacetItem.getField(), values[0]), lte(requestFacetItem.getField(), values[1])); } break; default: if (requestFacetItem.getField().startsWith(TITLE) || requestFacetItem.getField().startsWith(DESCRIPTION)) { andQuery.add(exists(requestFacetItem.getField())); } if (requestFacetItem.getField().startsWith(LANGUAGE)) { andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); } if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITH_OBJECT)) { andQuery.add(exists(requestFacetItem.getField())); } else if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITHOUT_OBJECT)) { andQuery.add(missing(requestFacetItem.getField())); } advancedFacetQuery = true; break; } continue; } } if (searchKeys.equalsIgnoreCase("fieldArchiveUnit") || searchKeys.equalsIgnoreCase("valueArchiveUnit")) { continue; } booleanQueries.add(match(searchKeys, (String) searchValue)); } if (searchCriteriaMap.get("fieldArchiveUnit") != null) { andQuery.add(eq(((String) searchCriteriaMap.get("fieldArchiveUnit")), ((String) searchCriteriaMap.get("valueArchiveUnit")))); } if (!Strings.isNullOrEmpty(endDate) && !Strings.isNullOrEmpty(startDate)) { andQuery.add(createSearchUntisQueryByDate(startDate, endDate)); } if (!Strings.isNullOrEmpty(ruleCategory)) { String managmentRuleCategory = VitamFieldsHelper.management() + "." + ruleCategory; if (!Strings.isNullOrEmpty(ruleFinalAction)) { andQuery.add(eq(managmentRuleCategory + ".FinalAction", ruleFinalAction)); } if (!Strings.isNullOrEmpty(ruleEndDate)) { andQuery.add(lte(managmentRuleCategory + ".Rules.EndDate", ruleEndDate)); } } if (nestedSubQuery != null) { andQuery.add(nestedSearch("#qualifiers.versions", nestedSubQuery.getCurrentQuery())); } boolean noRoots = select.getRoots() == null || select.getRoots().isEmpty(); if (advancedFacetQuery) { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } andQuery.add(booleanQueries); } if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (advancedSearchFlag.equalsIgnoreCase(YES)) { if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } select.addQueries(booleanQueries); } } } return select.getFinalSelect(); } DslQueryHelper(); }
DslQueryHelper { public JsonNode createSelectElasticsearchDSLQuery(Map<String, Object> searchCriteriaMap) throws InvalidParseOperationException, InvalidCreateOperationException { final SelectMultiQuery select = new SelectMultiQuery(); final BooleanQuery andQuery = and(); BooleanQuery nestedSubQuery = null; final BooleanQuery booleanQueries = or(); boolean advancedFacetQuery = false; String startDate = null; String endDate = null; String advancedSearchFlag = ""; String ruleCategory = null; String ruleFinalAction = null; String ruleEndDate = null; String fileSize = null; String fileSizeOperator = null; for (final Entry<String, Object> entry : searchCriteriaMap.entrySet()) { final String searchKeys = entry.getKey(); final Object searchValue = entry.getValue(); if (searchKeys.isEmpty() || searchValue == null) { throw new InvalidParseOperationException("Parameters should not be empty or null"); } if (searchKeys.startsWith(PROJECTION_PREFIX)) { select.addUsedProjection((String) searchValue); continue; } if (searchKeys.startsWith(FACETS_PREFIX)) { List<FacetItem> facetSettings = (List<FacetItem>) searchValue; for (int i = 0; i < facetSettings.size(); i++) { FacetItem facetItem = JsonHandler .getFromString(JsonHandler.writeAsString(facetSettings.get(i)), FacetItem.class); if (facetItem.getFacetType() != null) { switch (facetItem.getFacetType()) { case TERMS: select.addFacets(new TermsFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getSize(), facetItem.getOrder())); break; case DATE_RANGE: List<RangeFacetValue> ranges = facetItem.getRanges().stream() .map(range -> new RangeFacetValue(range.getDateMin(), range.getDateMax())) .collect(Collectors.toList()); select.addFacets( new DateRangeFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getFormat(), ranges)); break; case FILTERS: Map<String, Query> filters = new HashMap<>(); facetItem.getFilters().forEach(filter -> { if (filter.getQuery().get(EXISTS) != null) { try { filters.put(filter.getName(), QueryHelper.exists(filter.getQuery().get(EXISTS).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } else if (filter.getQuery().get(MISSING) != null) { try { filters.put(filter.getName(), QueryHelper.missing(filter.getQuery().get(MISSING).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } }); select.addFacets(new FiltersFacet(facetItem.getName(), filters)); break; default: break; } } } continue; } if (searchKeys.equals(ORDER_BY)) { Map<String, String> sortSetting = (Map<String, String>) searchValue; String sortField = sortSetting.get(SORT_FIELD_ENTRY); String sortType = sortSetting.get(SORT_TYPE_ENTRY); if (ASC_SORT_TYPE.equalsIgnoreCase(sortType)) { select.addOrderByAscFilter(sortField); } else { select.addOrderByDescFilter(sortField); } continue; } if (searchKeys.equals(UiConstants.SELECT_BY_ID.toString())) { select.addRoots((String) searchValue); continue; } if (searchKeys.equalsIgnoreCase(UNITUPS)) { andQuery.add(in(UiConstants.UNITUPS.getResultCriteria(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ROOTS)) { List<String> list = (List) searchValue; String[] roots = list.toArray(new String[list.size()]); select.addRoots(roots); continue; } if (searchKeys.equals(TITLE_AND_DESCRIPTION)) { booleanQueries.add(match(TITLE, (String) searchValue)); booleanQueries.add(match(TITLE_FR, (String) searchValue)); booleanQueries.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(UiConstants.ID.getReceivedCriteria())) { BooleanQuery idQuery = or().add(eq(UiConstants.ID.getResultCriteria(), (String) searchValue)); idQuery.add(eq("FilePlanPosition",(String) searchValue)); idQuery.add(eq("OriginatingSystemId",(String) searchValue)); idQuery.add(eq("OriginatingAgencySystemId",(String) searchValue)); idQuery.add(eq("TransferringAgencySystemId",(String) searchValue)); idQuery.add(eq("ArchivalAgencySystemId",(String) searchValue)); andQuery.add(idQuery); continue; } if (searchKeys.equals(ELIMINATION_OPERATION_ID)) { andQuery.add(eq(VitamFieldsHelper.elimination() + ".OperationId", (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(TITLE)) { andQuery.add(or().add(match(TITLE, (String) searchValue)).add(match(TITLE_FR, (String) searchValue))); continue; } if (searchKeys.equalsIgnoreCase(DESCRIPTION)) { andQuery.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCY)) { andQuery.add(eq(VitamFieldsHelper.originatingAgency(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCIES)) { andQuery.add(eq(VitamFieldsHelper.originatingAgencies(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_FORMAT_ID)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_FORMAT_ID_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_USAGE)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_USAGE_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE)) { fileSize = (String) searchValue; if (fileSizeOperator != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, (String) searchValue)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, (String) searchValue)); } } continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE_OPERATOR)) { fileSizeOperator = (String) searchValue; if (fileSize != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, fileSize)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, fileSize)); } } continue; } if (searchKeys.equalsIgnoreCase(RULE_CATEGORY)) { ruleCategory = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_DATE_SUP)) { ruleEndDate = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_FINAL_ACTION)) { ruleFinalAction = (String) searchValue; } if (searchKeys.startsWith(START_PREFIX)) { startDate = (String) searchValue; continue; } if (searchKeys.startsWith(END_PREFIX)) { endDate = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(ADVANCED_SEARCH_FLAG)) { advancedSearchFlag = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(REQUEST_FACET_PREFIX)) { RequestFacetItem requestFacetItem = JsonHandler .getFromString(JsonHandler.writeAsString(searchValue), RequestFacetItem.class); if (requestFacetItem != null && requestFacetItem.getField() != null && requestFacetItem.getValue() != null) { switch (requestFacetItem.getField()) { case ELIMINATION_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_NON_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_GLOBAL_STATUS_TAG: case ELIMINATION_EXTENDED_INFO_TYPE_TAG: case ORIGINATING_AGENCY_TAG: case DESCRIPTION_LEVEL_TAG: advancedFacetQuery = true; andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); break; case START_DATE: case END_DATE: final String[] values = requestFacetItem.getValue().split("-"); if (values.length == 2) { advancedFacetQuery = true; andQuery.add(gte(requestFacetItem.getField(), values[0]), lte(requestFacetItem.getField(), values[1])); } break; default: if (requestFacetItem.getField().startsWith(TITLE) || requestFacetItem.getField().startsWith(DESCRIPTION)) { andQuery.add(exists(requestFacetItem.getField())); } if (requestFacetItem.getField().startsWith(LANGUAGE)) { andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); } if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITH_OBJECT)) { andQuery.add(exists(requestFacetItem.getField())); } else if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITHOUT_OBJECT)) { andQuery.add(missing(requestFacetItem.getField())); } advancedFacetQuery = true; break; } continue; } } if (searchKeys.equalsIgnoreCase("fieldArchiveUnit") || searchKeys.equalsIgnoreCase("valueArchiveUnit")) { continue; } booleanQueries.add(match(searchKeys, (String) searchValue)); } if (searchCriteriaMap.get("fieldArchiveUnit") != null) { andQuery.add(eq(((String) searchCriteriaMap.get("fieldArchiveUnit")), ((String) searchCriteriaMap.get("valueArchiveUnit")))); } if (!Strings.isNullOrEmpty(endDate) && !Strings.isNullOrEmpty(startDate)) { andQuery.add(createSearchUntisQueryByDate(startDate, endDate)); } if (!Strings.isNullOrEmpty(ruleCategory)) { String managmentRuleCategory = VitamFieldsHelper.management() + "." + ruleCategory; if (!Strings.isNullOrEmpty(ruleFinalAction)) { andQuery.add(eq(managmentRuleCategory + ".FinalAction", ruleFinalAction)); } if (!Strings.isNullOrEmpty(ruleEndDate)) { andQuery.add(lte(managmentRuleCategory + ".Rules.EndDate", ruleEndDate)); } } if (nestedSubQuery != null) { andQuery.add(nestedSearch("#qualifiers.versions", nestedSubQuery.getCurrentQuery())); } boolean noRoots = select.getRoots() == null || select.getRoots().isEmpty(); if (advancedFacetQuery) { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } andQuery.add(booleanQueries); } if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (advancedSearchFlag.equalsIgnoreCase(YES)) { if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } select.addQueries(booleanQueries); } } } return select.getFinalSelect(); } DslQueryHelper(); static DslQueryHelper getInstance(); JsonNode createSingleQueryDSL(Map<String, Object> searchCriteriaMap); JsonNode createSelectDSLQuery(Map<String, String> searchCriteriaMap); JsonNode createGetByIdDSLSelectMultipleQuery(Map<String, String> projectionCriteriaMap); JsonNode createSelectElasticsearchDSLQuery(Map<String, Object> searchCriteriaMap); JsonNode createUpdateByIdDSLQuery(Map<String, JsonNode> searchCriteriaMap,
        Map<String, JsonNode> updateRules); ObjectNode createMassiveUpdateDSLBaseQuery(JsonNode modifiedFields); UpdateMultiQuery getFullMetadataActionQuery(JsonNode metadataModifications); JsonNode createSearchQueryAccessionRegister(Map<String, Object> options); JsonNode createSelectAndUpdateDSLQuery(Map<String, Object> optionsMap); }
DslQueryHelper { public JsonNode createSelectElasticsearchDSLQuery(Map<String, Object> searchCriteriaMap) throws InvalidParseOperationException, InvalidCreateOperationException { final SelectMultiQuery select = new SelectMultiQuery(); final BooleanQuery andQuery = and(); BooleanQuery nestedSubQuery = null; final BooleanQuery booleanQueries = or(); boolean advancedFacetQuery = false; String startDate = null; String endDate = null; String advancedSearchFlag = ""; String ruleCategory = null; String ruleFinalAction = null; String ruleEndDate = null; String fileSize = null; String fileSizeOperator = null; for (final Entry<String, Object> entry : searchCriteriaMap.entrySet()) { final String searchKeys = entry.getKey(); final Object searchValue = entry.getValue(); if (searchKeys.isEmpty() || searchValue == null) { throw new InvalidParseOperationException("Parameters should not be empty or null"); } if (searchKeys.startsWith(PROJECTION_PREFIX)) { select.addUsedProjection((String) searchValue); continue; } if (searchKeys.startsWith(FACETS_PREFIX)) { List<FacetItem> facetSettings = (List<FacetItem>) searchValue; for (int i = 0; i < facetSettings.size(); i++) { FacetItem facetItem = JsonHandler .getFromString(JsonHandler.writeAsString(facetSettings.get(i)), FacetItem.class); if (facetItem.getFacetType() != null) { switch (facetItem.getFacetType()) { case TERMS: select.addFacets(new TermsFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getSize(), facetItem.getOrder())); break; case DATE_RANGE: List<RangeFacetValue> ranges = facetItem.getRanges().stream() .map(range -> new RangeFacetValue(range.getDateMin(), range.getDateMax())) .collect(Collectors.toList()); select.addFacets( new DateRangeFacet(facetItem.getName(), facetItem.getField(), facetItem.getSubobject(), facetItem.getFormat(), ranges)); break; case FILTERS: Map<String, Query> filters = new HashMap<>(); facetItem.getFilters().forEach(filter -> { if (filter.getQuery().get(EXISTS) != null) { try { filters.put(filter.getName(), QueryHelper.exists(filter.getQuery().get(EXISTS).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } else if (filter.getQuery().get(MISSING) != null) { try { filters.put(filter.getName(), QueryHelper.missing(filter.getQuery().get(MISSING).asText())); } catch (InvalidCreateOperationException e) { LOGGER.error(e); } } }); select.addFacets(new FiltersFacet(facetItem.getName(), filters)); break; default: break; } } } continue; } if (searchKeys.equals(ORDER_BY)) { Map<String, String> sortSetting = (Map<String, String>) searchValue; String sortField = sortSetting.get(SORT_FIELD_ENTRY); String sortType = sortSetting.get(SORT_TYPE_ENTRY); if (ASC_SORT_TYPE.equalsIgnoreCase(sortType)) { select.addOrderByAscFilter(sortField); } else { select.addOrderByDescFilter(sortField); } continue; } if (searchKeys.equals(UiConstants.SELECT_BY_ID.toString())) { select.addRoots((String) searchValue); continue; } if (searchKeys.equalsIgnoreCase(UNITUPS)) { andQuery.add(in(UiConstants.UNITUPS.getResultCriteria(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ROOTS)) { List<String> list = (List) searchValue; String[] roots = list.toArray(new String[list.size()]); select.addRoots(roots); continue; } if (searchKeys.equals(TITLE_AND_DESCRIPTION)) { booleanQueries.add(match(TITLE, (String) searchValue)); booleanQueries.add(match(TITLE_FR, (String) searchValue)); booleanQueries.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(UiConstants.ID.getReceivedCriteria())) { BooleanQuery idQuery = or().add(eq(UiConstants.ID.getResultCriteria(), (String) searchValue)); idQuery.add(eq("FilePlanPosition",(String) searchValue)); idQuery.add(eq("OriginatingSystemId",(String) searchValue)); idQuery.add(eq("OriginatingAgencySystemId",(String) searchValue)); idQuery.add(eq("TransferringAgencySystemId",(String) searchValue)); idQuery.add(eq("ArchivalAgencySystemId",(String) searchValue)); andQuery.add(idQuery); continue; } if (searchKeys.equals(ELIMINATION_OPERATION_ID)) { andQuery.add(eq(VitamFieldsHelper.elimination() + ".OperationId", (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(TITLE)) { andQuery.add(or().add(match(TITLE, (String) searchValue)).add(match(TITLE_FR, (String) searchValue))); continue; } if (searchKeys.equalsIgnoreCase(DESCRIPTION)) { andQuery.add(match(DESCRIPTION, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCY)) { andQuery.add(eq(VitamFieldsHelper.originatingAgency(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(ORIGINATING_AGENCIES)) { andQuery.add(eq(VitamFieldsHelper.originatingAgencies(), (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_FORMAT_ID)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_FORMAT_ID_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_USAGE)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(eq(FILE_USAGE_GOT_FIELD, (String) searchValue)); continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE)) { fileSize = (String) searchValue; if (fileSizeOperator != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, (String) searchValue)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, (String) searchValue)); } } continue; } if (searchKeys.equalsIgnoreCase(GOT_FILE_SIZE_OPERATOR)) { fileSizeOperator = (String) searchValue; if (fileSize != null) { if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_LOWER)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(lt(FILE_SIZE_GOT_FIELD, fileSize)); } else if (fileSizeOperator.equals(GOT_FILE_SIZE_OPERATOR_GREATER_OR_EQUAL)) { if (nestedSubQuery == null) { nestedSubQuery = and(); } nestedSubQuery.add(gte(FILE_SIZE_GOT_FIELD, fileSize)); } } continue; } if (searchKeys.equalsIgnoreCase(RULE_CATEGORY)) { ruleCategory = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_DATE_SUP)) { ruleEndDate = (String) searchValue; } if (searchKeys.equalsIgnoreCase(RULE_FINAL_ACTION)) { ruleFinalAction = (String) searchValue; } if (searchKeys.startsWith(START_PREFIX)) { startDate = (String) searchValue; continue; } if (searchKeys.startsWith(END_PREFIX)) { endDate = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(ADVANCED_SEARCH_FLAG)) { advancedSearchFlag = (String) searchValue; continue; } if (searchKeys.equalsIgnoreCase(REQUEST_FACET_PREFIX)) { RequestFacetItem requestFacetItem = JsonHandler .getFromString(JsonHandler.writeAsString(searchValue), RequestFacetItem.class); if (requestFacetItem != null && requestFacetItem.getField() != null && requestFacetItem.getValue() != null) { switch (requestFacetItem.getField()) { case ELIMINATION_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_NON_DESTROYABLE_ORIGINATING_AGENCY_TAG: case ELIMINATION_GLOBAL_STATUS_TAG: case ELIMINATION_EXTENDED_INFO_TYPE_TAG: case ORIGINATING_AGENCY_TAG: case DESCRIPTION_LEVEL_TAG: advancedFacetQuery = true; andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); break; case START_DATE: case END_DATE: final String[] values = requestFacetItem.getValue().split("-"); if (values.length == 2) { advancedFacetQuery = true; andQuery.add(gte(requestFacetItem.getField(), values[0]), lte(requestFacetItem.getField(), values[1])); } break; default: if (requestFacetItem.getField().startsWith(TITLE) || requestFacetItem.getField().startsWith(DESCRIPTION)) { andQuery.add(exists(requestFacetItem.getField())); } if (requestFacetItem.getField().startsWith(LANGUAGE)) { andQuery.add(eq(requestFacetItem.getField(), requestFacetItem.getValue())); } if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITH_OBJECT)) { andQuery.add(exists(requestFacetItem.getField())); } else if (requestFacetItem.getValue().equalsIgnoreCase(FACET_WITHOUT_OBJECT)) { andQuery.add(missing(requestFacetItem.getField())); } advancedFacetQuery = true; break; } continue; } } if (searchKeys.equalsIgnoreCase("fieldArchiveUnit") || searchKeys.equalsIgnoreCase("valueArchiveUnit")) { continue; } booleanQueries.add(match(searchKeys, (String) searchValue)); } if (searchCriteriaMap.get("fieldArchiveUnit") != null) { andQuery.add(eq(((String) searchCriteriaMap.get("fieldArchiveUnit")), ((String) searchCriteriaMap.get("valueArchiveUnit")))); } if (!Strings.isNullOrEmpty(endDate) && !Strings.isNullOrEmpty(startDate)) { andQuery.add(createSearchUntisQueryByDate(startDate, endDate)); } if (!Strings.isNullOrEmpty(ruleCategory)) { String managmentRuleCategory = VitamFieldsHelper.management() + "." + ruleCategory; if (!Strings.isNullOrEmpty(ruleFinalAction)) { andQuery.add(eq(managmentRuleCategory + ".FinalAction", ruleFinalAction)); } if (!Strings.isNullOrEmpty(ruleEndDate)) { andQuery.add(lte(managmentRuleCategory + ".Rules.EndDate", ruleEndDate)); } } if (nestedSubQuery != null) { andQuery.add(nestedSearch("#qualifiers.versions", nestedSubQuery.getCurrentQuery())); } boolean noRoots = select.getRoots() == null || select.getRoots().isEmpty(); if (advancedFacetQuery) { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } andQuery.add(booleanQueries); } if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (advancedSearchFlag.equalsIgnoreCase(YES)) { if (andQuery.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { andQuery.setDepthLimit(DEPTH_LIMIT); } select.addQueries(andQuery); } } else { if (booleanQueries.isReady()) { if (!(noRoots && select.getNbQueries() == 0)) { booleanQueries.setDepthLimit(DEPTH_LIMIT); } select.addQueries(booleanQueries); } } } return select.getFinalSelect(); } DslQueryHelper(); static DslQueryHelper getInstance(); JsonNode createSingleQueryDSL(Map<String, Object> searchCriteriaMap); JsonNode createSelectDSLQuery(Map<String, String> searchCriteriaMap); JsonNode createGetByIdDSLSelectMultipleQuery(Map<String, String> projectionCriteriaMap); JsonNode createSelectElasticsearchDSLQuery(Map<String, Object> searchCriteriaMap); JsonNode createUpdateByIdDSLQuery(Map<String, JsonNode> searchCriteriaMap,
        Map<String, JsonNode> updateRules); ObjectNode createMassiveUpdateDSLBaseQuery(JsonNode modifiedFields); UpdateMultiQuery getFullMetadataActionQuery(JsonNode metadataModifications); JsonNode createSearchQueryAccessionRegister(Map<String, Object> options); JsonNode createSelectAndUpdateDSLQuery(Map<String, Object> optionsMap); static final String PROJECTION_DSL; }