This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support configuration of index settings #23
- Loading branch information
1 parent
c6e4a8f
commit 05dd841
Showing
5 changed files
with
199 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
search/src/main/java/io/jmix/search/index/IndexSettingsConfigurationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2021 Haulmont. | ||
* | ||
* 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 | ||
* | ||
* http://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 io.jmix.search.index; | ||
|
||
import org.elasticsearch.common.settings.Settings; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Allows to configure Elasticsearch index settings. | ||
* <p> | ||
* Settings can be configured for all search indexes ({@link IndexSettingsConfigurationContext#commonSettingsBuilder()}) | ||
* or for index related to specific entity ({@link IndexSettingsConfigurationContext#entitySettingsBuilder(Class)}). | ||
*/ | ||
public class IndexSettingsConfigurationContext { | ||
|
||
protected Settings.Builder commonSettingsBuilder; | ||
protected Map<Class<?>, Settings.Builder> specificSettingsBuilders; | ||
|
||
public IndexSettingsConfigurationContext() { | ||
commonSettingsBuilder = Settings.builder(); | ||
specificSettingsBuilders = new HashMap<>(); | ||
} | ||
|
||
/** | ||
* Provides builder to set settings for all search indexes. | ||
* <p> | ||
* Use builder's 'put' methods to set settings values. | ||
* | ||
* @return ES index settings builder | ||
*/ | ||
public Settings.Builder commonSettingsBuilder() { | ||
return commonSettingsBuilder; | ||
} | ||
|
||
/** | ||
* Provides builder to set settings for index related to provided entity. | ||
* Value explicitly set for specific index overrides common value. | ||
* <p> | ||
* Use builder's 'put' methods to set settings values. | ||
* | ||
* @param entityClass entity class | ||
* @return ES index settings builder | ||
*/ | ||
public Settings.Builder entitySettingsBuilder(Class<?> entityClass) { | ||
return specificSettingsBuilders.computeIfAbsent(entityClass, key -> Settings.builder()); | ||
} | ||
|
||
/** | ||
* Creates final settings for index related to provided entity as a combination of common and explicit settings. | ||
* | ||
* @param entityClass entity class | ||
* @return ES index settings | ||
*/ | ||
public Settings buildEffectiveSettingsForEntity(Class<?> entityClass) { | ||
Settings commonSettings = buildCommonSettings(); | ||
Settings entitySettings = buildSpecificSettingsForEntity(entityClass); | ||
return Settings.builder() | ||
.put(commonSettings) | ||
.put(entitySettings) | ||
.build(); | ||
} | ||
|
||
protected Settings buildCommonSettings() { | ||
return commonSettingsBuilder.build(); | ||
} | ||
|
||
protected Settings buildSpecificSettingsForEntity(Class<?> entityClass) { | ||
return Optional.ofNullable(specificSettingsBuilders.get(entityClass)).orElse(Settings.builder()).build(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
search/src/main/java/io/jmix/search/index/IndexSettingsConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 Haulmont. | ||
* | ||
* 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 | ||
* | ||
* http://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 io.jmix.search.index; | ||
|
||
/** | ||
* Base interface for configurers of Elasticsearch index settings. | ||
* <p> | ||
* Create Spring Bean that implements this interface. | ||
* Index settings can be configured inside {@link IndexSettingsConfigurer#configure(IndexSettingsConfigurationContext)}. | ||
* <p>See {@link IndexSettingsConfigurationContext}. | ||
* | ||
* <p>Example: | ||
* <p>Settings of all search indexes will have common values "index.max_result_window"=15000 and "index.mapping.total_fields.limit"=1500 | ||
* but settings of index related to entity class 'DemoEntity' will have common value "index.max_result_window"=15000 and explicit value "index.mapping.total_fields.limit"=2000. | ||
* <p>Configurer:<pre>{@code | ||
* @Component("demo_IndexSettingsConfigurer") | ||
* public class DemoIndexSettingsConfigurer implements IndexSettingsConfigurer { | ||
* | ||
* @Override | ||
* public void configure(@Nonnull IndexSettingsConfigurationContext context) { | ||
* context.commonSettingsBuilder() | ||
* .put("index.max_result_window", 15000) | ||
* .put("index.mapping.total_fields.limit", 1500); | ||
* | ||
* context.entitySettingsBuilder(DemoEntity.class) | ||
* .put("index.mapping.total_fields.limit", 2000); | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
public interface IndexSettingsConfigurer { | ||
|
||
void configure(IndexSettingsConfigurationContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters