Skip to content

Commit

Permalink
HSEARCH-3273 Test creation of field templates in Elasticsearch schema…
Browse files Browse the repository at this point in the history
… management
  • Loading branch information
yrodiere committed May 5, 2020
1 parent 895cae8 commit c903bc0
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
@RunWith(Parameterized.class)
@PortedFromSearch5(original = "org.hibernate.search.elasticsearch.test.Elasticsearch5SchemaCreationIT")
public class ElasticsearchIndexSchemaManagerCreationMappingIT {
public class ElasticsearchIndexSchemaManagerCreationMappingBaseIT {

private static final String INDEX_NAME = "IndexName";

Expand All @@ -53,7 +53,7 @@ public static EnumSet<ElasticsearchIndexSchemaManagerOperation> operations() {

private StubMappingIndexManager indexManager;

public ElasticsearchIndexSchemaManagerCreationMappingIT(ElasticsearchIndexSchemaManagerOperation operation) {
public ElasticsearchIndexSchemaManagerCreationMappingBaseIT(ElasticsearchIndexSchemaManagerOperation operation) {
this.operation = operation;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.integrationtest.backend.elasticsearch.schema.management;

import static org.hibernate.search.integrationtest.backend.elasticsearch.schema.management.ElasticsearchIndexSchemaManagerTestUtils.defaultMetadataMappingAndCommaForExpectations;
import static org.hibernate.search.integrationtest.backend.elasticsearch.schema.management.ElasticsearchIndexSchemaManagerTestUtils.defaultMetadataMappingForExpectations;
import static org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals;

import java.util.EnumSet;
import java.util.function.Consumer;

import org.hibernate.search.backend.elasticsearch.analysis.ElasticsearchAnalysisConfigurationContext;
import org.hibernate.search.backend.elasticsearch.analysis.ElasticsearchAnalysisConfigurer;
import org.hibernate.search.backend.elasticsearch.cfg.ElasticsearchBackendSettings;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField;
import org.hibernate.search.engine.backend.document.model.dsl.ObjectFieldStorage;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
import org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.rule.TestElasticsearchClient;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappedIndex;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingSchemaManagementStrategy;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
* Tests related to the mapping's field templates when creating indexes,
* for all index-creating schema management operations.
*/
@RunWith(Parameterized.class)
public class ElasticsearchIndexSchemaManagerCreationMappingFieldTemplatesIT {

private static final String INDEX_NAME = "IndexName";

@Parameters(name = "With operation {0}")
public static EnumSet<ElasticsearchIndexSchemaManagerOperation> operations() {
return ElasticsearchIndexSchemaManagerOperation.creating();
}

@Rule
public SearchSetupHelper setupHelper = new SearchSetupHelper();

@Rule
public TestElasticsearchClient elasticSearchClient = new TestElasticsearchClient();

private final ElasticsearchIndexSchemaManagerOperation operation;

public ElasticsearchIndexSchemaManagerCreationMappingFieldTemplatesIT(ElasticsearchIndexSchemaManagerOperation operation) {
this.operation = operation;
}

@Test
public void rootFieldTemplates() {
elasticSearchClient.index( INDEX_NAME )
.ensureDoesNotExist().registerForCleanup();

setupAndCreateIndex( root -> {
root.objectFieldTemplate( "myTemplate1", ObjectFieldStorage.NESTED )
.matchingPathGlob( "*_obj" );
root.fieldTemplate( "myTemplate2", f -> f.asString() )
.matchingPathGlob( "*_kw" );
} );

assertJsonEquals(
"{"
+ "'dynamic': 'true',"
+ "'dynamic_templates': ["
+ "{'myTemplate1': {"
+ "'match_mapping_type': 'object',"
+ "'path_match': '*_obj',"
+ "'mapping': {"
+ "'type': 'nested',"
+ "'dynamic': 'true'"
+ "}"
+ "} },"
+ "{'myTemplate2': {"
+ "'path_match': '*_kw',"
+ "'mapping': {"
+ "'type': 'keyword',"
+ "'doc_values': false,"
+ "'index': true,"
+ "'norms': false,"
+ "'store': false"
+ "}"
+ "} }"
+ "],"
+ "'properties': {"
+ defaultMetadataMappingForExpectations()
+ "}"
+ "}",
elasticSearchClient.index( INDEX_NAME ).type().getMapping()
);
}

@Test
public void nonRootFieldTemplates() {
elasticSearchClient.index( INDEX_NAME )
.ensureDoesNotExist().registerForCleanup();

setupAndCreateIndex( root -> {
IndexSchemaObjectField objectField = root.objectField( "staticObject" );
objectField.toReference();
objectField.objectFieldTemplate( "myTemplate1", ObjectFieldStorage.NESTED )
.matchingPathGlob( "*_obj" );
objectField.fieldTemplate( "myTemplate2", f -> f.asString() )
.matchingPathGlob( "*_kw" );
} );

assertJsonEquals(
"{"
+ "'dynamic': 'strict',"
+ "'dynamic_templates': ["
+ "{'staticObject.myTemplate1': {"
+ "'match_mapping_type': 'object',"
+ "'path_match': 'staticObject.*_obj',"
+ "'mapping': {"
+ "'type': 'nested',"
+ "'dynamic': 'true'"
+ "}"
+ "} },"
+ "{'staticObject.myTemplate2': {"
+ "'path_match': 'staticObject.*_kw',"
+ "'mapping': {"
+ "'type': 'keyword',"
+ "'doc_values': false,"
+ "'index': true,"
+ "'norms': false,"
+ "'store': false"
+ "}"
+ "} }"
+ "],"
+ "'properties': {"
+ defaultMetadataMappingAndCommaForExpectations()
+ "'staticObject': {"
+ "'type': 'object',"
+ "'dynamic': 'true'"
+ "}"
+ "}"
+ "}",
elasticSearchClient.index( INDEX_NAME ).type().getMapping()
);
}

private void setupAndCreateIndex(Consumer<IndexSchemaElement> binder) {
StubMappedIndex index = StubMappedIndex.withoutRetrievableBinding( INDEX_NAME, binder );

setupHelper.start()
.withIndex( index )
.withSchemaManagement( StubMappingSchemaManagementStrategy.DROP_ON_SHUTDOWN_ONLY )
.withBackendProperty(
// Don't contribute any analysis definitions, migration of those is tested in another test class
ElasticsearchBackendSettings.ANALYSIS_CONFIGURER,
(ElasticsearchAnalysisConfigurer) (ElasticsearchAnalysisConfigurationContext context) -> {
// No-op
}
)
.setup();

operation.apply( index.getSchemaManager() ).join();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,13 @@ static String defaultMetadataMappingAndCommaForInitialization() {
String mapping = defaultMetadataMappingForInitialization();
return mapping.isEmpty() ? "" : mapping + ", ";
}

static String defaultMetadataMappingForExpectations() {
return "'_entity_type': " + discriminatorMappingOmitDefaults().toString();
}

static String defaultMetadataMappingAndCommaForExpectations() {
String mapping = defaultMetadataMappingForExpectations();
return mapping.isEmpty() ? "" : mapping + ", ";
}
}

0 comments on commit c903bc0

Please sign in to comment.