Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Support custom index naming #27
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavrilov-Ivan committed Jul 16, 2021
1 parent fb7ca05 commit c6e4a8f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@
* @return entity class
*/
Class<?> entity();

/**
* Provides explicitly defined name of the search index.
* <p>If it's not set index name will be based on 'searchIndexNamePrefix' property and entity name.
*
* @return custom index name
*/
String indexName() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public IndexConfiguration createIndexConfiguration(String className) {
if (entityMetaClass == null) {
throw new RuntimeException("MetaClass for '" + entityJavaClass + "' not found");
}
String indexName = createIndexName(entityMetaClass);
String indexName = createIndexName(indexAnnotation, entityMetaClass);
log.debug("Index name for entity {}: {}", entityMetaClass, indexName);

IndexMappingConfiguration indexMappingConfiguration = createIndexMappingConfig(entityMetaClass, indexDefClass);
Expand All @@ -117,8 +117,14 @@ protected Class<?> resolveClass(String className) {
}
}

protected String createIndexName(MetaClass entityMetaClass) {
return searchProperties.getSearchIndexNamePrefix() + entityMetaClass.getName().toLowerCase();
protected String createIndexName(JmixEntitySearchIndex indexAnnotation, MetaClass entityMetaClass) {
String indexName;
if (StringUtils.isNotEmpty(indexAnnotation.indexName())) {
indexName = indexAnnotation.indexName().toLowerCase();
} else {
indexName = searchProperties.getSearchIndexNamePrefix() + entityMetaClass.getName();
}
return indexName.toLowerCase();
}

protected IndexMappingConfiguration createIndexMappingConfig(MetaClass entityMetaClass, Class<?> indexDefClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package index_definition;

import org.junit.jupiter.params.provider.Arguments;
import test_support.entity.TestSimpleFileRootEntity;
import test_support.entity.TestRootEntity;
import test_support.entity.TestSimpleEmbeddedRootEntity;
import test_support.entity.TestSimpleFileRootEntity;
import test_support.entity.TestSimpleRootEntity;
import test_support.index_definition.common.*;
import test_support.index_definition.embedded.TestIncludeAllEmbeddablePropertiesIndexDefinition;
Expand Down Expand Up @@ -90,6 +90,14 @@ public static Stream<Arguments> provideCommonTestCases() {
.expectedEntityClass(TestSimpleRootEntity.class)
.pathToFileWithExpectedMapping("index_definition/common/test_mapping_programmatic_with_annotations")
.build()
),
Arguments.of(AnnotatedIndexDefinitionProcessorTestCase.builder("Custom value can be used as index name")
.indexDefinitionClass(TestCustomIndexNameIndexDefinition.class)
.expectedEntityName("test_SimpleRootEntity")
.expectedIndexName("custom_search_index_test_simple_root_entity")
.expectedEntityClass(TestSimpleRootEntity.class)
.pathToFileWithExpectedMapping("index_definition/common/test_mapping_custom_index_name")
.build()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 test_support.index_definition.common;

import io.jmix.search.index.annotation.AutoMappedField;
import io.jmix.search.index.annotation.JmixEntitySearchIndex;
import test_support.entity.TestSimpleRootEntity;

@JmixEntitySearchIndex(entity = TestSimpleRootEntity.class, indexName = "custom_search_index_test_simple_root_entity")
public interface TestCustomIndexNameIndexDefinition {

@AutoMappedField(includeProperties = "name")
void mapping();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"properties": {
"name": {
"type": "text"
},
"_instance_name": {
"type": "text"
}
}
}

0 comments on commit c6e4a8f

Please sign in to comment.