Search in sources :

Example 1 with DateFormat

use of org.springframework.data.elasticsearch.annotations.DateFormat in project spring-data-elasticsearch by spring-projects.

the class SimpleElasticsearchPersistentProperty method getDateConverters.

private List<ElasticsearchDateConverter> getDateConverters(Field field, Class<?> actualType) {
    DateFormat[] dateFormats = field.format();
    String[] dateFormatPatterns = field.pattern();
    List<ElasticsearchDateConverter> converters = new ArrayList<>();
    if (dateFormats.length == 0 && dateFormatPatterns.length == 0) {
        LOGGER.warn(String.format("Property '%s' has @Field type '%s' but has no built-in format or custom date pattern defined. Make sure you have a converter registered for type %s.", getName(), field.type().name(), actualType.getSimpleName()));
        return converters;
    }
    // register converters for built-in formats
    for (DateFormat dateFormat : dateFormats) {
        switch(dateFormat) {
            case none:
            case custom:
                break;
            case weekyear:
            case weekyear_week:
            case weekyear_week_day:
                LOGGER.warn(String.format("No default converter available for '%s' and date format '%s'. Use a custom converter instead.", actualType.getName(), dateFormat.name()));
                break;
            default:
                converters.add(ElasticsearchDateConverter.of(dateFormat));
                break;
        }
    }
    for (String dateFormatPattern : dateFormatPatterns) {
        if (!StringUtils.hasText(dateFormatPattern)) {
            throw new MappingException(String.format("Date pattern of property '%s' must not be empty", getName()));
        }
        converters.add(ElasticsearchDateConverter.of(dateFormatPattern));
    }
    return converters;
}
Also used : DateFormat(org.springframework.data.elasticsearch.annotations.DateFormat) ElasticsearchDateConverter(org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter) ArrayList(java.util.ArrayList) MappingException(org.springframework.data.mapping.MappingException)

Example 2 with DateFormat

use of org.springframework.data.elasticsearch.annotations.DateFormat in project spring-data-elasticsearch by spring-projects.

the class MappingParameters method writeTypeAndParametersTo.

/**
 * writes the different fields to an {@link ObjectNode}.
 *
 * @param objectNode must not be {@literal null}
 */
