Skip to content

Commit

Permalink
IndexedIndexName: add tests, fix documentation
Browse files Browse the repository at this point in the history
Closes spring-projects#3007

Signed-off-by: Peter-Josef Meisch <[email protected]>
  • Loading branch information
sothawo committed Jan 12, 2025
1 parent 5f297f1 commit f7df26c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ This property will not be written to the mapping, it will not be stored in Elast
After an entity is persisted, for example with a call to `ElasticsearchOperations.save(T entity)`, the entity returned from that call will contain the name of the index that an entity was saved to in that property.
This is useful when the index name is dynamically set by a bean, or when writing to a write alias.

After a search operation, the property of the entity will contain the name of the index that the entity was retrieved from.

Putting some value into such a property does not set the index into which an entity is stored!

[[elasticsearch.mapping.meta-model.rules]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3739,6 +3739,22 @@ void shouldSetIndexedIndexNameProperty() {
assertThat(saved.getIndexedIndexName()).isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
}

@Test // #3007
@DisplayName("should set IndexedIndexName in search result")
void shouldSetIndexedIndexNameInSearchResult() {

var entity = new IndexedIndexNameEntity();
entity.setId("42");
entity.setSomeText("someText");
operations.save(entity);

var searchHits = operations.search(Query.findAll(), IndexedIndexNameEntity.class);

assertThat(searchHits.getTotalHits()).isEqualTo(1);
var foundEntity = searchHits.getSearchHit(0).getContent();
assertThat(foundEntity.indexedIndexName).isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
}

@Test // #1945
@DisplayName("should error on sort with unmapped field and default settings")
void shouldErrorOnSortWithUnmappedFieldAndDefaultSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ void shouldSetIndexedIndexNameProperty() {
assertThat(saved.getIndexedIndexName()).isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
}

@Test // #3007
@DisplayName("should set IndexedIndexName in search result")
void shouldSetIndexedIndexNameInSearchResult() {

var entity = new IndexedIndexNameEntity();
entity.setId("42");
entity.setSomeText("someText");
operations.save(entity).block();

operations.search(Query.findAll(), IndexedIndexNameEntity.class)
.as(StepVerifier::create)
.consumeNextWith(searchHit -> {
assertThat(searchHit.getContent().indexedIndexName)
.isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
})
.verifyComplete();
}

private Mono<Boolean> documentWithIdExistsInIndex(String id, String index) {
return operations.exists(id, IndexCoordinates.of(index));
}
Expand Down

0 comments on commit f7df26c

Please sign in to comment.