From 1d7e01e6fea0f47f352ab7fd9824c1aba544163c Mon Sep 17 00:00:00 2001 From: Elia Porciani Date: Fri, 22 May 2020 11:33:53 +0200 Subject: [PATCH] SEARCH-2166 (#257) [SEARCH-2166] Changes for allowing the use of date function fields in insightengine --- .../search/adaptor/lucene/QueryConstants.java | 12 +++ .../repo/search/impl/QueryParserUtils.java | 30 +++++++ .../AlfrescoFunctionEvaluationContext.java | 81 +++++++++++++++++++ 3 files changed, 123 insertions(+) diff --git a/src/main/java/org/alfresco/repo/search/adaptor/lucene/QueryConstants.java b/src/main/java/org/alfresco/repo/search/adaptor/lucene/QueryConstants.java index ba5faf3e5d..f53d080fb9 100644 --- a/src/main/java/org/alfresco/repo/search/adaptor/lucene/QueryConstants.java +++ b/src/main/java/org/alfresco/repo/search/adaptor/lucene/QueryConstants.java @@ -181,6 +181,18 @@ public interface QueryConstants public static final String FIELD_SOLR_NOLOCALE_TOKENISED_SUFFIX = ".__"; + public static final String FIELD_SOLR_UNIT_OF_TIME_SECOND_SUFFIX = "_unit_of_time_second"; + + public static final String FIELD_SOLR_UNIT_OF_TIME_MINUTE_SUFFIX = "_unit_of_time_minute"; + + public static final String FIELD_SOLR_UNIT_OF_TIME_HOUR_SUFFIX = "_unit_of_time_hour"; + + public static final String FIELD_SOLR_UNIT_OF_TIME_DAY_SUFFIX = "_unit_of_time_day"; + + public static final String FIELD_SOLR_UNIT_OF_TIME_MONTH_SUFFIX = "_unit_of_time_month"; + + public static final String FIELD_SOLR_UNIT_OF_TIME_YEAR_SUFFIX = "_unit_of_time_year"; + public static final String FIELD_SITE = "SITE"; public static final String FIELD_GEO = "GEO"; diff --git a/src/main/java/org/alfresco/repo/search/impl/QueryParserUtils.java b/src/main/java/org/alfresco/repo/search/impl/QueryParserUtils.java index df50966487..e36fb76b94 100644 --- a/src/main/java/org/alfresco/repo/search/impl/QueryParserUtils.java +++ b/src/main/java/org/alfresco/repo/search/impl/QueryParserUtils.java @@ -384,6 +384,36 @@ public class QueryParserUtils implements QueryConstants propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0, field.length() - FIELD_SOLR_NOLOCALE_TOKENISED_SUFFIX.length()); ending = FIELD_SOLR_NOLOCALE_TOKENISED_SUFFIX; } + else if (field.endsWith(FIELD_SOLR_UNIT_OF_TIME_SECOND_SUFFIX)) + { + propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0, field.length() - FIELD_SOLR_UNIT_OF_TIME_SECOND_SUFFIX.length()); + ending = FIELD_SOLR_UNIT_OF_TIME_SECOND_SUFFIX; + } + else if (field.endsWith(FIELD_SOLR_UNIT_OF_TIME_MINUTE_SUFFIX)) + { + propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0, field.length() - FIELD_SOLR_UNIT_OF_TIME_MINUTE_SUFFIX.length()); + ending = FIELD_SOLR_UNIT_OF_TIME_MINUTE_SUFFIX; + } + else if (field.endsWith(FIELD_SOLR_UNIT_OF_TIME_HOUR_SUFFIX)) + { + propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0, field.length() - FIELD_SOLR_UNIT_OF_TIME_HOUR_SUFFIX.length()); + ending = FIELD_SOLR_UNIT_OF_TIME_HOUR_SUFFIX; + } + else if (field.endsWith(FIELD_SOLR_UNIT_OF_TIME_DAY_SUFFIX)) + { + propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0, field.length() - FIELD_SOLR_UNIT_OF_TIME_DAY_SUFFIX.length()); + ending = FIELD_SOLR_UNIT_OF_TIME_DAY_SUFFIX; + } + else if (field.endsWith(FIELD_SOLR_UNIT_OF_TIME_MONTH_SUFFIX)) + { + propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0, field.length() - FIELD_SOLR_UNIT_OF_TIME_MONTH_SUFFIX.length()); + ending = FIELD_SOLR_UNIT_OF_TIME_MONTH_SUFFIX; + } + else if (field.endsWith(FIELD_SOLR_UNIT_OF_TIME_YEAR_SUFFIX)) + { + propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0, field.length() - FIELD_SOLR_UNIT_OF_TIME_YEAR_SUFFIX.length()); + ending = FIELD_SOLR_UNIT_OF_TIME_YEAR_SUFFIX; + } else { propertyFieldName = field.substring(field.startsWith("@") ? 1 : 0); diff --git a/src/main/java/org/alfresco/repo/search/impl/parsers/AlfrescoFunctionEvaluationContext.java b/src/main/java/org/alfresco/repo/search/impl/parsers/AlfrescoFunctionEvaluationContext.java index f929f81901..dee38088b8 100644 --- a/src/main/java/org/alfresco/repo/search/impl/parsers/AlfrescoFunctionEvaluationContext.java +++ b/src/main/java/org/alfresco/repo/search/impl/parsers/AlfrescoFunctionEvaluationContext.java @@ -499,6 +499,87 @@ public class AlfrescoFunctionEvaluationContext implements FunctionEvaluationCont return propertyField; } } + else if(field.endsWith(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_SECOND_SUFFIX)) + { + QName propertyField = QName.createQName(field.substring(0, field.length() - QueryConstants.FIELD_SOLR_UNIT_OF_TIME_SECOND_SUFFIX.length())); + PropertyDefinition propertyDef = dictionaryService.getProperty(propertyField); + if (!propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)) + { + throw new FTSQueryException(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_SECOND_SUFFIX+" only supported on datetime properties"); + } + else + { + return propertyField; + } + } + else if(field.endsWith(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_MINUTE_SUFFIX)) + { + QName propertyField = QName.createQName(field.substring(0, field.length() - QueryConstants.FIELD_SOLR_UNIT_OF_TIME_MINUTE_SUFFIX.length())); + PropertyDefinition propertyDef = dictionaryService.getProperty(propertyField); + if (!propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)) + { + throw new FTSQueryException(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_MINUTE_SUFFIX+" only supported on datetime properties"); + } + else + { + return propertyField; + } + } + else if(field.endsWith(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_HOUR_SUFFIX)) + { + QName propertyField = QName.createQName(field.substring(0, field.length() - QueryConstants.FIELD_SOLR_UNIT_OF_TIME_HOUR_SUFFIX.length())); + PropertyDefinition propertyDef = dictionaryService.getProperty(propertyField); + if (!propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)) + { + throw new FTSQueryException(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_HOUR_SUFFIX+" only supported on datetime properties"); + } + else + { + return propertyField; + } + } + else if(field.endsWith(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_DAY_SUFFIX)) + { + QName propertyField = QName.createQName(field.substring(0, field.length() - QueryConstants.FIELD_SOLR_UNIT_OF_TIME_DAY_SUFFIX.length())); + PropertyDefinition propertyDef = dictionaryService.getProperty(propertyField); + if (!propertyDef.getDataType().getName().equals(DataTypeDefinition.DATE) && + !propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)) + { + throw new FTSQueryException(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_DAY_SUFFIX+" only supported on date and datetime properties"); + } + else + { + return propertyField; + } + } + else if(field.endsWith(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_MONTH_SUFFIX)) + { + QName propertyField = QName.createQName(field.substring(0, field.length() - QueryConstants.FIELD_SOLR_UNIT_OF_TIME_MONTH_SUFFIX.length())); + PropertyDefinition propertyDef = dictionaryService.getProperty(propertyField); + if (!propertyDef.getDataType().getName().equals(DataTypeDefinition.DATE) && + !propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)) + { + throw new FTSQueryException(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_MONTH_SUFFIX+" only supported on date and datetime properties"); + } + else + { + return propertyField; + } + } + else if(field.endsWith(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_YEAR_SUFFIX)) + { + QName propertyField = QName.createQName(field.substring(0, field.length() - QueryConstants.FIELD_SOLR_UNIT_OF_TIME_YEAR_SUFFIX.length())); + PropertyDefinition propertyDef = dictionaryService.getProperty(propertyField); + if (!propertyDef.getDataType().getName().equals(DataTypeDefinition.DATE) && + !propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)) + { + throw new FTSQueryException(QueryConstants.FIELD_SOLR_UNIT_OF_TIME_YEAR_SUFFIX+" only supported on date and datetime properties"); + } + else + { + return propertyField; + } + } else { return qname;