22
22
import org .apache .lucene .index .LeafReaderContext ;
23
23
import org .apache .lucene .index .ReaderUtil ;
24
24
import org .elasticsearch .common .document .DocumentField ;
25
- import org .elasticsearch .common .xcontent .support .XContentMapValues ;
26
25
import org .elasticsearch .index .mapper .DocumentMapper ;
27
26
import org .elasticsearch .search .SearchHit ;
28
27
import org .elasticsearch .search .fetch .FetchSubPhase ;
29
28
import org .elasticsearch .search .internal .SearchContext ;
30
29
import org .elasticsearch .search .lookup .SourceLookup ;
31
30
32
- import java .util .Collection ;
33
- import java .util .HashMap ;
34
- import java .util .HashSet ;
35
- import java .util .List ;
36
31
import java .util .Map ;
37
- import java .util .Set ;
38
- import java .util .function .Function ;
39
32
40
33
/**
41
34
* A fetch sub-phase for high-level field retrieval. Given a list of fields, it
@@ -45,14 +38,6 @@ public final class FetchFieldsPhase implements FetchSubPhase {
45
38
46
39
@ Override
47
40
public void hitsExecute (SearchContext context , SearchHit [] hits ) {
48
- hitsExecute (context , hit -> getSourceLookup (context , hit ), hits );
49
- }
50
-
51
- // Visible for testing.
52
- @ SuppressWarnings ("unchecked" )
53
- void hitsExecute (SearchContext context ,
54
- Function <SearchHit , SourceLookup > sourceProvider ,
55
- SearchHit [] hits ) {
56
41
FetchFieldsContext fetchFieldsContext = context .fetchFieldsContext ();
57
42
if (fetchFieldsContext == null || fetchFieldsContext .fields ().isEmpty ()) {
58
43
return ;
@@ -64,52 +49,20 @@ void hitsExecute(SearchContext context,
64
49
"disabled in the mappings for index [" + context .indexShard ().shardId ().getIndexName () + "]" );
65
50
}
66
51
67
- Set <String > fields = new HashSet <>();
68
- for (String fieldPattern : context .fetchFieldsContext ().fields ()) {
69
- if (documentMapper .objectMappers ().containsKey (fieldPattern )) {
70
- continue ;
71
- }
72
- Collection <String > concreteFields = context .mapperService ().simpleMatchToFullName (fieldPattern );
73
- fields .addAll (concreteFields );
74
- }
52
+ SourceLookup sourceLookup = context .lookup ().source ();
53
+ FieldValueRetriever fieldValueRetriever = FieldValueRetriever .create (
54
+ context .mapperService (),
55
+ fetchFieldsContext .fields ());
75
56
76
57
for (SearchHit hit : hits ) {
77
- SourceLookup sourceLookup = sourceProvider .apply (hit );
78
- Map <String , Object > valuesByField = extractValues (sourceLookup , fields );
79
-
80
- for (Map .Entry <String , Object > entry : valuesByField .entrySet ()) {
81
- String field = entry .getKey ();
82
- Object value = entry .getValue ();
83
- List <Object > values = value instanceof List
84
- ? (List <Object >) value
85
- : List .of (value );
86
-
87
- DocumentField documentField = new DocumentField (field , values );
88
- hit .setDocumentField (field , documentField );
89
- }
90
- }
91
- }
92
-
93
- private SourceLookup getSourceLookup (SearchContext context , SearchHit hit ) {
94
- SourceLookup sourceLookup = context .lookup ().source ();
95
- int readerIndex = ReaderUtil .subIndex (hit .docId (), context .searcher ().getIndexReader ().leaves ());
96
- LeafReaderContext readerContext = context .searcher ().getIndexReader ().leaves ().get (readerIndex );
97
- sourceLookup .setSegmentAndDocument (readerContext , hit .docId ());
98
- return sourceLookup ;
99
- }
58
+ int readerIndex = ReaderUtil .subIndex (hit .docId (), context .searcher ().getIndexReader ().leaves ());
59
+ LeafReaderContext readerContext = context .searcher ().getIndexReader ().leaves ().get (readerIndex );
60
+ sourceLookup .setSegmentAndDocument (readerContext , hit .docId ());
100
61
101
- /**
102
- * For each of the provided paths, return its value in the source. Note that in contrast with
103
- * {@link SourceLookup#extractRawValues}, array and object values can be returned.
104
- */
105
- private Map <String , Object > extractValues (SourceLookup sourceLookup , Collection <String > paths ) {
106
- Map <String , Object > result = new HashMap <>(paths .size ());
107
- for (String path : paths ) {
108
- Object value = XContentMapValues .extractValue (path , sourceLookup );
109
- if (value != null ) {
110
- result .put (path , value );
62
+ Map <String , DocumentField > documentFields = fieldValueRetriever .retrieve (sourceLookup );
63
+ for (Map .Entry <String , DocumentField > entry : documentFields .entrySet ()) {
64
+ hit .setDocumentField (entry .getKey (), entry .getValue ());
111
65
}
112
66
}
113
- return result ;
114
67
}
115
68
}
0 commit comments