/integrationtest/backend/elasticsearch/src/test/java/org/hibernate/search/integrationtest/backend/elasticsearch/schema/management/ElasticsearchIndexSchemaManagerCreationCustomMappingIT.java

https://github.com/Unimarket/hibernate-search · Java · 252 lines · 217 code · 25 blank · 10 comment · 0 complexity · 3058262213cae2fc8eee723ab41285a1 MD5 · raw file

  1. /*
  2. * Hibernate Search, full-text search for your domain model
  3. *
  4. * License: GNU Lesser General Public License (LGPL), version 2.1 or later
  5. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  6. */
  7. package org.hibernate.search.integrationtest.backend.elasticsearch.schema.management;
  8. import static org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals;
  9. import java.util.EnumSet;
  10. import org.hibernate.search.backend.elasticsearch.cfg.ElasticsearchIndexSettings;
  11. import org.hibernate.search.engine.backend.document.IndexFieldReference;
  12. import org.hibernate.search.engine.backend.document.IndexObjectFieldReference;
  13. import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
  14. import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField;
  15. import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
  16. import org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.rule.TestElasticsearchClient;
  17. import org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex;
  18. import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingSchemaManagementStrategy;
  19. import org.hibernate.search.util.impl.test.annotation.TestForIssue;
  20. import org.junit.Rule;
  21. import org.junit.Test;
  22. import org.junit.runner.RunWith;
  23. import org.junit.runners.Parameterized;
  24. /**
  25. * Tests related to index custom mapping when creating indexes,
  26. * for all index-creating schema management operations.
  27. */
  28. @RunWith(Parameterized.class)
  29. @TestForIssue(jiraKey = "HSEARCH-4253")
  30. public class ElasticsearchIndexSchemaManagerCreationCustomMappingIT {
  31. @Parameterized.Parameters(name = "With operation {0}")
  32. public static EnumSet<ElasticsearchIndexSchemaManagerOperation> operations() {
  33. return ElasticsearchIndexSchemaManagerOperation.creating();
  34. }
  35. @Rule
  36. public final SearchSetupHelper setupHelper = new SearchSetupHelper();
  37. @Rule
  38. public TestElasticsearchClient elasticsearchClient = new TestElasticsearchClient();
  39. private final SimpleMappedIndex<IndexBinding> index = SimpleMappedIndex.of( IndexBinding::new );
  40. private final ElasticsearchIndexSchemaManagerOperation operation;
  41. public ElasticsearchIndexSchemaManagerCreationCustomMappingIT(ElasticsearchIndexSchemaManagerOperation operation) {
  42. this.operation = operation;
  43. }
  44. @Test
  45. public void noOverlapping() {
  46. setupAndCreateIndex( "no-overlapping.json" );
  47. assertJsonEquals(
  48. " { " +
  49. " '_source':{ " +
  50. " 'enabled':false " +
  51. " }, " +
  52. " 'properties':{ " +
  53. " '_entity_type':{ " +
  54. " 'type':'keyword', " +
  55. " 'index':false " +
  56. " }, " +
  57. " 'bothField':{ " +
  58. " 'type':'keyword', " +
  59. " 'doc_values':false " +
  60. " }, " +
  61. " 'bothObject':{ " +
  62. " 'dynamic':'strict', " +
  63. " 'properties':{ " +
  64. " 'bothNested':{ " +
  65. " 'type':'keyword', " +
  66. " 'doc_values':false " +
  67. " }, " +
  68. " 'bothNestedObject':{ " +
  69. " 'dynamic':'strict', " +
  70. " 'properties':{ " +
  71. " 'bothNestedNested':{ " +
  72. " 'type':'keyword', " +
  73. " 'doc_values':false " +
  74. " }, " +
  75. " 'searchNestedNested':{ " +
  76. " 'type':'keyword', " +
  77. " 'doc_values':false " +
  78. " } " +
  79. " } " +
  80. " }, " +
  81. " 'searchNested':{ " +
  82. " 'type':'keyword', " +
  83. " 'doc_values':false " +
  84. " }, " +
  85. " 'searchNestedObject':{ " +
  86. " 'type':'object', " +
  87. " 'dynamic':'strict' " +
  88. " } " +
  89. " } " +
  90. " }, " +
  91. " 'searchField':{ " +
  92. " 'type':'keyword', " +
  93. " 'doc_values':false " +
  94. " }, " +
  95. " 'searchObject':{ " +
  96. " 'type':'object', " +
  97. " 'dynamic':'strict' " +
  98. " }, " +
  99. " 'userField':{ " +
  100. " 'type':'keyword', " +
  101. " 'norms':true " +
  102. " }, " +
  103. " 'userObject':{ " +
  104. " 'type':'object', " +
  105. " 'dynamic':'true' " +
  106. " } " +
  107. " } " +
  108. " } ",
  109. elasticsearchClient.index( index.name() ).type().getMapping() );
  110. }
  111. @Test
  112. public void complexConflicts() {
  113. setupAndCreateIndex( "complex-conflicts.json" );
  114. assertJsonEquals(
  115. " { " +
  116. " 'dynamic':'strict', " +
  117. " '_source':{ " +
  118. " 'enabled':false " +
  119. " }, " +
  120. " 'properties':{ " +
  121. " '_entity_type':{ " +
  122. " 'type':'keyword', " +
  123. " 'index':false " +
  124. " }, " +
  125. " 'bothField':{ " +
  126. " 'type':'keyword', " +
  127. " 'norms':true " +
  128. " }, " +
  129. " 'bothObject':{ " +
  130. " 'dynamic':'strict', " +
  131. " 'properties':{ " +
  132. " 'bothNested':{ " +
  133. " 'type':'keyword', " +
  134. " 'norms':true " +
  135. " }, " +
  136. " 'bothNestedObject':{ " +
  137. " 'dynamic':'true', " +
  138. " 'properties':{ " +
  139. " 'bothNestedNested':{ " +
  140. " 'type':'keyword', " +
  141. " 'norms':true " +
  142. " }, " +
  143. " 'searchNestedNested':{ " +
  144. " 'type':'keyword', " +
  145. " 'doc_values':false " +
  146. " }, " +
  147. " 'userNestedNested':{ " +
  148. " 'type':'keyword', " +
  149. " 'norms':true " +
  150. " } " +
  151. " } " +
  152. " }, " +
  153. " 'searchNested':{ " +
  154. " 'type':'keyword', " +
  155. " 'doc_values':false " +
  156. " }, " +
  157. " 'searchNestedObject':{ " +
  158. " 'type':'object', " +
  159. " 'dynamic':'strict' " +
  160. " }, " +
  161. " 'userNested':{ " +
  162. " 'type':'keyword', " +
  163. " 'norms':true " +
  164. " }, " +
  165. " 'userNestedObject':{ " +
  166. " 'type':'object', " +
  167. " 'dynamic':'true' " +
  168. " } " +
  169. " } " +
  170. " }, " +
  171. " 'searchField':{ " +
  172. " 'type':'keyword', " +
  173. " 'doc_values':false " +
  174. " }, " +
  175. " 'searchObject':{ " +
  176. " 'type':'object', " +
  177. " 'dynamic':'strict' " +
  178. " }, " +
  179. " 'userField':{ " +
  180. " 'type':'keyword', " +
  181. " 'norms':true " +
  182. " }, " +
  183. " 'userObject':{ " +
  184. " 'type':'object', " +
  185. " 'dynamic':'true' " +
  186. " } " +
  187. " } " +
  188. " } ",
  189. elasticsearchClient.index( index.name() ).type().getMapping() );
  190. }
  191. private void setupAndCreateIndex(String customMappingFile) {
  192. setupHelper.start()
  193. .withSchemaManagement( StubMappingSchemaManagementStrategy.DROP_ON_SHUTDOWN_ONLY )
  194. .withIndexProperty( index.name(), ElasticsearchIndexSettings.SCHEMA_MANAGEMENT_MAPPING_FILE,
  195. "custom-index-mapping/" + customMappingFile
  196. )
  197. .withIndex( index )
  198. .setup();
  199. operation.apply( index.schemaManager() ).join();
  200. }
  201. private static class IndexBinding {
  202. final IndexFieldReference<String> searchField;
  203. final IndexFieldReference<String> bothField;
  204. final IndexObjectFieldReference searchObject;
  205. final IndexObjectFieldReference bothObject;
  206. final IndexFieldReference<String> searchNested;
  207. final IndexFieldReference<String> bothNested;
  208. final IndexObjectFieldReference searchNestedObject;
  209. final IndexObjectFieldReference bothNestedObject;
  210. final IndexFieldReference<String> searchNestedNested;
  211. final IndexFieldReference<String> bothNestedNested;
  212. IndexBinding(IndexSchemaElement root) {
  213. searchField = root.field( "searchField", f -> f.asString() ).toReference();
  214. bothField = root.field( "bothField", f -> f.asString() ).toReference();
  215. searchObject = root.objectField( "searchObject" ).toReference();
  216. IndexSchemaObjectField bObject = root.objectField( "bothObject" );
  217. bothObject = bObject.toReference();
  218. searchNested = bObject.field( "searchNested", f -> f.asString() ).toReference();
  219. bothNested = bObject.field( "bothNested", f -> f.asString() ).toReference();
  220. searchNestedObject = bObject.objectField( "searchNestedObject" ).toReference();
  221. IndexSchemaObjectField bNestedObject = bObject.objectField( "bothNestedObject" );
  222. bothNestedObject = bNestedObject.toReference();
  223. searchNestedNested = bNestedObject.field( "searchNestedNested", f -> f.asString() ).toReference();
  224. bothNestedNested = bNestedObject.field( "bothNestedNested", f -> f.asString() ).toReference();
  225. }
  226. }
  227. }