Search in sources :

Example 1 with SeqNoPrimaryTerm

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

the class ReactiveElasticsearchTemplate method getIndexQuery.

private IndexQuery getIndexQuery(Object value) {
    AdaptibleEntity<?> entity = operations.forEntity(value, converter.getConversionService(), routingResolver);
    Object id = entity.getId();
    IndexQuery query = new IndexQuery();
    if (id != null) {
        query.setId(id.toString());
    }
    query.setObject(value);
    boolean usingSeqNo = false;
    if (entity.hasSeqNoPrimaryTerm()) {
        SeqNoPrimaryTerm seqNoPrimaryTerm = entity.getSeqNoPrimaryTerm();
        if (seqNoPrimaryTerm != null) {
            query.setSeqNo(seqNoPrimaryTerm.getSequenceNumber());
            query.setPrimaryTerm(seqNoPrimaryTerm.getPrimaryTerm());
            usingSeqNo = true;
        }
    }
    // seq_no and version are incompatible in the same request
    if (!usingSeqNo && entity.isVersionedEntity()) {
        Number version = entity.getVersion();
        if (version != null) {
            query.setVersion(version.longValue());
        }
    }
    query.setRouting(entity.getRouting());
    return query;
}
Also used : IndexQuery(org.springframework.data.elasticsearch.core.query.IndexQuery) SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)

Example 2 with SeqNoPrimaryTerm

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

the class ReactiveElasticsearchTemplate method updateIndexedObject.

private <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
    ElasticsearchPersistentEntity<?> persistentEntity = converter.getMappingContext().getPersistentEntity(entity.getClass());
    if (persistentEntity != null) {
        PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
        ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
        // Only deal with text because ES generated Ids are strings!
        if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
            propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
        }
        if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null && persistentEntity.hasSeqNoPrimaryTermProperty()) {
            ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(seqNoPrimaryTermProperty, new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
        }
        if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
            ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
        }
        // noinspection unchecked
        T updatedEntity = (T) propertyAccessor.getBean();
        return updatedEntity;
    } else {
        AdaptibleEntity<T> adaptibleEntity = operations.forEntity(entity, converter.getConversionService(), routingResolver);
        adaptibleEntity.populateIdIfNecessary(indexedObjectInformation.getId());
    }
    return entity;
}
Also used : SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)

Example 3 with SeqNoPrimaryTerm

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

the class MappingElasticsearchConverterUnitTests method shouldNotWriteSeqNoPrimaryTermProperty.

// DATAES-799
@Test
void shouldNotWriteSeqNoPrimaryTermProperty() {
    EntityWithSeqNoPrimaryTerm entity = new EntityWithSeqNoPrimaryTerm();
    entity.seqNoPrimaryTerm = new SeqNoPrimaryTerm(1L, 2L);
    Document document = Document.create();
    mappingElasticsearchConverter.write(entity, document);
    assertThat(document).doesNotContainKey("seqNoPrimaryTerm");
}
Also used : SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test)

Example 4 with SeqNoPrimaryTerm

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

the class AbstractElasticsearchTemplate method updateIndexedObject.

protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
    ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext().getPersistentEntity(entity.getClass());
    if (persistentEntity != null) {
        PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
        ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
        // Only deal with text because ES generated Ids are strings!
        if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
            propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
        }
        if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null && persistentEntity.hasSeqNoPrimaryTermProperty()) {
            ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(seqNoPrimaryTermProperty, new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
        }
        if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
            ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
            // noinspection ConstantConditions
            propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
        }
        // noinspection unchecked
        return (T) propertyAccessor.getBean();
    }
    return entity;
}
Also used : SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm) ElasticsearchPersistentProperty(org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)

Example 5 with SeqNoPrimaryTerm

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

the class AbstractElasticsearchTemplate method getIndexQuery.

private <T> IndexQuery getIndexQuery(T entity) {
    String id = getEntityId(entity);
    if (id != null) {
        id = elasticsearchConverter.convertId(id);
    }
    // noinspection ConstantConditions
    IndexQueryBuilder builder = // 
    new IndexQueryBuilder().withId(// 
    id).withObject(entity);
    SeqNoPrimaryTerm seqNoPrimaryTerm = getEntitySeqNoPrimaryTerm(entity);
    if (seqNoPrimaryTerm != null) {
        builder.withSeqNoPrimaryTerm(seqNoPrimaryTerm);
    } else {
        // version cannot be used together with seq_no and primary_term
        // noinspection ConstantConditions
        builder.withVersion(getEntityVersion(entity));
    }
    String routing = getEntityRouting(entity);
    if (routing != null) {
        builder.withRouting(routing);
    }
    return builder.build();
}
Also used : IndexQueryBuilder(org.springframework.data.elasticsearch.core.query.IndexQueryBuilder) SeqNoPrimaryTerm(org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)

Aggregations

SeqNoPrimaryTerm (org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm)6 Test (org.junit.jupiter.api.Test)2 ElasticsearchPersistentProperty (org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty)2 IndexQuery (org.springframework.data.elasticsearch.core.query.IndexQuery)2 DisplayName (org.junit.jupiter.api.DisplayName)1 Document (org.springframework.data.elasticsearch.core.document.Document)1 IndexQueryBuilder (org.springframework.data.elasticsearch.core.query.IndexQueryBuilder)1 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)1