DATAES-594 Added support for index without refresh.

Original PR: #286
This commit is contained in:
murali_ch 2019-06-20 15:27:42 +05:30 committed by Peter-Josef Meisch
parent 9a6172b4fe
commit d22b12874d
3 changed files with 39 additions and 3 deletions

View File

@ -27,10 +27,13 @@ import org.springframework.data.repository.NoRepositoryBean;
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Sascha Woo
* @author Murali Chevuri
*/
@NoRepositoryBean
public interface ElasticsearchRepository<T, ID> extends ElasticsearchCrudRepository<T, ID> {
<S extends T> S indexWithoutRefresh(S entity);
<S extends T> S index(S entity);
Iterable<T> search(QueryBuilder query);

View File

@ -56,9 +56,9 @@ import org.springframework.util.Assert;
* @author Christoph Strobl
* @author Michael Wirth
* @author Sascha Woo
* @author Murali Chevuri
*/
public abstract class AbstractElasticsearchRepository<T, ID>
implements ElasticsearchRepository<T, ID> {
public abstract class AbstractElasticsearchRepository<T, ID> implements ElasticsearchRepository<T, ID> {
static final Logger LOGGER = LoggerFactory.getLogger(AbstractElasticsearchRepository.class);
protected ElasticsearchOperations elasticsearchOperations;
@ -88,7 +88,7 @@ public abstract class AbstractElasticsearchRepository<T, ID>
putMapping();
}
} catch (ElasticsearchException exception) {
LOGGER.error("failed to load elasticsearch nodes : " + exception.getDetailedMessage());
LOGGER.error("failed to load elasticsearch nodes : {}", exception.getDetailedMessage());
}
}
@ -175,6 +175,17 @@ public abstract class AbstractElasticsearchRepository<T, ID>
return save(entity);
}
/**
* This method might lead to a temporary inconsistent state until
* {@link org.springframework.data.elasticsearch.repository.ElasticsearchRepository#refresh() refresh} is called.
*/
@Override
public <S extends T> S indexWithoutRefresh(S entity) {
Assert.notNull(entity, "Cannot save 'null' entity.");
elasticsearchOperations.index(createIndexQuery(entity));
return entity;
}
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "Cannot insert 'null' as a List.");

View File

@ -60,6 +60,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Christoph Strobl
* @author Michael Wirth
* @author Peter-Josef Meisch
* @author Murali Chevuri
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/simple-repository-test.xml")
@ -569,6 +570,27 @@ public class SimpleElasticsearchRepositoryTests {
assertThat(entities.getTotalElements()).isEqualTo(1L);
}
@Test
public void shouldIndexWithoutRefreshEntity() {
// given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntity.setMessage("some message");
// when
repository.indexWithoutRefresh(sampleEntity);
// then
Page<SampleEntity> entities = repository.search(termQuery("id", documentId), PageRequest.of(0, 50));
assertThat(entities.getTotalElements()).isEqualTo(0L);
repository.refresh();
entities = repository.search(termQuery("id", documentId), PageRequest.of(0, 50));
assertThat(entities.getTotalElements()).isEqualTo(1L);
}
@Test
public void shouldSortByGivenField() {