@@ -161,24 +161,20 @@ public static SearchDocument from(SearchHit source) {
161
161
Map <String , SearchHits > sourceInnerHits = source .getInnerHits ();
162
162
163
163
if (sourceInnerHits != null ) {
164
- sourceInnerHits .forEach ((name , searchHits ) -> {
165
- innerHits .put (name , SearchDocumentResponse .from (searchHits , null , null ));
166
- });
164
+ sourceInnerHits
165
+ .forEach ((name , searchHits ) -> innerHits .put (name , SearchDocumentResponse .from (searchHits , null , null )));
167
166
}
168
167
169
- NestedMetaData nestedMetaData = null ;
170
-
171
- if (source .getNestedIdentity () != null ) {
172
- nestedMetaData = from (source .getNestedIdentity ());
173
- }
168
+ NestedMetaData nestedMetaData = from (source .getNestedIdentity ());
169
+ Explanation explanation = from (source .getExplanation ());
174
170
175
171
BytesReference sourceRef = source .getSourceRef ();
176
172
177
173
if (sourceRef == null || sourceRef .length () == 0 ) {
178
174
return new SearchDocumentAdapter (
179
175
source .getScore (), source .getSortValues (), source .getFields (), highlightFields , fromDocumentFields (source ,
180
176
source .getIndex (), source .getId (), source .getVersion (), source .getSeqNo (), source .getPrimaryTerm ()),
181
- innerHits , nestedMetaData );
177
+ innerHits , nestedMetaData , explanation );
182
178
}
183
179
184
180
Document document = Document .from (source .getSourceAsMap ());
@@ -192,25 +188,40 @@ public static SearchDocument from(SearchHit source) {
192
188
document .setPrimaryTerm (source .getPrimaryTerm ());
193
189
194
190
return new SearchDocumentAdapter (source .getScore (), source .getSortValues (), source .getFields (), highlightFields ,
195
- document , innerHits , nestedMetaData );
191
+ document , innerHits , nestedMetaData , explanation );
196
192
}
197
193
198
- private static NestedMetaData from (SearchHit .NestedIdentity nestedIdentity ) {
194
+ @ Nullable
195
+ private static Explanation from (@ Nullable org .apache .lucene .search .Explanation explanation ) {
199
196
200
- NestedMetaData child = null ;
197
+ if (explanation == null ) {
198
+ return null ;
199
+ }
201
200
202
- if (nestedIdentity .getChild () != null ) {
203
- child = from (nestedIdentity .getChild ());
201
+ List <Explanation > details = new ArrayList <>();
202
+ for (org .apache .lucene .search .Explanation detail : explanation .getDetails ()) {
203
+ details .add (from (detail ));
204
204
}
205
205
206
+ return new Explanation (explanation .isMatch (), explanation .getValue ().doubleValue (), explanation .getDescription (),
207
+ details );
208
+ }
209
+
210
+ @ Nullable
211
+ private static NestedMetaData from (@ Nullable SearchHit .NestedIdentity nestedIdentity ) {
212
+
213
+ if (nestedIdentity == null ) {
214
+ return null ;
215
+ }
216
+ NestedMetaData child = from (nestedIdentity .getChild ());
206
217
return NestedMetaData .of (nestedIdentity .getField ().string (), nestedIdentity .getOffset (), child );
207
218
}
208
219
209
220
/**
210
221
* Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s.
211
222
*
212
223
* @param documentFields the {@link DocumentField}s backing the {@link Document}.
213
- * @param index
224
+ * @param index the index where the Document was found
214
225
* @return the adapted {@link Document}.
215
226
*/
216
227
public static Document fromDocumentFields (Iterable <DocumentField > documentFields , String index , String id ,
@@ -458,10 +469,11 @@ static class SearchDocumentAdapter implements SearchDocument {
458
469
private final Map <String , List <String >> highlightFields = new HashMap <>();
459
470
private final Map <String , SearchDocumentResponse > innerHits = new HashMap <>();
460
471
@ Nullable private final NestedMetaData nestedMetaData ;
472
+ @ Nullable private final Explanation explanation ;
461
473
462
474
SearchDocumentAdapter (float score , Object [] sortValues , Map <String , DocumentField > fields ,
463
475
Map <String , List <String >> highlightFields , Document delegate , Map <String , SearchDocumentResponse > innerHits ,
464
- @ Nullable NestedMetaData nestedMetaData ) {
476
+ @ Nullable NestedMetaData nestedMetaData , @ Nullable Explanation explanation ) {
465
477
466
478
this .score = score ;
467
479
this .sortValues = sortValues ;
@@ -470,6 +482,7 @@ static class SearchDocumentAdapter implements SearchDocument {
470
482
this .highlightFields .putAll (highlightFields );
471
483
this .innerHits .putAll (innerHits );
472
484
this .nestedMetaData = nestedMetaData ;
485
+ this .explanation = explanation ;
473
486
}
474
487
475
488
@ Override
@@ -646,6 +659,12 @@ public Set<Entry<String, Object>> entrySet() {
646
659
return delegate .entrySet ();
647
660
}
648
661
662
+ @ Override
663
+ @ Nullable
664
+ public Explanation getExplanation () {
665
+ return explanation ;
666
+ }
667
+
649
668
@ Override
650
669
public boolean equals (Object o ) {
651
670
if (this == o ) {
0 commit comments