diff --git a/src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java b/src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java index 83b9d3c7..a33c737e 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java @@ -35,14 +35,16 @@ import org.springframework.util.Assert; */ public class SearchHit { - private final String id; + @Nullable private final String index; + @Nullable private final String id; private final float score; private final List sortValues; private final T content; private final Map> highlightFields = new LinkedHashMap<>(); - public SearchHit(@Nullable String id, float score, @Nullable Object[] sortValues, + public SearchHit(@Nullable String index, @Nullable String id, float score, @Nullable Object[] sortValues, @Nullable Map> highlightFields, T content) { + this.index = index; this.id = id; this.score = score; this.sortValues = (sortValues != null) ? Arrays.asList(sortValues) : new ArrayList<>(); @@ -53,6 +55,15 @@ public class SearchHit { this.content = content; } + /** + * @return the index name where the hit's document was found + * @since 4.1 + */ + @Nullable + public String getIndex() { + return index; + } + @Nullable public String getId() { return id; diff --git a/src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java b/src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java index 2a9a2973..f0a65adb 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java @@ -62,12 +62,13 @@ class SearchHitMapping { Assert.notNull(searchDocument, "searchDocument is null"); Assert.notNull(content, "content is null"); + String index = searchDocument.getIndex(); String id = searchDocument.hasId() ? searchDocument.getId() : null; float score = searchDocument.getScore(); Object[] sortValues = searchDocument.getSortValues(); Map> highlightFields = getHighlightsAndRemapFieldNames(searchDocument); - return new SearchHit<>(id, score, sortValues, highlightFields, content); + return new SearchHit<>(index, id, score, sortValues, highlightFields, content); } SearchHits mapHits(SearchDocumentResponse searchDocumentResponse, List contents) { diff --git a/src/main/java/org/springframework/data/elasticsearch/core/document/Document.java b/src/main/java/org/springframework/data/elasticsearch/core/document/Document.java index 59af552d..444db1b8 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/document/Document.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/document/Document.java @@ -111,6 +111,27 @@ public interface Document extends Map { return false; } + /** + * @return the index if this document was retrieved from an index + * @since 4.1 + */ + @Nullable + default String getIndex() { + return null; + } + + /** + * Sets the index name for this document + * + * @param index index name + *

+ * The default implementation throws {@link UnsupportedOperationException}. + * @since 4.1 + */ + default void setIndex(@Nullable String index) { + throw new UnsupportedOperationException(); + } + /** * Retrieve the identifier associated with this {@link Document}. *

@@ -461,4 +482,5 @@ public interface Document extends Map { * @return a JSON representation of this document. */ String toJson(); + } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/document/DocumentAdapters.java b/src/main/java/org/springframework/data/elasticsearch/core/document/DocumentAdapters.java index 95cf06c3..34c5129a 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/document/DocumentAdapters.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/document/DocumentAdapters.java @@ -77,11 +77,12 @@ public class DocumentAdapters { } if (source.isSourceEmpty()) { - return fromDocumentFields(source, source.getId(), source.getVersion(), source.getSeqNo(), + return fromDocumentFields(source, source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()); } Document document = Document.from(source.getSourceAsMap()); + document.setIndex(source.getIndex()); document.setId(source.getId()); document.setVersion(source.getVersion()); document.setSeqNo(source.getSeqNo()); @@ -108,11 +109,12 @@ public class DocumentAdapters { } if (source.isSourceEmpty()) { - return fromDocumentFields(source, source.getId(), source.getVersion(), source.getSeqNo(), + return fromDocumentFields(source, source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()); } Document document = Document.from(source.getSource()); + document.setIndex(source.getIndex()); document.setId(source.getId()); document.setVersion(source.getVersion()); document.setSeqNo(source.getSeqNo()); @@ -157,10 +159,12 @@ public class DocumentAdapters { if (sourceRef == null || sourceRef.length() == 0) { return new SearchDocumentAdapter(source.getScore(), source.getSortValues(), source.getFields(), highlightFields, - fromDocumentFields(source, source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm())); + fromDocumentFields(source, source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), + source.getPrimaryTerm())); } Document document = Document.from(source.getSourceAsMap()); + document.setIndex(source.getIndex()); document.setId(source.getId()); if (source.getVersion() >= 0) { @@ -177,13 +181,15 @@ public class DocumentAdapters { * Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s. * * @param documentFields the {@link DocumentField}s backing the {@link Document}. + * @param index * @return the adapted {@link Document}. */ - public static Document fromDocumentFields(Iterable documentFields, String id, long version, long seqNo, - long primaryTerm) { + public static Document fromDocumentFields(Iterable documentFields, String index, String id, + long version, long seqNo, long primaryTerm) { if (documentFields instanceof Collection) { - return new DocumentFieldAdapter((Collection) documentFields, id, version, seqNo, primaryTerm); + return new DocumentFieldAdapter((Collection) documentFields, index, id, version, seqNo, + primaryTerm); } List fields = new ArrayList<>(); @@ -191,58 +197,49 @@ public class DocumentAdapters { fields.add(documentField); } - return new DocumentFieldAdapter(fields, id, version, seqNo, primaryTerm); + return new DocumentFieldAdapter(fields, index, id, version, seqNo, primaryTerm); } // TODO: Performance regarding keys/values/entry-set static class DocumentFieldAdapter implements Document { private final Collection documentFields; + private final String index; private final String id; private final long version; private final long seqNo; private final long primaryTerm; - DocumentFieldAdapter(Collection documentFields, String id, long version, long seqNo, + DocumentFieldAdapter(Collection documentFields, String index, String id, long version, long seqNo, long primaryTerm) { this.documentFields = documentFields; + this.index = index; this.id = id; this.version = version; this.seqNo = seqNo; this.primaryTerm = primaryTerm; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasId() - */ + @Override + public String getIndex() { + return index; + } + @Override public boolean hasId() { return StringUtils.hasLength(id); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getId() - */ @Override public String getId() { return this.id; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasVersion() - */ @Override public boolean hasVersion() { return this.version >= 0; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getVersion() - */ @Override public long getVersion() { @@ -253,19 +250,11 @@ public class DocumentAdapters { return this.version; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasSeqNo() - */ @Override public boolean hasSeqNo() { return true; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getSeqNo() - */ @Override public long getSeqNo() { @@ -276,19 +265,11 @@ public class DocumentAdapters { return this.seqNo; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasPrimaryTerm() - */ @Override public boolean hasPrimaryTerm() { return true; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getPrimaryTerm() - */ @Override public long getPrimaryTerm() { @@ -299,28 +280,16 @@ public class DocumentAdapters { return this.primaryTerm; } - /* - * (non-Javadoc) - * @see java.util.Map#size() - */ @Override public int size() { return documentFields.size(); } - /* - * (non-Javadoc) - * @see java.util.Map#isEmpty() - */ @Override public boolean isEmpty() { return documentFields.isEmpty(); } - /* - * (non-Javadoc) - * @see java.util.Map#containsKey(java.lang.Object) - */ @Override public boolean containsKey(Object key) { @@ -333,10 +302,6 @@ public class DocumentAdapters { return false; } - /* - * (non-Javadoc) - * @see java.util.Map#containsValue(java.lang.Object) - */ @Override public boolean containsValue(Object value) { @@ -351,10 +316,6 @@ public class DocumentAdapters { return false; } - /* - * (non-Javadoc) - * @see java.util.Map#get(java.lang.Object) - */ @Override @Nullable public Object get(Object key) { @@ -365,74 +326,42 @@ public class DocumentAdapters { } - /* - * (non-Javadoc) - * @see java.util.Map#put(java.lang.Object, java.lang.Object) - */ @Override public Object put(String key, Object value) { throw new UnsupportedOperationException(); } - /* - * (non-Javadoc) - * @see java.util.Map#remove(java.lang.Object) - */ @Override public Object remove(Object key) { throw new UnsupportedOperationException(); } - /* - * (non-Javadoc) - * @see java.util.Map#putAll(Map) - */ @Override public void putAll(Map m) { throw new UnsupportedOperationException(); } - /* - * (non-Javadoc) - * @see java.util.Map#clear() - */ @Override public void clear() { throw new UnsupportedOperationException(); } - /* - * (non-Javadoc) - * @see java.util.Map#keySet() - */ @Override public Set keySet() { return documentFields.stream().map(DocumentField::getName).collect(Collectors.toCollection(LinkedHashSet::new)); } - /* - * (non-Javadoc) - * @see java.util.Map#values() - */ @Override public Collection values() { return documentFields.stream().map(DocumentFieldAdapter::getValue).collect(Collectors.toList()); } - /* - * (non-Javadoc) - * @see java.util.Map#entrySet() - */ @Override public Set> entrySet() { return documentFields.stream().collect(Collectors.toMap(DocumentField::getName, DocumentFieldAdapter::getValue)) .entrySet(); } - /* - * (non-Javadoc) - * @see java.util.Map#forEach(java.util.function.BiConsumer) - */ @Override public void forEach(BiConsumer action) { @@ -441,10 +370,6 @@ public class DocumentAdapters { documentFields.forEach(field -> action.accept(field.getName(), getValue(field))); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#toJson() - */ @Override public String toJson() { @@ -472,10 +397,6 @@ public class DocumentAdapters { } } - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ @Override public String toString() { return getClass().getSimpleName() + '@' + this.id + '#' + this.version + ' ' + toJson(); @@ -517,10 +438,6 @@ public class DocumentAdapters { this.highlightFields.putAll(highlightFields); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#append(java.lang.String, java.lang.Object) - */ @Override public SearchDocument append(String key, Object value) { delegate.append(key, value); @@ -528,281 +445,162 @@ public class DocumentAdapters { return this; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.SearchDocument#getScore() - */ @Override public float getScore() { return score; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.SearchDocument#getFields() - */ @Override public Map> getFields() { return fields; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.SearchDocument#getSortValues() - */ @Override public Object[] getSortValues() { return sortValues; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.SearchDocument#getHighlightFields() - */ @Override public Map> getHighlightFields() { return highlightFields; } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasId() - */ + @Override + public String getIndex() { + return delegate.getIndex(); + } + @Override public boolean hasId() { return delegate.hasId(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getId() - */ @Override public String getId() { return delegate.getId(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#setId(java.lang.String) - */ @Override public void setId(String id) { delegate.setId(id); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasVersion() - */ @Override public boolean hasVersion() { return delegate.hasVersion(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getVersion() - */ @Override public long getVersion() { return delegate.getVersion(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#setVersion(long) - */ @Override public void setVersion(long version) { delegate.setVersion(version); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasSeqNo() - */ @Override public boolean hasSeqNo() { return delegate.hasSeqNo(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getSeqNo() - */ @Override public long getSeqNo() { return delegate.getSeqNo(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#setSeqNo(long) - */ @Override public void setSeqNo(long seqNo) { delegate.setSeqNo(seqNo); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#hasPrimaryTerm() - */ @Override public boolean hasPrimaryTerm() { return delegate.hasPrimaryTerm(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#getPrimaryTerm() - */ @Override public long getPrimaryTerm() { return delegate.getPrimaryTerm(); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#setPrimaryTerm(long) - */ @Override public void setPrimaryTerm(long primaryTerm) { delegate.setPrimaryTerm(primaryTerm); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#get(java.lang.Object, java.lang.Class) - */ @Override @Nullable public T get(Object key, Class type) { return delegate.get(key, type); } - /* - * (non-Javadoc) - * @see org.springframework.data.elasticsearch.core.document.Document#toJson() - */ @Override public String toJson() { return delegate.toJson(); } - /* - * (non-Javadoc) - * @see java.util.Map#size() - */ @Override public int size() { return delegate.size(); } - /* - * (non-Javadoc) - * @see java.util.Map#isEmpty() - */ @Override public boolean isEmpty() { return delegate.isEmpty(); } - /* - * (non-Javadoc) - * @see java.util.Map#containsKey(java.lang.Object) - */ @Override public boolean containsKey(Object key) { return delegate.containsKey(key); } - /* - * (non-Javadoc) - * @see java.util.Map#containsValue(java.lang.Object) - */ @Override public boolean containsValue(Object value) { return delegate.containsValue(value); } - /* - * (non-Javadoc) - * @see java.util.Map#get(java.lang.Object) - */ @Override public Object get(Object key) { return delegate.get(key); } - /* - * (non-Javadoc) - * @see java.util.Map#put(java.lang.Object, java.lang.Object) - */ @Override public Object put(String key, Object value) { return delegate.put(key, value); } - /* - * (non-Javadoc) - * @see java.util.Map#remove(java.lang.Object) - */ @Override public Object remove(Object key) { return delegate.remove(key); } - /* - * (non-Javadoc) - * @see java.util.Map#putAll(Map) - */ @Override public void putAll(Map m) { delegate.putAll(m); } - /* - * (non-Javadoc) - * @see java.util.Map#clear() - */ @Override public void clear() { delegate.clear(); } - /* - * (non-Javadoc) - * @see java.util.Map#keySet() - */ @Override public Set keySet() { return delegate.keySet(); } - /* - * (non-Javadoc) - * @see java.util.Map#values() - */ @Override public Collection values() { return delegate.values(); } - /* - * (non-Javadoc) - * @see java.util.Map#entrySet() - */ @Override public Set> entrySet() { return delegate.entrySet(); } - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public boolean equals(Object o) { if (this == o) { @@ -815,37 +613,21 @@ public class DocumentAdapters { return Float.compare(that.score, score) == 0 && delegate.equals(that.delegate); } - /* - * (non-Javadoc) - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { return delegate.hashCode(); } - /* - * (non-Javadoc) - * @see java.util.Map#forEach(java.util.function.BiConsumer) - */ @Override public void forEach(BiConsumer action) { delegate.forEach(action); } - /* - * (non-Javadoc) - * @see java.util.Map#remove(java.lang.Object, java.lang.Object) - */ @Override public boolean remove(Object key, Object value) { return delegate.remove(key, value); } - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ @Override public String toString() { diff --git a/src/main/java/org/springframework/data/elasticsearch/core/document/MapDocument.java b/src/main/java/org/springframework/data/elasticsearch/core/document/MapDocument.java index fb80c35e..693e55c1 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/document/MapDocument.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/document/MapDocument.java @@ -40,6 +40,7 @@ class MapDocument implements Document { private final LinkedHashMap documentAsMap; + private @Nullable String index; private @Nullable String id; private @Nullable Long version; private @Nullable Long seqNo; @@ -53,6 +54,17 @@ class MapDocument implements Document { this.documentAsMap = new LinkedHashMap<>(documentAsMap); } + @Override + public void setIndex(@Nullable String index) { + this.index = index; + } + + @Nullable + @Override + public String getIndex() { + return index; + } + /* * (non-Javadoc) * @see org.springframework.data.elasticsearch.core.document.Document#hasId() diff --git a/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java index 8568e6d4..37dca2fd 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java @@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch.core; import static org.assertj.core.api.Assertions.*; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; @@ -27,7 +28,9 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.text.Text; import org.elasticsearch.index.get.GetResult; +import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchShardTarget; import org.junit.jupiter.api.Test; import org.springframework.data.elasticsearch.core.document.Document; import org.springframework.data.elasticsearch.core.document.DocumentAdapters; @@ -42,7 +45,7 @@ import org.springframework.data.elasticsearch.core.document.SearchDocument; */ public class DocumentAdaptersUnitTests { - @Test // DATAES-628 + @Test // DATAES-628, DATAES-848 public void shouldAdaptGetResponse() { Map fields = Collections.singletonMap("field", @@ -53,6 +56,7 @@ public class DocumentAdaptersUnitTests { Document document = DocumentAdapters.from(response); + assertThat(document.getIndex()).isEqualTo("index"); assertThat(document.hasId()).isTrue(); assertThat(document.getId()).isEqualTo("my-id"); assertThat(document.hasVersion()).isTrue(); @@ -64,7 +68,7 @@ public class DocumentAdaptersUnitTests { assertThat(document.getPrimaryTerm()).isEqualTo(2); } - @Test // DATAES-628 + @Test // DATAES-628, DATAES-848 public void shouldAdaptGetResponseSource() { BytesArray source = new BytesArray("{\"field\":\"value\"}"); @@ -74,6 +78,7 @@ public class DocumentAdaptersUnitTests { Document document = DocumentAdapters.from(response); + assertThat(document.getIndex()).isEqualTo("index"); assertThat(document.hasId()).isTrue(); assertThat(document.getId()).isEqualTo("my-id"); assertThat(document.hasVersion()).isTrue(); @@ -85,7 +90,7 @@ public class DocumentAdaptersUnitTests { assertThat(document.getPrimaryTerm()).isEqualTo(2); } - @Test // DATAES-799 + @Test // DATAES-799, DATAES-848 public void shouldAdaptGetResult() { Map fields = Collections.singletonMap("field", @@ -95,6 +100,7 @@ public class DocumentAdaptersUnitTests { Document document = DocumentAdapters.from(getResult); + assertThat(document.getIndex()).isEqualTo("index"); assertThat(document.hasId()).isTrue(); assertThat(document.getId()).isEqualTo("my-id"); assertThat(document.hasVersion()).isTrue(); @@ -106,7 +112,7 @@ public class DocumentAdaptersUnitTests { assertThat(document.getPrimaryTerm()).isEqualTo(2); } - @Test // DATAES-799 + @Test // DATAES-799, DATAES-848 public void shouldAdaptGetResultSource() { BytesArray source = new BytesArray("{\"field\":\"value\"}"); @@ -115,6 +121,7 @@ public class DocumentAdaptersUnitTests { Document document = DocumentAdapters.from(getResult); + assertThat(document.getIndex()).isEqualTo("index"); assertThat(document.hasId()).isTrue(); assertThat(document.getId()).isEqualTo("my-id"); assertThat(document.hasVersion()).isTrue(); @@ -126,19 +133,22 @@ public class DocumentAdaptersUnitTests { assertThat(document.getPrimaryTerm()).isEqualTo(2); } - @Test // DATAES-628 - public void shouldAdaptSearchResponse() { + @Test // DATAES-628, DATAES-848 + public void shouldAdaptSearchResponse() throws IOException { Map fields = Collections.singletonMap("field", new DocumentField("field", Collections.singletonList("value"))); + SearchShardTarget shard = new SearchShardTarget("node", new ShardId("index", "uuid", 42), null, null); SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), fields); + searchHit.shard(shard); searchHit.setSeqNo(1); searchHit.setPrimaryTerm(2); searchHit.score(42); SearchDocument document = DocumentAdapters.from(searchHit); + assertThat(document.getIndex()).isEqualTo("index"); assertThat(document.hasId()).isTrue(); assertThat(document.getId()).isEqualTo("my-id"); assertThat(document.hasVersion()).isFalse(); @@ -199,12 +209,14 @@ public class DocumentAdaptersUnitTests { assertThat(document.toJson()).isEqualTo("{\"string\":\"value\",\"bool\":[true,true,false]}"); } - @Test // DATAES-628 + @Test // DATAES-628, DATAES-848 public void shouldAdaptSearchResponseSource() { BytesArray source = new BytesArray("{\"field\":\"value\"}"); + SearchShardTarget shard = new SearchShardTarget("node", new ShardId("index", "uuid", 42), null, null); SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), Collections.emptyMap()); + searchHit.shard(shard); searchHit.sourceRef(source).score(42); searchHit.version(22); searchHit.setSeqNo(1); @@ -212,6 +224,7 @@ public class DocumentAdaptersUnitTests { SearchDocument document = DocumentAdapters.from(searchHit); + assertThat(document.getIndex()).isEqualTo("index"); assertThat(document.hasId()).isTrue(); assertThat(document.getId()).isEqualTo("my-id"); assertThat(document.hasVersion()).isTrue(); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java index f538b29b..a21057f8 100755 --- a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java @@ -1728,6 +1728,23 @@ public abstract class ElasticsearchTemplateTests { assertThat(ids).hasSize(30); } + @Test // DATAES-848 + public void shouldReturnIndexName() { + // given + List entities = createSampleEntitiesWithMessage("Test message", 3); + // when + operations.bulkIndex(entities, index); + indexOperations.refresh(); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "message")) + .withPageable(PageRequest.of(0, 100)).build(); + // then + SearchHits searchHits = operations.search(searchQuery, SampleEntity.class); + + searchHits.forEach(searchHit -> { + assertThat(searchHit.getIndex()).isEqualTo(INDEX_NAME_SAMPLE_ENTITY); + }); + } + @Test public void shouldReturnDocumentAboveMinimalScoreGivenQuery() { // given diff --git a/src/test/java/org/springframework/data/elasticsearch/core/SearchHitSupportTest.java b/src/test/java/org/springframework/data/elasticsearch/core/SearchHitSupportTest.java index 6ef6f8cc..9bd5bc4a 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/SearchHitSupportTest.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/SearchHitSupportTest.java @@ -84,7 +84,7 @@ class SearchHitSupportTest { @Override public SearchHit next() { String nextString = iterator.next(); - return new SearchHit<>("id", 1.0f, new Object[0], emptyMap(), nextString); + return new SearchHit<>("index", "id", 1.0f, new Object[0], emptyMap(), nextString); } } } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/StreamQueriesTest.java b/src/test/java/org/springframework/data/elasticsearch/core/StreamQueriesTest.java index 51cc2589..0d53e30a 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/StreamQueriesTest.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/StreamQueriesTest.java @@ -38,7 +38,7 @@ public class StreamQueriesTest { // given List> hits = new ArrayList<>(); - hits.add(new SearchHit(null, 0, null, null, "one")); + hits.add(new SearchHit(null, null, 0, null, null, "one")); SearchScrollHits searchHits = newSearchScrollHits(hits, "1234"); @@ -66,7 +66,7 @@ public class StreamQueriesTest { // given List> hits = new ArrayList<>(); - hits.add(new SearchHit(null, 0, null, null, "one")); + hits.add(new SearchHit(null, null, 0, null, null, "one")); SearchScrollHits searchHits = newSearchScrollHits(hits, "1234"); @@ -86,11 +86,11 @@ public class StreamQueriesTest { void shouldClearAllScrollIds() { SearchScrollHits searchHits1 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-1"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-1"); SearchScrollHits searchHits2 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-2"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-2"); SearchScrollHits searchHits3 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-2"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-2"); SearchScrollHits searchHits4 = newSearchScrollHits(Collections.emptyList(), "s-3"); Iterator> searchScrollHitsIterator = Arrays @@ -115,11 +115,11 @@ public class StreamQueriesTest { void shouldReturnAllForRequestedSizeOf0() { SearchScrollHits searchHits1 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-1"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-1"); SearchScrollHits searchHits2 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-2"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-2"); SearchScrollHits searchHits3 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-2"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-2"); SearchScrollHits searchHits4 = newSearchScrollHits(Collections.emptyList(), "s-3"); Iterator> searchScrollHitsIterator = Arrays @@ -140,11 +140,11 @@ public class StreamQueriesTest { void shouldOnlyReturnRequestedCount() { SearchScrollHits searchHits1 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-1"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-1"); SearchScrollHits searchHits2 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-2"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-2"); SearchScrollHits searchHits3 = newSearchScrollHits( - Collections.singletonList(new SearchHit(null, 0, null, null, "one")), "s-2"); + Collections.singletonList(new SearchHit(null, null, 0, null, null, "one")), "s-2"); SearchScrollHits searchHits4 = newSearchScrollHits(Collections.emptyList(), "s-3"); Iterator> searchScrollHitsIterator = Arrays