3131import java .util .function .BiConsumer ;
3232import 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+
3438import org .elasticsearch .action .get .GetResponse ;
3539import org .elasticsearch .action .get .MultiGetItemResponse ;
3640import org .elasticsearch .action .get .MultiGetResponse ;
4751import org .springframework .util .Assert ;
4852import 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}
6060 * @author Mark Paluch
6161 * @author Peter-Josef Meisch
6262 * @author Roman Puchkovskiy
63+ * @author Matt Gilene
6364 * @since 4.0
6465 */
6566public 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,11 @@ 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+ return matchedQueries == null ? null : Arrays .asList (matchedQueries );
239+ }
240+
234241 /**
235242 * Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s.
236243 *
@@ -484,10 +491,11 @@ static class SearchDocumentAdapter implements SearchDocument {
484491 private final Map <String , SearchDocumentResponse > innerHits = new HashMap <>();
485492 @ Nullable private final NestedMetaData nestedMetaData ;
486493 @ Nullable private final Explanation explanation ;
494+ @ Nullable private final List <String > matchedQueries ;
487495
488496 SearchDocumentAdapter (float score , Object [] sortValues , Map <String , DocumentField > fields ,
489497 Map <String , List <String >> highlightFields , Document delegate , Map <String , SearchDocumentResponse > innerHits ,
490- @ Nullable NestedMetaData nestedMetaData , @ Nullable Explanation explanation ) {
498+ @ Nullable NestedMetaData nestedMetaData , @ Nullable Explanation explanation , @ Nullable List < String > matchedQueries ) {
491499
492500 this .score = score ;
493501 this .sortValues = sortValues ;
@@ -497,6 +505,7 @@ static class SearchDocumentAdapter implements SearchDocument {
497505 this .innerHits .putAll (innerHits );
498506 this .nestedMetaData = nestedMetaData ;
499507 this .explanation = explanation ;
508+ this .matchedQueries = matchedQueries ;
500509 }
501510
502511 @ Override
@@ -679,6 +688,12 @@ public Explanation getExplanation() {
679688 return explanation ;
680689 }
681690
691+ @ Override
692+ @ Nullable
693+ public List <String > getMatchedQueries () {
694+ return matchedQueries ;
695+ }
696+
682697 @ Override
683698 public boolean equals (Object o ) {
684699 if (this == o ) {
0 commit comments