import org.elasticsearch.common.settings.Settings;
import org.sonar.process.ProcessProperties;
+import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static org.sonar.server.es.BaseIndex.SEARCH_PARTIAL_SUFFIX;
import static org.sonar.server.es.BaseIndex.SEARCH_WORDS_SUFFIX;
return new StringFieldBuilder(this, fieldName);
}
+ public NestedFieldBuilder nestedFieldBuilder(String fieldName) {
+ return new NestedFieldBuilder(this, fieldName);
+ }
+
public NewIndexType createBooleanField(String fieldName) {
return setProperty(fieldName, ImmutableMap.of("type", "boolean"));
}
return this;
}
- public void build() {
+ public NewIndexType build() {
Map<String, Object> hash = new TreeMap<>();
if (subFields.isEmpty()) {
hash.putAll(ImmutableMap.of(
hash.put("fields", multiFields);
}
- indexType.setProperty(fieldName, hash);
+ return indexType.setProperty(fieldName, hash);
+ }
+ }
+
+ public static class NestedFieldBuilder {
+ private final NewIndexType indexType;
+ private final String fieldName;
+ private final Map<String, Object> properties = new TreeMap<>();
+
+ private NestedFieldBuilder(NewIndexType indexType, String fieldName) {
+ this.indexType = indexType;
+ this.fieldName = fieldName;
+ }
+
+ private NestedFieldBuilder setProperty(String fieldName, Object value) {
+ properties.put(fieldName, value);
+
+ return this;
+ }
+
+ public NestedFieldBuilder addStringFied(String fieldName) {
+ return setProperty(fieldName, ImmutableMap.of("type", "string"));
+ }
+
+ public NestedFieldBuilder addDoubleField(String fieldName) {
+ return setProperty(fieldName, ImmutableMap.of("type", "double"));
+ }
+
+ public NewIndexType build() {
+ checkArgument(!properties.isEmpty(), "At least one sub-field must be declared in nested property '%s'", fieldName);
+ Map<String, Object> hash = new TreeMap<>();
+ hash.put("type", "nested");
+ hash.put("properties", properties);
+
+ return indexType.setProperty(fieldName, hash);
}
}
import org.assertj.core.data.MapEntry;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.config.MapSettings;
import org.sonar.process.ProcessProperties;
public class NewIndexTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
@Test
public void most_basic_index() {
NewIndex index = new NewIndex("issues");
assertThat((Map) props.get("fields")).isNotEmpty();
}
+ @Test
+ public void define_nested_field() {
+ NewIndex index = new NewIndex("projectmeasures");
+ NewIndex.NewIndexType mapping = index.createType("projectmeasures");
+
+ mapping.nestedFieldBuilder("measures")
+ .addStringFied("key")
+ .addDoubleField("value")
+ .build();
+ Map<String, Object> result = (Map) mapping.getProperty("measures");
+
+ assertThat(result.get("type")).isEqualTo("nested");
+ Map<String, Map<String, Object>> subProperties = (Map) result.get("properties");
+ assertThat(subProperties.get("key").get("type")).isEqualTo("string");
+ assertThat(subProperties.get("value").get("type")).isEqualTo("double");
+ }
+
+ @Test
+ public void fail_when_nested_with_no_field() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("At least one sub-field must be declared in nested property 'measures'");
+
+ NewIndex index = new NewIndex("projectmeasures");
+ NewIndex.NewIndexType mapping = index.createType("project_measures");
+
+ mapping.nestedFieldBuilder("measures").build();
+ }
+
@Test
public void use_default_doc_values() {
NewIndex index = new NewIndex("issues");
mapping = index.getTypes().get("issue");
assertThat(mapping).isNotNull();
- assertThat((Map<String, Object>)mapping.getAttributes().get("_source")).containsExactly(MapEntry.entry("enabled", true));
+ assertThat((Map<String, Object>) mapping.getAttributes().get("_source")).containsExactly(MapEntry.entry("enabled", true));
}
@Test
mapping = index.getTypes().get("issue");
assertThat(mapping).isNotNull();
- assertThat((Map<String, Object>)mapping.getAttributes().get("_source")).containsExactly(MapEntry.entry("enabled", false));
+ assertThat((Map<String, Object>) mapping.getAttributes().get("_source")).containsExactly(MapEntry.entry("enabled", false));
}
}