Skip to content

Commit 7cf90ae

Browse files
committed
Add matched_queries field to SearchHit.
Closes spring-projects#1514
1 parent e4c7b96 commit 7cf90ae

File tree

5 files changed

+67
-10
lines changed

5 files changed

+67
-10
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*
3434
* @param <T> the result data class.
3535
* @author Peter-Josef Meisch
36+
* @author Matt Gilene
3637
* @since 4.0
3738
*/
3839
public class SearchHit<T> {
@@ -47,16 +48,18 @@ public class SearchHit<T> {
4748
@Nullable private final NestedMetaData nestedMetaData;
4849
@Nullable private final String routing;
4950
@Nullable private final Explanation explanation;
51+
@Nullable private final List<String> matchedQueries;
5052

5153
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
5254
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields, T content) {
53-
this(index, id, routing, score, sortValues, highlightFields, null, null, null, content);
55+
this(index, id, routing, score, sortValues, highlightFields, null, null, null, null, content);
5456
}
5557

5658
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
5759
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields,
5860
@Nullable Map<String, SearchHits<?>> innerHits, @Nullable NestedMetaData nestedMetaData,
59-
@Nullable Explanation explanation, T content) {
61+
@Nullable Explanation explanation,
62+
@Nullable List<String> matchedQueries, T content) {
6063
this.index = index;
6164
this.id = id;
6265
this.routing = routing;
@@ -74,6 +77,7 @@ public SearchHit(@Nullable String index, @Nullable String id, @Nullable String r
7477
this.nestedMetaData = nestedMetaData;
7578
this.explanation = explanation;
7679
this.content = content;
80+
this.matchedQueries = matchedQueries;
7781
}
7882

7983
/**
@@ -188,4 +192,12 @@ public String getRouting() {
188192
public Explanation getExplanation() {
189193
return explanation;
190194
}
195+
196+
/**
197+
* @return the matched queries for this SearchHit.
198+
*/
199+
@Nullable
200+
public List<String> getMatchedQueries() {
201+
return matchedQueries;
202+
}
191203
}

Diff for: src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* @author Peter-Josef Meisch
4444
* @author Mark Paluch
4545
* @author Roman Puchkovskiy
46+
* @author Matt Gilene
4647
* @since 4.0
4748
*/
4849
class SearchHitMapping<T> {
@@ -114,6 +115,7 @@ SearchHit<T> mapHit(SearchDocument searchDocument, T content) {
114115
mapInnerHits(searchDocument), //
115116
searchDocument.getNestedMetaData(), //
116117
searchDocument.getExplanation(), //
118+
searchDocument.getMatchedQueries(), //
117119
content); //
118120
}
119121

@@ -198,6 +200,7 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
198200
searchHit.getInnerHits(), //
199201
persistentEntityWithNestedMetaData.nestedMetaData, //
200202
searchHit.getExplanation(), //
203+
searchHit.getMatchedQueries(), //
201204
targetObject));
202205
});
203206

Diff for: src/main/java/org/springframework/data/elasticsearch/core/document/DocumentAdapters.java

+26-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
import java.util.function.BiConsumer;
3232
import java.util.stream.Collectors;
3333

34+
import com.fasterxml.jackson.core.JsonEncoding;
35+
import com.fasterxml.jackson.core.JsonFactory;
36+
import com.fasterxml.jackson.core.JsonGenerator;
37+
3438
import org.elasticsearch.action.get.GetResponse;
3539
import org.elasticsearch.action.get.MultiGetItemResponse;
3640
import org.elasticsearch.action.get.MultiGetResponse;
@@ -47,10 +51,6 @@
4751
import org.springframework.util.Assert;
4852
import org.springframework.util.StringUtils;
4953

50-
import com.fasterxml.jackson.core.JsonEncoding;
51-
import com.fasterxml.jackson.core.JsonFactory;
52-
import com.fasterxml.jackson.core.JsonGenerator;
53-
5454
/**
5555
* Utility class to adapt {@link org.elasticsearch.action.get.GetResponse},
5656
* {@link org.elasticsearch.index.get.GetResult}, {@link org.elasticsearch.action.get.MultiGetResponse}
@@ -60,6 +60,7 @@
6060
* @author Mark Paluch
6161
* @author Peter-Josef Meisch
6262
* @author Roman Puchkovskiy
63+
* @author Matt Gilene
6364
* @since 4.0
6465
*/
6566
public class DocumentAdapters {
@@ -181,14 +182,15 @@ public static SearchDocument from(SearchHit source) {
181182

182183
NestedMetaData nestedMetaData = from(source.getNestedIdentity());
183184
Explanation explanation = from(source.getExplanation());
185+
List<String> matchedQueries = from(source.getMatchedQueries());
184186

185187
BytesReference sourceRef = source.getSourceRef();
186188

187189
if (sourceRef == null || sourceRef.length() == 0) {
188190
return new SearchDocumentAdapter(
189191
source.getScore(), source.getSortValues(), source.getFields(), highlightFields, fromDocumentFields(source,
190192
source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()),
191-
innerHits, nestedMetaData, explanation);
193+
innerHits, nestedMetaData, explanation, matchedQueries);
192194
}
193195

194196
Document document = Document.from(source.getSourceAsMap());
@@ -202,7 +204,7 @@ public static SearchDocument from(SearchHit source) {
202204
document.setPrimaryTerm(source.getPrimaryTerm());
203205

204206
return new SearchDocumentAdapter(source.getScore(), source.getSortValues(), source.getFields(), highlightFields,
205-
document, innerHits, nestedMetaData, explanation);
207+
document, innerHits, nestedMetaData, explanation, matchedQueries);
206208
}
207209

