32
32
33
33
package org .opensearch .action .admin .cluster .stats ;
34
34
35
+ import org .opensearch .action .admin .cluster .stats .ClusterStatsRequest .IndexMetric ;
35
36
import org .opensearch .action .admin .indices .stats .CommonStats ;
36
37
import org .opensearch .common .annotation .PublicApi ;
37
38
import org .opensearch .core .xcontent .ToXContentFragment ;
47
48
import java .util .HashMap ;
48
49
import java .util .List ;
49
50
import java .util .Map ;
51
+ import java .util .Set ;
50
52
51
53
/**
52
54
* Cluster Stats per index
@@ -68,14 +70,23 @@ public class ClusterStatsIndices implements ToXContentFragment {
68
70
private MappingStats mappings ;
69
71
70
72
public ClusterStatsIndices (List <ClusterStatsNodeResponse > nodeResponses , MappingStats mappingStats , AnalysisStats analysisStats ) {
71
- Map <String , ShardStats > countsPerIndex = new HashMap <>();
73
+ this (Set .of (IndexMetric .values ()), nodeResponses , mappingStats , analysisStats );
74
+
75
+ }
72
76
73
- this .docs = new DocsStats ();
74
- this .store = new StoreStats ();
75
- this .fieldData = new FieldDataStats ();
76
- this .queryCache = new QueryCacheStats ();
77
- this .completion = new CompletionStats ();
78
- this .segments = new SegmentsStats ();
77
+ public ClusterStatsIndices (
78
+ Set <IndexMetric > indicesMetrics ,
79
+ List <ClusterStatsNodeResponse > nodeResponses ,
80
+ MappingStats mappingStats ,
81
+ AnalysisStats analysisStats
82
+ ) {
83
+ Map <String , ShardStats > countsPerIndex = new HashMap <>();
84
+ this .docs = indicesMetrics .contains (IndexMetric .DOCS ) ? new DocsStats () : null ;
85
+ this .store = indicesMetrics .contains (IndexMetric .STORE ) ? new StoreStats () : null ;
86
+ this .fieldData = indicesMetrics .contains (IndexMetric .FIELDDATA ) ? new FieldDataStats () : null ;
87
+ this .queryCache = indicesMetrics .contains (IndexMetric .QUERY_CACHE ) ? new QueryCacheStats () : null ;
88
+ this .completion = indicesMetrics .contains (IndexMetric .COMPLETION ) ? new CompletionStats () : null ;
89
+ this .segments = indicesMetrics .contains (IndexMetric .SEGMENTS ) ? new SegmentsStats () : null ;
79
90
80
91
for (ClusterStatsNodeResponse r : nodeResponses ) {
81
92
// Aggregated response from the node
@@ -92,12 +103,24 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
92
103
}
93
104
}
94
105
95
- docs .add (r .getAggregatedNodeLevelStats ().commonStats .docs );
96
- store .add (r .getAggregatedNodeLevelStats ().commonStats .store );
97
- fieldData .add (r .getAggregatedNodeLevelStats ().commonStats .fieldData );
98
- queryCache .add (r .getAggregatedNodeLevelStats ().commonStats .queryCache );
99
- completion .add (r .getAggregatedNodeLevelStats ().commonStats .completion );
100
- segments .add (r .getAggregatedNodeLevelStats ().commonStats .segments );
106
+ if (indicesMetrics .contains (IndexMetric .DOCS )) {
107
+ docs .add (r .getAggregatedNodeLevelStats ().commonStats .docs );
108
+ }
109
+ if (indicesMetrics .contains (IndexMetric .STORE )) {
110
+ store .add (r .getAggregatedNodeLevelStats ().commonStats .store );
111
+ }
112
+ if (indicesMetrics .contains (IndexMetric .FIELDDATA )) {
113
+ fieldData .add (r .getAggregatedNodeLevelStats ().commonStats .fieldData );
114
+ }
115
+ if (indicesMetrics .contains (IndexMetric .QUERY_CACHE )) {
116
+ queryCache .add (r .getAggregatedNodeLevelStats ().commonStats .queryCache );
117
+ }
118
+ if (indicesMetrics .contains (IndexMetric .COMPLETION )) {
119
+ completion .add (r .getAggregatedNodeLevelStats ().commonStats .completion );
120
+ }
121
+ if (indicesMetrics .contains (IndexMetric .SEGMENTS )) {
122
+ segments .add (r .getAggregatedNodeLevelStats ().commonStats .segments );
123
+ }
101
124
} else {
102
125
// Default response from the node
103
126
for (org .opensearch .action .admin .indices .stats .ShardStats shardStats : r .shardsStats ()) {
@@ -113,21 +136,35 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
113
136
114
137
if (shardStats .getShardRouting ().primary ()) {
115
138
indexShardStats .primaries ++;
116
- docs .add (shardCommonStats .docs );
139
+ if (indicesMetrics .contains (IndexMetric .DOCS )) {
140
+ docs .add (shardCommonStats .docs );
141
+ }
142
+ }
143
+ if (indicesMetrics .contains (IndexMetric .STORE )) {
144
+ store .add (shardCommonStats .store );
145
+ }
146
+ if (indicesMetrics .contains (IndexMetric .FIELDDATA )) {
147
+ fieldData .add (shardCommonStats .fieldData );
148
+ }
149
+ if (indicesMetrics .contains (IndexMetric .QUERY_CACHE )) {
150
+ queryCache .add (shardCommonStats .queryCache );
151
+ }
152
+ if (indicesMetrics .contains (IndexMetric .COMPLETION )) {
153
+ completion .add (shardCommonStats .completion );
154
+ }
155
+ if (indicesMetrics .contains (IndexMetric .SEGMENTS )) {
156
+ segments .add (shardCommonStats .segments );
117
157
}
118
- store .add (shardCommonStats .store );
119
- fieldData .add (shardCommonStats .fieldData );
120
- queryCache .add (shardCommonStats .queryCache );
121
- completion .add (shardCommonStats .completion );
122
- segments .add (shardCommonStats .segments );
123
158
}
124
159
}
125
160
}
126
161
127
- shards = new ShardStats ();
128
162
indexCount = countsPerIndex .size ();
129
- for (final ShardStats indexCountsCursor : countsPerIndex .values ()) {
130
- shards .addIndexShardCount (indexCountsCursor );
163
+ if (indicesMetrics .contains (IndexMetric .SHARDS )) {
164
+ shards = new ShardStats ();
165
+ for (final ShardStats indexCountsCursor : countsPerIndex .values ()) {
166
+ shards .addIndexShardCount (indexCountsCursor );
167
+ }
131
168
}
132
169
133
170
this .mappings = mappingStats ;
@@ -186,13 +223,27 @@ static final class Fields {
186
223
@ Override
187
224
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
188
225
builder .field (Fields .COUNT , indexCount );
189
- shards .toXContent (builder , params );
190
- docs .toXContent (builder , params );
191
- store .toXContent (builder , params );
192
- fieldData .toXContent (builder , params );
193
- queryCache .toXContent (builder , params );
194
- completion .toXContent (builder , params );
195
- segments .toXContent (builder , params );
226
+ if (shards != null ) {
227
+ shards .toXContent (builder , params );
228
+ }
229
+ if (docs != null ) {
230
+ docs .toXContent (builder , params );
231
+ }
232
+ if (store != null ) {
233
+ store .toXContent (builder , params );
234
+ }
235
+ if (fieldData != null ) {
236
+ fieldData .toXContent (builder , params );
237
+ }
238
+ if (queryCache != null ) {
239
+ queryCache .toXContent (builder , params );
240
+ }
241
+ if (completion != null ) {
242
+ completion .toXContent (builder , params );
243
+ }
244
+ if (segments != null ) {
245
+ segments .toXContent (builder , params );
246
+ }
196
247
if (mappings != null ) {
197
248
mappings .toXContent (builder , params );
198
249
}
0 commit comments