9
9
package org .elasticsearch .action .fieldcaps ;
10
10
11
11
import org .elasticsearch .Version ;
12
- import org .elasticsearch .action .ActionResponse ;
13
12
import org .elasticsearch .common .io .stream .StreamInput ;
14
13
import org .elasticsearch .common .io .stream .StreamOutput ;
15
14
import org .elasticsearch .common .io .stream .Writeable ;
15
+ import org .elasticsearch .core .Nullable ;
16
16
17
17
import java .io .IOException ;
18
+ import java .util .List ;
18
19
import java .util .Map ;
19
20
import java .util .Objects ;
21
+ import java .util .function .Predicate ;
22
+ import java .util .stream .Collectors ;
23
+ import java .util .stream .Stream ;
24
+
25
+ final class FieldCapabilitiesIndexResponse implements Writeable {
26
+ private static final Version MAPPING_HASH_VERSION = Version .V_8_2_0 ;
20
27
21
- public class FieldCapabilitiesIndexResponse extends ActionResponse implements Writeable {
22
28
private final String indexName ;
29
+ @ Nullable
30
+ private final String indexMappingHash ;
23
31
private final Map <String , IndexFieldCapabilities > responseMap ;
24
32
private final boolean canMatch ;
25
33
private final transient Version originVersion ;
26
34
27
- FieldCapabilitiesIndexResponse (String indexName , Map <String , IndexFieldCapabilities > responseMap , boolean canMatch ) {
35
+ FieldCapabilitiesIndexResponse (
36
+ String indexName ,
37
+ @ Nullable String indexMappingHash ,
38
+ Map <String , IndexFieldCapabilities > responseMap ,
39
+ boolean canMatch
40
+ ) {
28
41
this .indexName = indexName ;
42
+ this .indexMappingHash = indexMappingHash ;
29
43
this .responseMap = responseMap ;
30
44
this .canMatch = canMatch ;
31
45
this .originVersion = Version .CURRENT ;
32
46
}
33
47
34
48
FieldCapabilitiesIndexResponse (StreamInput in ) throws IOException {
35
- super (in );
36
49
this .indexName = in .readString ();
37
50
this .responseMap = in .readMap (StreamInput ::readString , IndexFieldCapabilities ::new );
38
51
this .canMatch = in .readBoolean ();
39
52
this .originVersion = in .getVersion ();
53
+ if (in .getVersion ().onOrAfter (MAPPING_HASH_VERSION )) {
54
+ this .indexMappingHash = in .readOptionalString ();
55
+ } else {
56
+ this .indexMappingHash = null ;
57
+ }
58
+ }
59
+
60
+ @ Override
61
+ public void writeTo (StreamOutput out ) throws IOException {
62
+ out .writeString (indexName );
63
+ out .writeMap (responseMap , StreamOutput ::writeString , (valueOut , fc ) -> fc .writeTo (valueOut ));
64
+ out .writeBoolean (canMatch );
65
+ if (out .getVersion ().onOrAfter (MAPPING_HASH_VERSION )) {
66
+ out .writeOptionalString (indexMappingHash );
67
+ }
68
+ }
69
+
70
+ private record GroupByMappingHash (List <String > indices , String indexMappingHash , Map <String , IndexFieldCapabilities > responseMap )
71
+ implements
72
+ Writeable {
73
+ GroupByMappingHash (StreamInput in ) throws IOException {
74
+ this (in .readStringList (), in .readString (), in .readMap (StreamInput ::readString , IndexFieldCapabilities ::new ));
75
+ }
76
+
77
+ @ Override
78
+ public void writeTo (StreamOutput out ) throws IOException {
79
+ out .writeStringCollection (indices );
80
+ out .writeString (indexMappingHash );
81
+ out .writeMap (responseMap , StreamOutput ::writeString , (valueOut , fc ) -> fc .writeTo (valueOut ));
82
+ }
83
+
84
+ List <FieldCapabilitiesIndexResponse > getResponses () {
85
+ return indices .stream ().map (index -> new FieldCapabilitiesIndexResponse (index , indexMappingHash , responseMap , true )).toList ();
86
+ }
87
+ }
88
+
89
+ static List <FieldCapabilitiesIndexResponse > readList (StreamInput input ) throws IOException {
90
+ if (input .getVersion ().before (MAPPING_HASH_VERSION )) {
91
+ return input .readList (FieldCapabilitiesIndexResponse ::new );
92
+ }
93
+ final List <FieldCapabilitiesIndexResponse > ungroupedList = input .readList (FieldCapabilitiesIndexResponse ::new );
94
+ final List <GroupByMappingHash > groups = input .readList (GroupByMappingHash ::new );
95
+ return Stream .concat (ungroupedList .stream (), groups .stream ().flatMap (g -> g .getResponses ().stream ())).toList ();
96
+ }
97
+
98
+ static void writeList (StreamOutput output , List <FieldCapabilitiesIndexResponse > responses ) throws IOException {
99
+ if (output .getVersion ().before (MAPPING_HASH_VERSION )) {
100
+ output .writeCollection (responses );
101
+ return ;
102
+ }
103
+ final Predicate <FieldCapabilitiesIndexResponse > canGroup = r -> r .canMatch && r .indexMappingHash != null ;
104
+ final List <FieldCapabilitiesIndexResponse > ungroupedResponses = responses .stream ().filter (r -> canGroup .test (r ) == false ).toList ();
105
+ final List <GroupByMappingHash > groupedResponses = responses .stream ()
106
+ .filter (canGroup )
107
+ .collect (Collectors .groupingBy (r -> r .indexMappingHash ))
108
+ .values ()
109
+ .stream ()
110
+ .map (rs -> {
111
+ final String indexMappingHash = rs .get (0 ).indexMappingHash ;
112
+ final Map <String , IndexFieldCapabilities > responseMap = rs .get (0 ).responseMap ;
113
+ final List <String > indices = rs .stream ().map (r -> r .indexName ).toList ();
114
+ return new GroupByMappingHash (indices , indexMappingHash , responseMap );
115
+ })
116
+ .toList ();
117
+ output .writeList (ungroupedResponses );
118
+ output .writeList (groupedResponses );
40
119
}
41
120
42
121
/**
@@ -46,6 +125,14 @@ public String getIndexName() {
46
125
return indexName ;
47
126
}
48
127
128
+ /**
129
+ * Returns the index mapping hash associated with this index if exists
130
+ */
131
+ @ Nullable
132
+ public String getIndexMappingHash () {
133
+ return indexMappingHash ;
134
+ }
135
+
49
136
public boolean canMatch () {
50
137
return canMatch ;
51
138
}
@@ -69,23 +156,19 @@ Version getOriginVersion() {
69
156
return originVersion ;
70
157
}
71
158
72
- @ Override
73
- public void writeTo (StreamOutput out ) throws IOException {
74
- out .writeString (indexName );
75
- out .writeMap (responseMap , StreamOutput ::writeString , (valueOut , fc ) -> fc .writeTo (valueOut ));
76
- out .writeBoolean (canMatch );
77
- }
78
-
79
159
@ Override
80
160
public boolean equals (Object o ) {
81
161
if (this == o ) return true ;
82
162
if (o == null || getClass () != o .getClass ()) return false ;
83
163
FieldCapabilitiesIndexResponse that = (FieldCapabilitiesIndexResponse ) o ;
84
- return canMatch == that .canMatch && Objects .equals (indexName , that .indexName ) && Objects .equals (responseMap , that .responseMap );
164
+ return canMatch == that .canMatch
165
+ && Objects .equals (indexName , that .indexName )
166
+ && Objects .equals (indexMappingHash , that .indexMappingHash )
167
+ && Objects .equals (responseMap , that .responseMap );
85
168
}
86
169
87
170
@ Override
88
171
public int hashCode () {
89
- return Objects .hash (indexName , responseMap , canMatch );
172
+ return Objects .hash (indexName , indexMappingHash , responseMap , canMatch );
90
173
}
91
174
}
0 commit comments