org.springframework.data.elasticsearch.core.index.Settings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-elasticsearch Show documentation
Show all versions of spring-data-elasticsearch Show documentation
Spring Data Implementation for Elasticsearch
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core.index;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.data.elasticsearch.support.DefaultStringObjectMap;
import org.springframework.util.Assert;
/**
* class defining the settings for an index.
*
* @author Peter-Josef Meisch
* @since 4.2
*/
public class Settings extends DefaultStringObjectMap {
public Settings() {}
public Settings(Map map) {
super(map);
}
/**
* Creates a {@link Settings} object from the given JSON String
*
* @param json must not be {@literal null}
* @return Settings object
*/
public static Settings parse(String json) {
return new Settings().fromJson(json);
}
@Override
public String toString() {
return "Settings: " + toJson();
}
@Override
public Object get(Object key) {
return containsKey(key) ? super.get(key) : path(key.toString());
}
/**
* Merges some other settings onto this one. Other has higher priority on same keys.
*
* @param other the other settings. Must not be {@literal null}
* @since 4.4
*/
public void merge(Settings other) {
Assert.notNull(other, "other must not be null");
deepMerge(this, other);
}
/*
* taken from https://stackoverflow.com/a/29698326/4393565
*/
@SuppressWarnings("unchecked")
private static Map deepMerge(Map original, Map newMap) {
for (Object key : newMap.keySet()) {
if (newMap.get(key) instanceof Map && original.get(key) instanceof Map) {
Map originalChild = (Map) original.get(key);
Map newChild = (Map) newMap.get(key);
original.put(key.toString(), deepMerge(originalChild, newChild));
} else if (newMap.get(key) instanceof List && original.get(key) instanceof List) {
List