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 327066b47..4eebdcd2a 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/SearchHit.java @@ -33,6 +33,7 @@ * * @param the result data class. * @author Peter-Josef Meisch + * @author Matt Gilene * @since 4.0 */ public class SearchHit { @@ -47,16 +48,18 @@ public class SearchHit { @Nullable private final NestedMetaData nestedMetaData; @Nullable private final String routing; @Nullable private final Explanation explanation; + @Nullable private final List matchedQueries; public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score, @Nullable Object[] sortValues, @Nullable Map> highlightFields, T content) { - this(index, id, routing, score, sortValues, highlightFields, null, null, null, content); + this(index, id, routing, score, sortValues, highlightFields, null, null, null, null, content); } public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score, @Nullable Object[] sortValues, @Nullable Map> highlightFields, @Nullable Map> innerHits, @Nullable NestedMetaData nestedMetaData, - @Nullable Explanation explanation, T content) { + @Nullable Explanation explanation, + @Nullable List matchedQueries, T content) { this.index = index; this.id = id; this.routing = routing; @@ -74,6 +77,7 @@ public SearchHit(@Nullable String index, @Nullable String id, @Nullable String r this.nestedMetaData = nestedMetaData; this.explanation = explanation; this.content = content; + this.matchedQueries = matchedQueries; } /** @@ -188,4 +192,12 @@ public String getRouting() { public Explanation getExplanation() { return explanation; } + + /** + * @return the matched queries for this SearchHit. + */ + @Nullable + public List getMatchedQueries() { + return matchedQueries; + } } 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 3971dcef6..b89d6ee0e 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java @@ -43,6 +43,7 @@ * @author Peter-Josef Meisch * @author Mark Paluch * @author Roman Puchkovskiy + * @author Matt Gilene * @since 4.0 */ class SearchHitMapping { @@ -114,6 +115,7 @@ SearchHit mapHit(SearchDocument searchDocument, T content) { mapInnerHits(searchDocument), // searchDocument.getNestedMetaData(), // searchDocument.getExplanation(), // + searchDocument.getMatchedQueries(), // content); // } @@ -198,6 +200,7 @@ private SearchHits mapInnerDocuments(SearchHits searchHits, C searchHit.getInnerHits(), // persistentEntityWithNestedMetaData.nestedMetaData, // searchHit.getExplanation(), // + searchHit.getMatchedQueries(), // targetObject)); }); 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 ae4dc09fa..ee8d0908d 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 @@ -31,6 +31,10 @@ import java.util.function.BiConsumer; import java.util.stream.Collectors; +import com.fasterxml.jackson.core.JsonEncoding; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; + import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetItemResponse; import org.elasticsearch.action.get.MultiGetResponse; @@ -47,10 +51,6 @@ import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import com.fasterxml.jackson.core.JsonEncoding; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; - /** * Utility class to adapt {@link org.elasticsearch.action.get.GetResponse}, * {@link org.elasticsearch.index.get.GetResult}, {@link org.elasticsearch.action.get.MultiGetResponse} @@ -60,6 +60,7 @@ * @author Mark Paluch * @author Peter-Josef Meisch * @author Roman Puchkovskiy + * @author Matt Gilene * @since 4.0 */ public class DocumentAdapters { @@ -181,6 +182,7 @@ public static SearchDocument from(SearchHit source) { NestedMetaData nestedMetaData = from(source.getNestedIdentity()); Explanation explanation = from(source.getExplanation()); + List matchedQueries = from(source.getMatchedQueries()); BytesReference sourceRef = source.getSourceRef(); @@ -188,7 +190,7 @@ public static SearchDocument from(SearchHit source) { return new SearchDocumentAdapter( source.getScore(), source.getSortValues(), source.getFields(), highlightFields, fromDocumentFields(source, source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()), - innerHits, nestedMetaData, explanation); + innerHits, nestedMetaData, explanation, matchedQueries); } Document document = Document.from(source.getSourceAsMap()); @@ -202,7 +204,7 @@ public static SearchDocument from(SearchHit source) { document.setPrimaryTerm(source.getPrimaryTerm()); return new SearchDocumentAdapter(source.getScore(), source.getSortValues(), source.getFields(), highlightFields, - document, innerHits, nestedMetaData, explanation); + document, innerHits, nestedMetaData, explanation, matchedQueries); } @Nullable @@ -231,6 +233,11 @@ private static NestedMetaData from(@Nullable SearchHit.NestedIdentity nestedIden return NestedMetaData.of(nestedIdentity.getField().string(), nestedIdentity.getOffset(), child); } + @Nullable + private static List from(@Nullable String[] matchedQueries) { + return matchedQueries == null ? null : Arrays.asList(matchedQueries); + } + /** * Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s. * @@ -484,10 +491,11 @@ static class SearchDocumentAdapter implements SearchDocument { private final Map innerHits = new HashMap<>(); @Nullable private final NestedMetaData nestedMetaData; @Nullable private final Explanation explanation; + @Nullable private final List matchedQueries; SearchDocumentAdapter(float score, Object[] sortValues, Map fields, Map> highlightFields, Document delegate, Map innerHits, - @Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation) { + @Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation, @Nullable List matchedQueries) { this.score = score; this.sortValues = sortValues; @@ -497,6 +505,7 @@ static class SearchDocumentAdapter implements SearchDocument { this.innerHits.putAll(innerHits); this.nestedMetaData = nestedMetaData; this.explanation = explanation; + this.matchedQueries = matchedQueries; } @Override @@ -679,6 +688,12 @@ public Explanation getExplanation() { return explanation; } + @Override + @Nullable + public List getMatchedQueries() { + return matchedQueries; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/org/springframework/data/elasticsearch/core/document/SearchDocument.java b/src/main/java/org/springframework/data/elasticsearch/core/document/SearchDocument.java index 17daec2df..e3f21f963 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/document/SearchDocument.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/document/SearchDocument.java @@ -25,6 +25,7 @@ * * @author Mark Paluch * @author Peter-Josef Meisch + * @author Matt Gilene * @since 4.0 * @see Document */ @@ -105,4 +106,10 @@ default String getRouting() { */ @Nullable Explanation getExplanation(); + + /** + * @return the matched queries for the SearchHit. + */ + @Nullable + List getMatchedQueries(); } 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 34c3b1246..ca9c3366c 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.elasticsearch.core; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; import java.util.Arrays; @@ -45,6 +45,7 @@ * @author Mark Paluch * @author Peter-Josef Meisch * @author Roman Puchkovskiy + * @author Matt Gilene */ public class DocumentAdaptersUnitTests { @@ -262,4 +263,18 @@ void shouldAdaptReturnedExplanations() { List details = explanation.getDetails(); assertThat(details).containsExactly(new Explanation(false, 0.0, "explanation noMatch", Collections.emptyList())); } + + @Test // DATAES-979 + @DisplayName("should adapt returned matched queries") + void shouldAdaptReturnedMatchedQueries() { + SearchHit searchHit = new SearchHit(42); + searchHit.matchedQueries(new String[] { "query1", "query2" }); + + SearchDocument searchDocument = DocumentAdapters.from(searchHit); + + List matchedQueries = searchDocument.getMatchedQueries(); + assertThat(matchedQueries).isNotNull(); + assertThat(matchedQueries).hasSize(2); + assertThat(matchedQueries).isEqualTo(Arrays.asList("query1", "query2")); + } }