Skip to content

Commit 79dae4e

Browse files
authored
DATAES-848 - Add the name of the index to SearchHit.
Original PR: spring-projects#471
1 parent 852273e commit 79dae4e

File tree

9 files changed

+124
-266
lines changed

9 files changed

+124
-266
lines changed

src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@
3535
*/
3636
public class SearchHit<T> {
3737

38-
private final String id;
38+
@Nullable private final String index;
39+
@Nullable private final String id;
3940
private final float score;
4041
private final List<Object> sortValues;
4142
private final T content;
4243
private final Map<String, List<String>> highlightFields = new LinkedHashMap<>();
4344

44-
public SearchHit(@Nullable String id, float score, @Nullable Object[] sortValues,
45+
public SearchHit(@Nullable String index, @Nullable String id, float score, @Nullable Object[] sortValues,
4546
@Nullable Map<String, List<String>> highlightFields, T content) {
47+
this.index = index;
4648
this.id = id;
4749
this.score = score;
4850
this.sortValues = (sortValues != null) ? Arrays.asList(sortValues) : new ArrayList<>();
@@ -53,6 +55,15 @@ public SearchHit(@Nullable String id, float score, @Nullable Object[] sortValues
5355
this.content = content;
5456
}
5557

58+
/**
59+
* @return the index name where the hit's document was found
60+
* @since 4.1
61+
*/
62+
@Nullable
63+
public String getIndex() {
64+
return index;
65+
}
66+
5667
@Nullable
5768
public String getId() {
5869
return id;

src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ SearchHit<T> mapHit(SearchDocument searchDocument, T content) {
6262
Assert.notNull(searchDocument, "searchDocument is null");
6363
Assert.notNull(content, "content is null");
6464

65+
String index = searchDocument.getIndex();
6566
String id = searchDocument.hasId() ? searchDocument.getId() : null;
6667
float score = searchDocument.getScore();
6768
Object[] sortValues = searchDocument.getSortValues();
6869
Map<String, List<String>> highlightFields = getHighlightsAndRemapFieldNames(searchDocument);
6970

70-
return new SearchHit<>(id, score, sortValues, highlightFields, content);
71+
return new SearchHit<>(index, id, score, sortValues, highlightFields, content);
7172
}
7273

7374
SearchHits<T> mapHits(SearchDocumentResponse searchDocumentResponse, List<T> contents) {

src/main/java/org/springframework/data/elasticsearch/core/document/Document.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ default boolean hasId() {
111111
return false;
112112
}
113113

114+
/**
115+
* @return the index if this document was retrieved from an index
116+
* @since 4.1
117+
*/
118+
@Nullable
119+
default String getIndex() {
120+
return null;
121+
}
122+
123+
/**
124+
* Sets the index name for this document
125+
*
126+
* @param index index name
127+
* <p>
128+
* The default implementation throws {@link UnsupportedOperationException}.
129+
* @since 4.1
130+
*/
131+
default void setIndex(@Nullable String index) {
132+
throw new UnsupportedOperationException();
133+
}
134+
114135
/**
115136
* Retrieve the identifier associated with this {@link Document}.
116137
* <p>
@@ -461,4 +482,5 @@ default <R> R transform(Function<? super Document, ? extends R> transformer) {
461482
* @return a JSON representation of this document.
462483
*/
463484
String toJson();
485+
464486
}

0 commit comments

Comments
 (0)