Search in sources :

Example 1 with Field

use of org.springframework.data.elasticsearch.core.query.Field in project spring-data-elasticsearch by spring-projects.

the class MappingElasticsearchConverter method updateCriteria.

private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?> persistentEntity) {
    Field field = criteria.getField();
    if (field == null) {
        return;
    }
    String[] fieldNames = field.getName().split("\\.");
    ElasticsearchPersistentEntity<?> currentEntity = persistentEntity;
    ElasticsearchPersistentProperty persistentProperty = null;
    int propertyCount = 0;
    boolean isNested = false;
    for (int i = 0; i < fieldNames.length; i++) {
        persistentProperty = currentEntity.getPersistentProperty(fieldNames[i]);
        if (persistentProperty != null) {
            propertyCount++;
            fieldNames[i] = persistentProperty.getFieldName();
            org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = persistentProperty.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
            if (fieldAnnotation != null && fieldAnnotation.type() == FieldType.Nested) {
                isNested = true;
            }
            try {
                currentEntity = mappingContext.getPersistentEntity(persistentProperty.getActualType());
            } catch (Exception e) {
                // using system types like UUIDs will lead to java.lang.reflect.InaccessibleObjectException in JDK 16
                // so if we cannot get an entity here, bail out.
                currentEntity = null;
            }
        }
        if (currentEntity == null) {
            break;
        }
    }
    field.setName(String.join(".", fieldNames));
    if (propertyCount > 1 && isNested) {
        List<String> propertyNames = Arrays.asList(fieldNames);
        field.setPath(String.join(".", propertyNames.subList(0, propertyCount - 1)));
    }
    if (persistentProperty != null) {
        if (persistentProperty.hasPropertyValueConverter()) {
            PropertyValueConverter propertyValueConverter = Objects.requireNonNull(persistentProperty.getPropertyValueConverter());
            criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
                if (criteriaEntry.getKey().hasValue()) {
                    Object value = criteriaEntry.getValue();
                    if (value.getClass().isArray()) {
                        Object[] objects = (Object[]) value;
                        for (int i = 0; i < objects.length; i++) {
                            objects[i] = propertyValueConverter.write(objects[i]);
                        }
                    } else {
                        criteriaEntry.setValue(propertyValueConverter.write(value));
                    }
                }
            });
        }
        org.springframework.data.elasticsearch.annotations.Field fieldAnnotation = persistentProperty.findAnnotation(org.springframework.data.elasticsearch.annotations.Field.class);
        if (fieldAnnotation != null) {
            field.setFieldType(fieldAnnotation.type());
        }
    }
}
Also used : InstanceCreatorMetadata(org.springframework.data.mapping.InstanceCreatorMetadata) MappingException(org.springframework.data.mapping.MappingException) BeansException(org.springframework.beans.BeansException) ScriptedField(org.springframework.data.elasticsearch.annotations.ScriptedField) Field(org.springframework.data.elasticsearch.core.query.Field) PropertyValueConverter(org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)

Example 2 with Field

use of org.springframework.data.elasticsearch.core.query.Field in project spring-data-elasticsearch by spring-projects.

the class CriteriaQueryProcessor method queryForEntries.

@Nullable
private QueryBuilder queryForEntries(Criteria criteria) {
    Field field = criteria.getField();
    if (field == null || criteria.getQueryCriteriaEntries().isEmpty())
        return null;
    String fieldName = field.getName();
    Assert.notNull(fieldName, "Unknown field " + fieldName);
    Iterator<Criteria.CriteriaEntry> it = criteria.getQueryCriteriaEntries().iterator();
    QueryBuilder query;
    if (criteria.getQueryCriteriaEntries().size() == 1) {
        query = queryFor(it.next(), field);
    } else {
        query = boolQuery();
        while (it.hasNext()) {
            Criteria.CriteriaEntry entry = it.next();
            ((BoolQueryBuilder) query).must(queryFor(entry, field));
        }
    }
    addBoost(query, criteria.getBoost());
    if (hasText(field.getPath())) {
        query = nestedQuery(field.getPath(), query, ScoreMode.Avg);
    }
    return query;
}
Also used : Field(org.springframework.data.elasticsearch.core.query.Field) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Nullable(org.springframework.lang.Nullable)

Aggregations

Field (org.springframework.data.elasticsearch.core.query.Field)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 BeansException (org.springframework.beans.BeansException)1 ScriptedField (org.springframework.data.elasticsearch.annotations.ScriptedField)1 ElasticsearchPersistentProperty (org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)1 PropertyValueConverter (org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter)1 Criteria (org.springframework.data.elasticsearch.core.query.Criteria)1 InstanceCreatorMetadata (org.springframework.data.mapping.InstanceCreatorMetadata)1 MappingException (org.springframework.data.mapping.MappingException)1 Nullable (org.springframework.lang.Nullable)1