2424import org .elasticsearch .client .node .NodeClient ;
2525import org .elasticsearch .common .Strings ;
2626import org .elasticsearch .common .settings .Settings ;
27+ import org .elasticsearch .common .xcontent .XContentParser ;
2728import org .elasticsearch .rest .BaseRestHandler ;
2829import org .elasticsearch .rest .RestController ;
2930import org .elasticsearch .rest .RestRequest ;
3031import org .elasticsearch .rest .action .AcknowledgedRestListener ;
3132
3233import java .io .IOException ;
34+ import java .util .HashMap ;
3335import java .util .Map ;
3436import java .util .Set ;
3537
36- import static java .util .Collections .unmodifiableSet ;
3738import static org .elasticsearch .client .Requests .updateSettingsRequest ;
38- import static org .elasticsearch .common .util .set .Sets .newHashSet ;
3939
4040public class RestUpdateSettingsAction extends BaseRestHandler {
41- private static final Set <String > VALUES_TO_EXCLUDE = unmodifiableSet (newHashSet (
42- "error_trace" ,
43- "pretty" ,
44- "timeout" ,
45- "master_timeout" ,
46- "index" ,
47- "preserve_existing" ,
48- "expand_wildcards" ,
49- "ignore_unavailable" ,
50- "allow_no_indices" ));
5141
5242 public RestUpdateSettingsAction (Settings settings , RestController controller ) {
5343 super (settings );
@@ -63,29 +53,22 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
6353 updateSettingsRequest .masterNodeTimeout (request .paramAsTime ("master_timeout" , updateSettingsRequest .masterNodeTimeout ()));
6454 updateSettingsRequest .indicesOptions (IndicesOptions .fromRequest (request , updateSettingsRequest .indicesOptions ()));
6555
66- Settings .Builder updateSettings = Settings .builder ();
67- String bodySettingsStr = request .content ().utf8ToString ();
68- if (Strings .hasText (bodySettingsStr )) {
69- Settings buildSettings = Settings .builder ()
70- .loadFromSource (bodySettingsStr , request .getXContentType ())
71- .build ();
72- for (Map .Entry <String , String > entry : buildSettings .getAsMap ().entrySet ()) {
73- String key = entry .getKey ();
74- String value = entry .getValue ();
56+ Map <String , Object > settings = new HashMap <>();
57+ if (request .hasContent ()) {
58+ try (XContentParser parser = request .contentParser ()) {
59+ Map <String , Object > bodySettings = parser .map ();
60+ Object innerBodySettings = bodySettings .get ("settings" );
7561 // clean up in case the body is wrapped with "settings" : { ... }
76- if (key .startsWith ("settings." )) {
77- key = key .substring ("settings." .length ());
62+ if (innerBodySettings instanceof Map ) {
63+ @ SuppressWarnings ("unchecked" )
64+ Map <String , Object > innerBodySettingsMap = (Map <String , Object >) innerBodySettings ;
65+ settings .putAll (innerBodySettingsMap );
66+ } else {
67+ settings .putAll (bodySettings );
7868 }
79- updateSettings .put (key , value );
8069 }
8170 }
82- for (Map .Entry <String , String > entry : request .params ().entrySet ()) {
83- if (VALUES_TO_EXCLUDE .contains (entry .getKey ())) {
84- continue ;
85- }
86- updateSettings .put (entry .getKey (), entry .getValue ());
87- }
88- updateSettingsRequest .settings (updateSettings );
71+ updateSettingsRequest .settings (settings );
8972
9073 return channel -> client .admin ().indices ().updateSettings (updateSettingsRequest , new AcknowledgedRestListener <>(channel ));
9174 }
@@ -94,5 +77,4 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
9477 protected Set <String > responseParams () {
9578 return Settings .FORMAT_PARAMS ;
9679 }
97-
9880}
0 commit comments