23
23
import java .util .Collection ;
24
24
import java .util .HashMap ;
25
25
import java .util .LinkedHashMap ;
26
- import java .util .LinkedHashSet ;
27
26
import java .util .List ;
28
27
import java .util .Map ;
29
28
import java .util .Objects ;
63
62
* @author Matt Gilene
64
63
* @since 4.0
65
64
*/
66
- public class DocumentAdapters {
65
+ public final class DocumentAdapters {
66
+
67
+ private DocumentAdapters () {}
67
68
68
69
/**
69
70
* Create a {@link Document} from {@link GetResponse}.
70
71
* <p>
71
- * Returns a {@link Document} using the source if available.
72
+ * Returns a {@link Document} using the getResponse if available.
72
73
*
73
- * @param source the source {@link GetResponse}.
74
- * @return the adapted {@link Document}, null if source .isExists() returns false.
74
+ * @param getResponse the getResponse {@link GetResponse}.
75
+ * @return the adapted {@link Document}, null if getResponse .isExists() returns false.
75
76
*/
76
77
@ Nullable
77
- public static Document from (GetResponse source ) {
78
+ public static Document from (GetResponse getResponse ) {
78
79
79
- Assert .notNull (source , "GetResponse must not be null" );
80
+ Assert .notNull (getResponse , "GetResponse must not be null" );
80
81
81
- if (!source .isExists ()) {
82
+ if (!getResponse .isExists ()) {
82
83
return null ;
83
84
}
84
85
85
- if (source .isSourceEmpty ()) {
86
- return fromDocumentFields (source , source .getIndex (), source .getId (), source .getVersion (), source . getSeqNo (),
87
- source .getPrimaryTerm ());
86
+ if (getResponse .isSourceEmpty ()) {
87
+ return fromDocumentFields (getResponse , getResponse .getIndex (), getResponse .getId (), getResponse .getVersion (),
88
+ getResponse . getSeqNo (), getResponse .getPrimaryTerm ());
88
89
}
89
90
90
- Document document = Document .from (source .getSourceAsMap ());
91
- document .setIndex (source .getIndex ());
92
- document .setId (source .getId ());
93
- document .setVersion (source .getVersion ());
94
- document .setSeqNo (source .getSeqNo ());
95
- document .setPrimaryTerm (source .getPrimaryTerm ());
91
+ Document document = Document .from (getResponse .getSourceAsMap ());
92
+ document .setIndex (getResponse .getIndex ());
93
+ document .setId (getResponse .getId ());
94
+ document .setVersion (getResponse .getVersion ());
95
+ document .setSeqNo (getResponse .getSeqNo ());
96
+ document .setPrimaryTerm (getResponse .getPrimaryTerm ());
96
97
97
98
return document ;
98
99
}
@@ -188,9 +189,10 @@ public static SearchDocument from(SearchHit source) {
188
189
189
190
if (sourceRef == null || sourceRef .length () == 0 ) {
190
191
return new SearchDocumentAdapter (
191
- source .getScore (), source .getSortValues (), source .getFields (), highlightFields , fromDocumentFields (source ,
192
- source .getIndex (), source .getId (), source .getVersion (), source .getSeqNo (), source .getPrimaryTerm ()),
193
- innerHits , nestedMetaData , explanation , matchedQueries );
192
+ fromDocumentFields (source , source .getIndex (), source .getId (), source .getVersion (), source .getSeqNo (),
193
+ source .getPrimaryTerm ()),
194
+ source .getScore (), source .getSortValues (), source .getFields (), highlightFields , innerHits , nestedMetaData ,
195
+ explanation , matchedQueries );
194
196
}
195
197
196
198
Document document = Document .from (source .getSourceAsMap ());
@@ -203,8 +205,8 @@ public static SearchDocument from(SearchHit source) {
203
205
document .setSeqNo (source .getSeqNo ());
204
206
document .setPrimaryTerm (source .getPrimaryTerm ());
205
207
206
- return new SearchDocumentAdapter (source .getScore (), source .getSortValues (), source .getFields (), highlightFields ,
207
- document , innerHits , nestedMetaData , explanation , matchedQueries );
208
+ return new SearchDocumentAdapter (document , source .getScore (), source .getSortValues (), source .getFields (),
209
+ highlightFields , innerHits , nestedMetaData , explanation , matchedQueries );
208
210
}
209
211
210
212
@ Nullable
@@ -243,6 +245,10 @@ private static List<String> from(@Nullable String[] matchedQueries) {
243
245
*
244
246
* @param documentFields the {@link DocumentField}s backing the {@link Document}.
245
247
* @param index the index where the Document was found
248
+ * @param id the document id
249
+ * @param version the document version
250
+ * @param seqNo the seqNo if the document
251
+ * @param primaryTerm the primaryTerm of the document
246
252
* @return the adapted {@link Document}.
247
253
*/
248
254
public static Document fromDocumentFields (Iterable <DocumentField > documentFields , String index , String id ,
@@ -261,10 +267,13 @@ public static Document fromDocumentFields(Iterable<DocumentField> documentFields
261
267
return new DocumentFieldAdapter (fields , index , id , version , seqNo , primaryTerm );
262
268
}
263
269
264
- // TODO: Performance regarding keys/values/entry-set
270
+ /**
271
+ * Adapter for a collection of {@link DocumentField}s.
272
+ */
265
273
static class DocumentFieldAdapter implements Document {
266
274
267
275
private final Collection <DocumentField > documentFields ;
276
+ private final Map <String , DocumentField > documentFieldMap ;
268
277
private final String index ;
269
278
private final String id ;
270
279
private final long version ;
@@ -274,6 +283,8 @@ static class DocumentFieldAdapter implements Document {
274
283
DocumentFieldAdapter (Collection <DocumentField > documentFields , String index , String id , long version , long seqNo ,
275
284
long primaryTerm ) {
276
285
this .documentFields = documentFields ;
286
+ this .documentFieldMap = new LinkedHashMap <>(documentFields .size ());
287
+ documentFields .forEach (documentField -> documentFieldMap .put (documentField .getName (), documentField ));
277
288
this .index = index ;
278
289
this .id = id ;
279
290
this .version = version ;
@@ -353,14 +364,7 @@ public boolean isEmpty() {
353
364
354
365
@ Override
355
366
public boolean containsKey (Object key ) {
356
-
357
- for (DocumentField documentField : documentFields ) {
358
- if (documentField .getName ().equals (key )) {
359
- return true ;
360
- }
361
- }
362
-
363
- return false ;
367
+ return documentFieldMap .containsKey (key );
364
368
}
365
369
366
370
@ Override
@@ -380,11 +384,9 @@ public boolean containsValue(Object value) {
380
384
@ Override
381
385
@ Nullable
382
386
public Object get (Object key ) {
383
- return documentFields .stream () //
384
- .filter (documentField -> documentField .getName ().equals (key )) //
385
- .map (DocumentField ::getValue ).findFirst () //
386
- .orElse (null ); //
387
387
388
+ DocumentField documentField = documentFieldMap .get (key );
389
+ return documentField != null ? documentField .getValue () : null ;
388
390
}
389
391
390
392
@ Override
@@ -409,17 +411,18 @@ public void clear() {
409
411
410
412
@ Override
411
413
public Set <String > keySet () {
412
- return documentFields . stream (). map ( DocumentField :: getName ). collect ( Collectors . toCollection ( LinkedHashSet :: new ) );
414
+ return documentFieldMap . keySet ( );
413
415
}
414
416
415
417
@ Override
416
418
public Collection <Object > values () {
417
- return documentFields .stream ().map (DocumentFieldAdapter ::getValue ).collect (Collectors .toList ());
419
+ return documentFieldMap . values () .stream ().map (DocumentFieldAdapter ::getValue ).collect (Collectors .toList ());
418
420
}
419
421
420
422
@ Override
421
423
public Set <Entry <String , Object >> entrySet () {
422
- return documentFields .stream ().collect (Collectors .toMap (DocumentField ::getName , DocumentFieldAdapter ::getValue ))
424
+ return documentFieldMap .entrySet ().stream ()
425
+ .collect (Collectors .toMap (Entry ::getKey , entry -> DocumentFieldAdapter .getValue (entry .getValue ())))
423
426
.entrySet ();
424
427
}
425
428
@@ -458,7 +461,6 @@ public String toJson() {
458
461
}
459
462
}
460
463
461
-
462
464
@ Override
463
465
public String toString () {
464
466
return getClass ().getSimpleName () + '@' + this .id + '#' + this .version + ' ' + toJson ();
@@ -494,14 +496,14 @@ static class SearchDocumentAdapter implements SearchDocument {
494
496
@ Nullable private final Explanation explanation ;
495
497
@ Nullable private final List <String > matchedQueries ;
496
498
497
- SearchDocumentAdapter (float score , Object [] sortValues , Map <String , DocumentField > fields ,
498
- Map <String , List <String >> highlightFields , Document delegate , Map <String , SearchDocumentResponse > innerHits ,
499
+ SearchDocumentAdapter (Document delegate , float score , Object [] sortValues , Map <String , DocumentField > fields ,
500
+ Map <String , List <String >> highlightFields , Map <String , SearchDocumentResponse > innerHits ,
499
501
@ Nullable NestedMetaData nestedMetaData , @ Nullable Explanation explanation ,
500
502
@ Nullable List <String > matchedQueries ) {
501
503
504
+ this .delegate = delegate ;
502
505
this .score = score ;
503
506
this .sortValues = sortValues ;
504
- this .delegate = delegate ;
505
507
fields .forEach ((name , documentField ) -> this .fields .put (name , documentField .getValues ()));
506
508
this .highlightFields .putAll (highlightFields );
507
509
this .innerHits .putAll (innerHits );
@@ -646,7 +648,13 @@ public boolean containsValue(Object value) {
646
648
647
649
@ Override
648
650
public Object get (Object key ) {
649
- return delegate .get (key );
651
+
652
+ if (delegate .containsKey (key )) {
653
+ return delegate .get (key );
654
+ }
655
+
656
+ // fallback to fields
657
+ return fields .get (key );
650
658
}
651
659
652
660
@ Override
0 commit comments