208210
@Nullable
@@ -231,6 +233,15 @@ private static NestedMetaData from(@Nullable SearchHit.NestedIdentity nestedIden
231233
return NestedMetaData.of(nestedIdentity.getField().string(), nestedIdentity.getOffset(), child);
232234
}
233235

236+
@Nullable
237+
private static List<String> from(@Nullable String[] matchedQueries) {
238+
if (matchedQueries == null) {
239+
return null;
240+
}
241+
242+
return List.of(matchedQueries);
243+
}
244+
234245
/**
235246
* Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s.
236247
*
@@ -484,10 +495,11 @@ static class SearchDocumentAdapter implements SearchDocument {
484495
private final Map<String, SearchDocumentResponse> innerHits = new HashMap<>();
485496
@Nullable private final NestedMetaData nestedMetaData;
486497
@Nullable private final Explanation explanation;
498+
@Nullable private final List<String> matchedQueries;
487499

488500
SearchDocumentAdapter(float score, Object[] sortValues, Map<String, DocumentField> fields,
489501
Map<String, List<String>> highlightFields, Document delegate, Map<String, SearchDocumentResponse> innerHits,
490-
@Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation) {
502+
@Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation, @Nullable List<String> matchedQueries) {
491503

492504
this.score = score;
493505
this.sortValues = sortValues;
@@ -497,6 +509,7 @@ static class SearchDocumentAdapter implements SearchDocument {
497509
this.innerHits.putAll(innerHits);
498510
this.nestedMetaData = nestedMetaData;
499511
this.explanation = explanation;
512+
this.matchedQueries = matchedQueries;
500513
}
501514

502515
@Override
@@ -679,6 +692,12 @@ public Explanation getExplanation() {
679692
return explanation;
680693
}
681694

695+
@Override
696+
@Nullable
697+
public List<String> getMatchedQueries() {
698+
return matchedQueries;
699+
}
700+
682701
@Override
683702
public boolean equals(Object o) {
684703
if (this == o) {

Diff for: src/main/java/org/springframework/data/elasticsearch/core/document/SearchDocument.java

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*
2626
* @author Mark Paluch
2727
* @author Peter-Josef Meisch
28+
* @author Matt Gilene
2829
* @since 4.0
2930
* @see Document
3031
*/
@@ -105,4 +106,10 @@ default String getRouting() {
105106
*/
106107
@Nullable
107108
Explanation getExplanation();
109+
110+
/**
111+
* @return the matched queries for the SearchHit.
112+
*/
113+
@Nullable
114+
List<String> getMatchedQueries();
108115
}

Diff for: src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.junit.Assert.assertThat;
1920

2021
import java.io.IOException;
2122
import java.util.Arrays;
@@ -45,6 +46,7 @@
4546
* @author Mark Paluch
4647
* @author Peter-Josef Meisch
4748
* @author Roman Puchkovskiy
49+
* @author Matt Gilene
4850
*/
4951
public class DocumentAdaptersUnitTests {
5052

@@ -262,4 +264,18 @@ void shouldAdaptReturnedExplanations() {
262264
List<Explanation> details = explanation.getDetails();
263265
assertThat(details).containsExactly(new Explanation(false, 0.0, "explanation noMatch", Collections.emptyList()));
264266
}
267+
268+
@Test // DATAES-979
269+
@DisplayName("should adapt returned matched queries")
270+
void shouldAdaptReturnedMatchedQueries() {
271+
SearchHit searchHit = new SearchHit(42);
272+
searchHit.matchedQueries(new String[] { "query1", "query2" });
273+
274+
SearchDocument searchDocument = DocumentAdapters.from(searchHit);
275+
276+
List<String> matchedQueries = searchDocument.getMatchedQueries();
277+
assertThat(matchedQueries).isNotNull();
278+
assertThat(matchedQueries).hasSize(2);
279+
assertThat(matchedQueries).isEqualTo(List.of("query1", "query2"));
280+
}
265281
}

0 commit comments

Comments
 (0)