DATAES-523 - Allow specifying version type.

Allow specifying the version type for documents.

Original pull request: #236
This commit is contained in:
Ivan Greene 2018-12-21 13:00:29 -06:00 committed by xhaggi
parent 76d9d74c45
commit 31b8963b31
8 changed files with 149 additions and 6 deletions

View File

@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch.annotations;
import java.lang.annotation.*;
import org.elasticsearch.index.VersionType;
import org.springframework.data.annotation.Persistent;
/**
@ -25,6 +26,7 @@ import org.springframework.data.annotation.Persistent;
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Mason Chan
* @author Ivan Greene
*/
@Persistent
@ -48,4 +50,6 @@ public @interface Document {
String indexStoreType() default "fs";
boolean createIndex() default true;
VersionType versionType() default VersionType.EXTERNAL;
}

View File

@ -16,7 +16,6 @@
package org.springframework.data.elasticsearch.core;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.VersionType.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
import static org.springframework.util.CollectionUtils.*;
@ -52,7 +51,6 @@ import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
@ -64,6 +62,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
@ -135,6 +134,7 @@ import org.springframework.util.StringUtils;
* @author Sascha Woo
* @author Ted Liang
* @author Jean-Baptiste Nizet
* @author Ivan Greene
*/
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
@ -1107,7 +1107,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
}
if (query.getVersion() != null) {
indexRequestBuilder.setVersion(query.getVersion());
indexRequestBuilder.setVersionType(EXTERNAL);
VersionType versionType = retrieveVersionTypeFromPersistentEntity(query.getObject().getClass());
indexRequestBuilder.setVersionType(versionType);
}
if (query.getParentId() != null) {
@ -1220,6 +1221,13 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
return null;
}
private VersionType retrieveVersionTypeFromPersistentEntity(Class clazz) {
if (clazz != null) {
return getPersistentEntityFor(clazz).getVersionType();
}
return VersionType.EXTERNAL;
}
private List<String> extractIds(SearchResponse response) {
List<String> ids = new ArrayList<>();
for (SearchHit hit : response.getHits()) {

View File

@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.core.mapping;
import org.elasticsearch.index.VersionType;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.lang.Nullable;
@ -26,6 +27,7 @@ import org.springframework.lang.Nullable;
* @author Mark Paluch
* @author Sascha Woo
* @author Oliver Gierke
* @author Ivan Greene
*/
public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, ElasticsearchPersistentProperty> {
@ -51,6 +53,8 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String settingPath();
VersionType getVersionType();
boolean isCreateIndexAndMapping();
/**

View File

@ -19,6 +19,7 @@ import static org.springframework.util.StringUtils.*;
import java.util.Locale;
import org.elasticsearch.index.VersionType;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@ -28,7 +29,6 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Parent;
import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.PersistentPropertyAccessorFactory;
import org.springframework.data.util.TypeInformation;
@ -47,6 +47,7 @@ import org.springframework.util.Assert;
* @author Mohsin Husen
* @author Mark Paluch
* @author Sascha Woo
* @author Ivan Greene
*/
public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty>
implements ElasticsearchPersistentEntity<T>, ApplicationContextAware {
@ -65,6 +66,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private ElasticsearchPersistentProperty parentIdProperty;
private ElasticsearchPersistentProperty scoreProperty;
private String settingPath;
private VersionType versionType;
private boolean createIndexAndMapping;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
@ -84,6 +86,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
this.replicas = document.replicas();
this.refreshInterval = document.refreshInterval();
this.indexStoreType = document.indexStoreType();
this.versionType = document.versionType();
this.createIndexAndMapping = document.createIndex();
}
if (clazz.isAnnotationPresent(Setting.class)) {
@ -145,6 +148,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return parentIdProperty;
}
@Override
public VersionType getVersionType() {
return versionType;
}
@Override
public String settingPath() {
return settingPath;

View File

@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.repository.support;
import org.elasticsearch.index.VersionType;
import org.springframework.data.repository.core.EntityInformation;
/**
@ -23,6 +24,7 @@ import org.springframework.data.repository.core.EntityInformation;
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Christoph Strobl
* @author Ivan Greene
*/
public interface ElasticsearchEntityInformation<T, ID> extends EntityInformation<T, ID> {
@ -34,5 +36,7 @@ public interface ElasticsearchEntityInformation<T, ID> extends EntityInformation
Long getVersion(T entity);
VersionType getVersionType();
String getParentId(T entity);
}

View File

@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.repository.support;
import org.elasticsearch.index.VersionType;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.repository.core.support.PersistentEntityInformation;
@ -32,6 +33,7 @@ import org.springframework.util.Assert;
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
* @author Ivan Greene
*/
public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEntityInformation<T, ID>
implements ElasticsearchEntityInformation<T, ID> {
@ -39,12 +41,13 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
private final ElasticsearchPersistentEntity<T> entityMetadata;
private final String indexName;
private final String type;
private final VersionType versionType;
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity) {
this(entity, entity.getIndexName(), entity.getIndexType());
this(entity, entity.getIndexName(), entity.getIndexType(), entity.getVersionType());
}
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type) {
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type, VersionType versionType) {
super(entity);
Assert.notNull(indexName, "IndexName must not be null!");
@ -53,6 +56,7 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
this.entityMetadata = entity;
this.indexName = indexName;
this.type = type;
this.versionType = versionType;
}
@Override
@ -81,6 +85,11 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
}
}
@Override
public VersionType getVersionType() {
return versionType;
}
@Override
public String getParentId(T entity) {

View File

@ -52,6 +52,8 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.elasticsearch.entities.Book;
import org.springframework.data.elasticsearch.entities.GTEVersionEntity;
import org.springframework.data.elasticsearch.entities.HetroEntity1;
import org.springframework.data.elasticsearch.entities.HetroEntity2;
import org.springframework.data.elasticsearch.entities.SampleEntity;
@ -78,6 +80,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
* @author Alen Turkovic
* @author Sascha Woo
* @author Jean-Baptiste Nizet
* @author Ivan Greene
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
@ -1767,6 +1770,60 @@ public class ElasticsearchTemplateTests {
assertThat(sampleEntities.getContent().get(1).get("userId"), is(person2.get("userId")));
}
/*
DATAES-523
*/
@Test
public void shouldIndexGteEntityWithVersionType() {
// given
String documentId = randomNumeric(5);
GTEVersionEntity entity = GTEVersionEntity.builder().id(documentId)
.name("FooBar")
.version(System.currentTimeMillis()).build();
IndexQueryBuilder indexQueryBuilder = new IndexQueryBuilder().withId(documentId)
.withIndexName(INDEX_NAME).withType(TYPE_NAME)
.withVersion(entity.getVersion())
.withObject(entity);
Exception ex = null;
try {
elasticsearchTemplate.index(indexQueryBuilder.build());
} catch (Exception e) {
ex = e;
}
assertNull(ex);
elasticsearchTemplate.refresh(INDEX_NAME);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withIndices(INDEX_NAME)
.withTypes(TYPE_NAME).withQuery(matchAllQuery()).build();
// when
Page<GTEVersionEntity> entities = elasticsearchTemplate.queryForPage(searchQuery, GTEVersionEntity.class);
// then
assertThat(entities, is(notNullValue()));
assertThat(entities.getTotalElements(), greaterThanOrEqualTo(1L));
// reindex with same version
try {
elasticsearchTemplate.index(indexQueryBuilder.build());
} catch (Exception e) {
ex = e;
}
assertNull(ex);
elasticsearchTemplate.refresh(INDEX_NAME);
// reindex with version one below
try {
elasticsearchTemplate.index(indexQueryBuilder.withVersion(entity.getVersion() - 1).build());
} catch (Exception e) {
ex = e;
}
assertNotNull(ex);
String message = ex.getMessage().toLowerCase();
assertTrue("Exception is version conflict", message.contains("version") && message.contains("conflict"));
}
@Test
public void shouldIndexSampleEntityWithIndexAndTypeAtRuntime() {
// given

View File

@ -0,0 +1,49 @@
/*
* Copyright 2018-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.entities;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.elasticsearch.index.VersionType;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* @author Ivan Greene
*/
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
@Document(indexName = "test-index-sample", type = "test-type", shards = 1, replicas = 0,
refreshInterval = "-1", versionType = VersionType.EXTERNAL_GTE)
public class GTEVersionEntity {
@Version
private Long version;
@Id
private String id;
private String name;
}