Index(NewIndex newIndex) { this.name = newIndex.getName(); this.settings = newIndex.getSettings().build(); ImmutableMap.Builder<String, Type> builder = ImmutableMap.builder(); for (NewIndex.NewIndexType newIndexType : newIndex.getTypes().values()) { Type type = new Type(newIndexType); builder.put(type.getName(), type); } this.types = builder.build(); }
@Test public void test_definition_of_index() { IndexDefinition.IndexDefinitionContext context = new IndexDefinition.IndexDefinitionContext(); underTest.define(context); assertThat(context.getIndices()).hasSize(1); NewIndex ruleIndex = context.getIndices().get("rules"); assertThat(ruleIndex).isNotNull(); assertThat(ruleIndex.getTypes().keySet()).containsOnly("activeRule", "ruleExtension", "rule"); // no cluster by default assertThat(ruleIndex.getSettings().get("index.number_of_shards")).isEqualTo("2"); assertThat(ruleIndex.getSettings().get("index.number_of_replicas")).isEqualTo("0"); }
@Test public void no_types_of_none_are_specified() { assertThat(new NewIndex("foo", defaultSettingsConfiguration).getTypes()).isEmpty(); }
@Test public void index_without_source() { NewIndex index = new NewIndex("issues", defaultSettingsConfiguration); NewIndex.NewIndexType mapping = index.createType("issue"); mapping.setEnableSource(false); mapping = index.getTypes().get("issue"); assertThat(mapping).isNotNull(); assertThat(getAttributeAsMap(mapping, "_source")).containsExactly(entry("enabled", false)); }
@Test public void index_with_source() { NewIndex index = new NewIndex("issues", defaultSettingsConfiguration); NewIndex.NewIndexType mapping = index.createType("issue"); mapping.setEnableSource(true); mapping = index.getTypes().get("issue"); assertThat(mapping).isNotNull(); assertThat(getAttributeAsMap(mapping, "_source")).containsExactly(entry("enabled", true)); }
@Test public void define() { ViewIndexDefinition def = new ViewIndexDefinition(new MapSettings().asConfig()); def.define(underTest); assertThat(underTest.getIndices()).hasSize(1); NewIndex index = underTest.getIndices().get("views"); assertThat(index).isNotNull(); assertThat(index.getTypes().keySet()).containsOnly("view"); assertThat(index.getSettings().get("index.number_of_shards")).isEqualTo("5"); assertThat(index.getSettings().get("index.number_of_replicas")).isEqualTo("0"); } }
@Test public void define() { IssueIndexDefinition def = new IssueIndexDefinition(new MapSettings().asConfig()); def.define(underTest); assertThat(underTest.getIndices()).hasSize(1); NewIndex issuesIndex = underTest.getIndices().get("issues"); assertThat(issuesIndex).isNotNull(); assertThat(issuesIndex.getTypes().keySet()).containsOnly("issue", "authorization"); // no cluster by default assertThat(issuesIndex.getSettings().get("index.number_of_shards")).isEqualTo("5"); assertThat(issuesIndex.getSettings().get("index.number_of_replicas")).isEqualTo("0"); } }
@Test public void define() { UserIndexDefinition def = new UserIndexDefinition(new MapSettings().asConfig()); def.define(underTest); assertThat(underTest.getIndices()).hasSize(1); NewIndex index = underTest.getIndices().get("users"); assertThat(index).isNotNull(); assertThat(index.getTypes().keySet()).containsOnly("user"); // no cluster by default assertThat(index.getSettings().get("index.number_of_shards")).isEqualTo("1"); assertThat(index.getSettings().get("index.number_of_replicas")).isEqualTo("0"); } }
@Test public void index_requires_project_authorization() { NewIndex index = new NewIndex("issues", defaultSettingsConfiguration); index.createType("issue") // creates a second type "authorization" and configures _parent and _routing fields .requireProjectAuthorization(); // issue type NewIndex.NewIndexType issueType = index.getTypes().get("issue"); assertThat(getAttributeAsMap(issueType, "_parent")).containsExactly(entry("type", "authorization")); assertThat(getAttributeAsMap(issueType, "_routing")).containsExactly(entry("required", true)); // authorization type NewIndex.NewIndexType authorizationType = index.getTypes().get("authorization"); assertThat(getAttributeAsMap(authorizationType, "_parent")).isNull(); assertThat(getAttributeAsMap(authorizationType, "_routing")).containsExactly(entry("required", true)); }
@Test public void of() { IndexDefinition.Index indexV1 = new IndexDefinition.Index(createIndex()); String hashV1 = IndexDefinitionHash.of(indexV1); assertThat(hashV1).isNotEmpty(); // always the same assertThat(hashV1).isEqualTo(IndexDefinitionHash.of(indexV1)); NewIndex newIndexV2 = createIndex(); newIndexV2.getTypes().get("fake").createIntegerField("max"); String hashV2 = IndexDefinitionHash.of(new IndexDefinition.Index(newIndexV2)); assertThat(hashV2).isNotEmpty().isNotEqualTo(hashV1); }
@Test public void define_fields() { NewIndex index = new NewIndex("issues", defaultSettingsConfiguration); NewIndex.NewIndexType mapping = index.createType("issue"); mapping.setAttribute("dynamic", "true"); mapping.setProperty("foo_field", ImmutableMap.of("type", "keyword")); mapping.createBooleanField("boolean_field"); mapping.createByteField("byte_field"); mapping.createDateTimeField("dt_field"); mapping.createDoubleField("double_field"); mapping.createIntegerField("int_field"); mapping.createLongField("long_field"); mapping.createShortField("short_field"); mapping.createUuidPathField("uuid_path_field"); mapping = index.getTypes().get("issue"); assertThat(mapping).isNotNull(); assertThat(mapping.getAttributes().get("dynamic")).isEqualTo("true"); assertThat(mapping.getProperty("foo_field")).isInstanceOf(Map.class); assertThat((Map) mapping.getProperty("foo_field")).containsEntry("type", "keyword"); assertThat((Map) mapping.getProperty("byte_field")).isNotEmpty(); assertThat((Map) mapping.getProperty("double_field")).isNotEmpty(); assertThat((Map) mapping.getProperty("dt_field")).isNotEmpty(); assertThat((Map) mapping.getProperty("int_field")).containsEntry("type", "integer"); assertThat((Map) mapping.getProperty("long_field")).isNotEmpty(); assertThat((Map) mapping.getProperty("short_field")).isNotEmpty(); assertThat((Map) mapping.getProperty("uuid_path_field")).isNotEmpty(); assertThat((Map) mapping.getProperty("unknown")).isNull(); }
Index(NewIndex newIndex) { this.name = newIndex.getName(); this.settings = newIndex.getSettings().build(); ImmutableMap.Builder<String, IndexType> builder = ImmutableMap.builder(); for (NewIndex.NewIndexType newIndexType : newIndex.getTypes().values()) { IndexType type = new IndexType(newIndexType); builder.put(type.getName(), type); } this.types = builder.build(); }