Skip to content

Commit f7df26c

Browse files
committed
IndexedIndexName: add tests, fix documentation
Closes spring-projects#3007 Signed-off-by: Peter-Josef Meisch <[email protected]>
1 parent 5f297f1 commit f7df26c

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/main/antora/modules/ROOT/pages/elasticsearch/object-mapping.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ This property will not be written to the mapping, it will not be stored in Elast
195195
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.
196196
This is useful when the index name is dynamically set by a bean, or when writing to a write alias.
197197

198+
After a search operation, the property of the entity will contain the name of the index that the entity was retrieved from.
199+
198200
Putting some value into such a property does not set the index into which an entity is stored!
199201

200202
[[elasticsearch.mapping.meta-model.rules]]

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -3739,6 +3739,22 @@ void shouldSetIndexedIndexNameProperty() {
37393739
assertThat(saved.getIndexedIndexName()).isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
37403740
}
37413741

3742+
@Test // #3007
3743+
@DisplayName("should set IndexedIndexName in search result")
3744+
void shouldSetIndexedIndexNameInSearchResult() {
3745+
3746+
var entity = new IndexedIndexNameEntity();
3747+
entity.setId("42");
3748+
entity.setSomeText("someText");
3749+
operations.save(entity);
3750+
3751+
var searchHits = operations.search(Query.findAll(), IndexedIndexNameEntity.class);
3752+
3753+
assertThat(searchHits.getTotalHits()).isEqualTo(1);
3754+
var foundEntity = searchHits.getSearchHit(0).getContent();
3755+
assertThat(foundEntity.indexedIndexName).isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
3756+
}
3757+
37423758
@Test // #1945
37433759
@DisplayName("should error on sort with unmapped field and default settings")
37443760
void shouldErrorOnSortWithUnmappedFieldAndDefaultSettings() {

src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchIntegrationTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ void shouldSetIndexedIndexNameProperty() {
167167
assertThat(saved.getIndexedIndexName()).isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
168168
}
169169

170+
@Test // #3007
171+
@DisplayName("should set IndexedIndexName in search result")
172+
void shouldSetIndexedIndexNameInSearchResult() {
173+
174+
var entity = new IndexedIndexNameEntity();
175+
entity.setId("42");
176+
entity.setSomeText("someText");
177+
operations.save(entity).block();
178+
179+
operations.search(Query.findAll(), IndexedIndexNameEntity.class)
180+
.as(StepVerifier::create)
181+
.consumeNextWith(searchHit -> {
182+
assertThat(searchHit.getContent().indexedIndexName)
183+
.isEqualTo(indexNameProvider.indexName() + "-indexedindexname");
184+
})
185+
.verifyComplete();
186+
}
187+
170188
private Mono<Boolean> documentWithIdExistsInIndex(String id, String index) {
171189
return operations.exists(id, IndexCoordinates.of(index));
172190
}

0 commit comments

Comments
 (0)