From 7cf90ae425ccd220ded244c7401fe642fbfc72d4 Mon Sep 17 00:00:00 2001 From: Matt Gilen Date: Fri, 5 Mar 2021 14:32:57 -0500 Subject: [PATCH 1/2] Add matched_queries field to SearchHit. Closes #1514 --- .../data/elasticsearch/core/SearchHit.java | 16 +++++++-- .../elasticsearch/core/SearchHitMapping.java | 3 ++ .../core/document/DocumentAdapters.java | 33 +++++++++++++++---- .../core/document/SearchDocument.java | 7 ++++ .../core/DocumentAdaptersUnitTests.java | 18 +++++++++- 5 files changed, 67 insertions(+), 10 deletions(-) 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..36793f97d 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,15 @@ 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) { + if (matchedQueries == null) { + return null; + } + + return List.of(matchedQueries); + } + /** * Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s. * @@ -484,10 +495,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 +509,7 @@ static class SearchDocumentAdapter implements SearchDocument { this.innerHits.putAll(innerHits); this.nestedMetaData = nestedMetaData; this.explanation = explanation; + this.matchedQueries = matchedQueries; } @Override @@ -679,6 +692,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..f437b1bb6 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,8 @@ */ package org.springframework.data.elasticsearch.core; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertThat; import java.io.IOException; import java.util.Arrays; @@ -45,6 +46,7 @@ * @author Mark Paluch * @author Peter-Josef Meisch * @author Roman Puchkovskiy + * @author Matt Gilene */ public class DocumentAdaptersUnitTests { @@ -262,4 +264,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(List.of("query1", "query2")); + } } From 5cb24a2e3cb26a073a008ffebb9a22c814cfd518 Mon Sep 17 00:00:00 2001 From: Matt Gilen Date: Wed, 10 Mar 2021 18:02:34 -0500 Subject: [PATCH 2/2] Remove Java 9 List creators --- .../data/elasticsearch/core/document/DocumentAdapters.java | 6 +----- .../data/elasticsearch/core/DocumentAdaptersUnitTests.java | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) 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 36793f97d..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 @@ -235,11 +235,7 @@ private static NestedMetaData from(@Nullable SearchHit.NestedIdentity nestedIden @Nullable private static List from(@Nullable String[] matchedQueries) { - if (matchedQueries == null) { - return null; - } - - return List.of(matchedQueries); + return matchedQueries == null ? null : Arrays.asList(matchedQueries); } /** 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 f437b1bb6..ca9c3366c 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/DocumentAdaptersUnitTests.java @@ -16,7 +16,6 @@ package org.springframework.data.elasticsearch.core; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertThat; import java.io.IOException; import java.util.Arrays; @@ -276,6 +275,6 @@ void shouldAdaptReturnedMatchedQueries() { List matchedQueries = searchDocument.getMatchedQueries(); assertThat(matchedQueries).isNotNull(); assertThat(matchedQueries).hasSize(2); - assertThat(matchedQueries).isEqualTo(List.of("query1", "query2")); + assertThat(matchedQueries).isEqualTo(Arrays.asList("query1", "query2")); } }