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 ae1accd13b
commit c77b543fb8
10 changed files with 161 additions and 8 deletions

View File

@@ -53,6 +53,7 @@ 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;
@@ -80,6 +81,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
* @author Jean-Baptiste Nizet
* @author Zetang Zeng
* @author Peter Nowak
* @author Ivan Greene
*/
@Ignore
@@ -1814,6 +1816,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;
}