public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
    Assert.notNull(objectNode, "objectNode must not be null");
    if (fielddata) {
        objectNode.put(FIELD_PARAM_DATA, fielddata);
    }
    if (type != FieldType.Auto) {
        objectNode.put(FIELD_PARAM_TYPE, type.getMappedName());
        if (type == FieldType.Date || type == FieldType.Date_Nanos || type == FieldType.Date_Range) {
            List<String> formats = new ArrayList<>();
            // built-in formats
            for (DateFormat dateFormat : dateFormats) {
                if (dateFormat == DateFormat.none || dateFormat == DateFormat.custom) {
                    continue;
                }
                formats.add(dateFormat.toString());
            }
            // custom date formats
            Collections.addAll(formats, dateFormatPatterns);
            if (!formats.isEmpty()) {
                objectNode.put(FIELD_PARAM_FORMAT, String.join("||", formats));
            }
        }
    }
    if (!index) {
        objectNode.put(FIELD_PARAM_INDEX, index);
    }
    if (StringUtils.hasLength(analyzer)) {
        objectNode.put(FIELD_PARAM_INDEX_ANALYZER, analyzer);
    }
    if (StringUtils.hasLength(searchAnalyzer)) {
        objectNode.put(FIELD_PARAM_SEARCH_ANALYZER, searchAnalyzer);
    }
    if (StringUtils.hasLength(normalizer)) {
        objectNode.put(FIELD_PARAM_NORMALIZER, normalizer);
    }
    if (copyTo != null && copyTo.length > 0) {
        objectNode.putArray(FIELD_PARAM_COPY_TO).addAll(Arrays.stream(copyTo).map(TextNode::valueOf).collect(Collectors.toList()));
    }
    if (ignoreAbove != null) {
        Assert.isTrue(ignoreAbove >= 0, "ignore_above must be a positive value");
        objectNode.put(FIELD_PARAM_IGNORE_ABOVE, ignoreAbove);
    }
    if (!coerce) {
        objectNode.put(FIELD_PARAM_COERCE, coerce);
    }
    if (!docValues) {
        objectNode.put(FIELD_PARAM_DOC_VALUES, docValues);
    }
    if (ignoreMalformed) {
        objectNode.put(FIELD_PARAM_IGNORE_MALFORMED, ignoreMalformed);
    }
    if (indexOptions != IndexOptions.none) {
        objectNode.put(FIELD_PARAM_INDEX_OPTIONS, indexOptions.toString());
    }
    if (indexPhrases) {
        objectNode.put(FIELD_PARAM_INDEX_PHRASES, indexPhrases);
    }
    if (indexPrefixes != null) {
        ObjectNode prefixNode = objectNode.putObject(FIELD_PARAM_INDEX_PREFIXES);
        if (indexPrefixes.minChars() != IndexPrefixes.MIN_DEFAULT) {
            prefixNode.put(FIELD_PARAM_INDEX_PREFIXES_MIN_CHARS, indexPrefixes.minChars());
        }
        if (indexPrefixes.maxChars() != IndexPrefixes.MAX_DEFAULT) {
            prefixNode.put(FIELD_PARAM_INDEX_PREFIXES_MAX_CHARS, indexPrefixes.maxChars());
        }
    }
    if (!norms) {
        objectNode.put(FIELD_PARAM_NORMS, norms);
    }
    if (StringUtils.hasLength(nullValue)) {
        switch(nullValueType) {
            case Integer:
                objectNode.put(FIELD_PARAM_NULL_VALUE, Integer.valueOf(nullValue));
                break;
            case Long:
                objectNode.put(FIELD_PARAM_NULL_VALUE, Long.valueOf(nullValue));
                break;
            case Double:
                objectNode.put(FIELD_PARAM_NULL_VALUE, Double.valueOf(nullValue));
                break;
            case String:
            default:
                objectNode.put(FIELD_PARAM_NULL_VALUE, nullValue);
                break;
        }
    }
    if (positionIncrementGap != null && positionIncrementGap >= 0) {
        objectNode.put(FIELD_PARAM_POSITION_INCREMENT_GAP, positionIncrementGap);
    }
    if (similarity != Similarity.Default) {
        objectNode.put(FIELD_PARAM_SIMILARITY, similarity.toString());
    }
    if (termVector != TermVector.none) {
        objectNode.put(FIELD_PARAM_TERM_VECTOR, termVector.toString());
    }
    if (type == FieldType.Scaled_Float) {
        objectNode.put(FIELD_PARAM_SCALING_FACTOR, scalingFactor);
    }
    if (maxShingleSize != null) {
        objectNode.put(FIELD_PARAM_MAX_SHINGLE_SIZE, maxShingleSize);
    }
    if (!positiveScoreImpact) {
        objectNode.put(FIELD_PARAM_POSITIVE_SCORE_IMPACT, positiveScoreImpact);
    }
    if (type == FieldType.Dense_Vector) {
        objectNode.put(FIELD_PARAM_DIMS, dims);
    }
    if (!enabled) {
        objectNode.put(FIELD_PARAM_ENABLED, enabled);
    }
    if (eagerGlobalOrdinals) {
        objectNode.put(FIELD_PARAM_EAGER_GLOBAL_ORDINALS, eagerGlobalOrdinals);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DateFormat(org.springframework.data.elasticsearch.annotations.DateFormat) ArrayList(java.util.ArrayList) TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 3 with DateFormat

use of org.springframework.data.elasticsearch.annotations.DateFormat in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchDateConverter method forPattern.

/**
 * Creates a {@link DateFormatter} for a given pattern. The pattern can be the name of a {@link DateFormat} enum value
 * or a literal pattern.
 *
 * @param pattern the pattern to use
 * @return DateFormatter
 */
private static DateFormatter forPattern(String pattern) {
    String resolvedPattern = pattern;
    if (DateFormat.epoch_millis.getPattern().equals(pattern)) {
        return new EpochMillisDateFormatter();
    }
    if (DateFormat.epoch_second.getPattern().equals(pattern)) {
        return new EpochSecondDateFormatter();
    }
    // check the enum values
    for (DateFormat dateFormat : DateFormat.values()) {
        switch(dateFormat) {
            case weekyear:
            case weekyear_week:
            case weekyear_week_day:
            case custom:
                continue;
        }
        if (dateFormat.name().equals(pattern)) {
            resolvedPattern = dateFormat.getPattern();
            break;
        }
    }
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(resolvedPattern);
    return new PatternDateFormatter(dateTimeFormatter);
}
Also used : DateFormat(org.springframework.data.elasticsearch.annotations.DateFormat) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

DateFormat (org.springframework.data.elasticsearch.annotations.DateFormat)3 ArrayList (java.util.ArrayList)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ElasticsearchDateConverter (org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter)1 MappingException (org.springframework.data.mapping.MappingException)1