]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6465 Refactor string multi field definition
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 4 May 2015 10:57:08 +0000 (12:57 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 6 May 2015 08:57:07 +0000 (10:57 +0200)
server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java
server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java
server/sonar-server/src/test/java/org/sonar/server/es/NewIndexTest.java

index 8af1cb72770c91809c2b719004a9381b0d299c02..dd26b7edf84239fc35e04cbb9c62bf188c51a61e 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.es;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSortedMap;
+import com.google.common.collect.Maps;
 import org.apache.commons.lang.StringUtils;
 import org.elasticsearch.cluster.metadata.IndexMetaData;
 import org.elasticsearch.common.settings.ImmutableSettings;
@@ -174,7 +175,8 @@ public class NewIndex {
   public static class StringFieldBuilder {
     private final NewIndexType indexType;
     private final String fieldName;
-    private boolean multiField = false, sortable = false, wordSearch = false, gramSearch = false, docValues = false, disableSearch = false;
+    private boolean sortable = false, wordSearch = false, gramSearch = false, docValues = false, disableSearch = false;
+    private SortedMap<String, Object> subFields = Maps.newTreeMap();
 
     private StringFieldBuilder(NewIndexType indexType, String fieldName) {
       this.indexType = indexType;
@@ -187,10 +189,10 @@ public class NewIndex {
     }
 
     /**
-     * Prepare the structure to add sub-fields
+     * Add a sub-field
      */
-    public StringFieldBuilder enableMultiField() {
-      this.multiField = true;
+    public StringFieldBuilder addSubField(String fieldName, Map<String, String> fieldDefinition) {
+      subFields.put(fieldName, fieldDefinition);
       return this;
     }
 
@@ -198,8 +200,11 @@ public class NewIndex {
      * Create an inner-field named "sort" with analyzer "sortable"
      */
     public StringFieldBuilder enableSorting() {
-      this.enableMultiField();
       this.sortable = true;
+      addSubField(IndexField.SORT_SUFFIX, ImmutableSortedMap.of(
+        "type", "string",
+        "index", "analyzed",
+        "analyzer", "sortable"));
       return this;
     }
 
@@ -207,8 +212,12 @@ public class NewIndex {
      * Create an inner-field named "words" with analyzer "words"
      */
     public StringFieldBuilder enableWordSearch() {
-      this.enableMultiField();
       this.wordSearch = true;
+      addSubField(IndexField.SEARCH_WORDS_SUFFIX, ImmutableSortedMap.of(
+        "type", "string",
+        "index", "analyzed",
+        "index_analyzer", "index_words",
+        "search_analyzer", "search_words"));
       return this;
     }
 
@@ -216,8 +225,12 @@ public class NewIndex {
      * Create a inner-field named "grams" with analyzer "grams"
      */
     public StringFieldBuilder enableGramSearch() {
-      this.enableMultiField();
       this.gramSearch = true;
+      addSubField(IndexField.SEARCH_PARTIAL_SUFFIX, ImmutableSortedMap.of(
+        "type", "string",
+        "index", "analyzed",
+        "index_analyzer", "index_grams",
+        "search_analyzer", "search_grams"));
       return this;
     }
 
@@ -234,30 +247,9 @@ public class NewIndex {
     public void build() {
       validate();
       Map<String, Object> hash = new TreeMap<>();
-      if (multiField) {
+      if (!subFields.isEmpty()) {
         hash.put("type", "multi_field");
-        Map<String, Object> multiFields = new TreeMap<>();
-
-        if (sortable) {
-          multiFields.put(IndexField.SORT_SUFFIX, ImmutableSortedMap.of(
-            "type", "string",
-            "index", "analyzed",
-            "analyzer", "sortable"));
-        }
-        if (wordSearch) {
-          multiFields.put(IndexField.SEARCH_WORDS_SUFFIX, ImmutableSortedMap.of(
-            "type", "string",
-            "index", "analyzed",
-            "index_analyzer", "index_words",
-            "search_analyzer", "search_words"));
-        }
-        if (gramSearch) {
-          multiFields.put(IndexField.SEARCH_PARTIAL_SUFFIX, ImmutableSortedMap.of(
-            "type", "string",
-            "index", "analyzed",
-            "index_analyzer", "index_grams",
-            "search_analyzer", "search_grams"));
-        }
+        Map<String, Object> multiFields = new TreeMap<>(subFields);
         multiFields.put(fieldName, ImmutableMap.of(
           "type", "string",
           "index", "not_analyzed",
index 5b88a695edb5b4b115b4a7b3e87cf268ee3fe0c2..c5b272329b4e26edf3147b3f2ac52437d42ec0fb 100644 (file)
@@ -24,7 +24,6 @@ import com.google.common.collect.ImmutableSortedMap;
 import org.sonar.api.config.Settings;
 import org.sonar.server.es.IndexDefinition;
 import org.sonar.server.es.NewIndex;
-import org.sonar.server.es.NewIndex.NewIndexType;
 
 import java.util.Map;
 
@@ -80,10 +79,8 @@ public class UserIndexDefinition implements IndexDefinition {
     NewIndex.NewIndexType mapping = index.createType(TYPE_USER);
     mapping.setAttribute("_id", ImmutableMap.of("path", FIELD_LOGIN));
 
-    mapping.stringFieldBuilder(FIELD_LOGIN).enableMultiField().build();
-    addSubSearchField(mapping, FIELD_LOGIN);
-    mapping.stringFieldBuilder(FIELD_NAME).enableMultiField().build();
-    addSubSearchField(mapping, FIELD_NAME);
+    mapping.stringFieldBuilder(FIELD_LOGIN).addSubField(SEARCH_SUB_SUFFIX, buildGramSearchField()).build();
+    mapping.stringFieldBuilder(FIELD_NAME).addSubField(SEARCH_SUB_SUFFIX, buildGramSearchField()).build();
     mapping.stringFieldBuilder(FIELD_EMAIL).enableSorting().build();
     mapping.createDateTimeField(FIELD_CREATED_AT);
     mapping.createDateTimeField(FIELD_UPDATED_AT);
@@ -91,16 +88,11 @@ public class UserIndexDefinition implements IndexDefinition {
     mapping.stringFieldBuilder(FIELD_SCM_ACCOUNTS).build();
   }
 
-  private void addSubSearchField(NewIndexType mapping, String field) {
-    Map<String, Object> hash = (Map<String, Object>) mapping.getProperty(field);
-    if (hash == null) {
-      throw new IllegalStateException(String.format("Field %s is not defined", field));
-    }
-    Map<String, Object> multiField = (Map<String, Object>) hash.get("fields");
-    multiField.put(SEARCH_SUB_SUFFIX, ImmutableSortedMap.of(
+  private Map<String, String> buildGramSearchField() {
+    return ImmutableSortedMap.of(
       "type", "string",
       "index", "analyzed",
       "index_analyzer", "index_ngrams",
-      "search_analyzer", "search_ngrams"));
+      "search_analyzer", "search_ngrams");
   }
 }
index 20310ec57f83c37f86932b2eded005565043a998..28c7a00750a7b02a81822b678631325e6abcf4c5 100644 (file)
@@ -91,7 +91,6 @@ public class NewIndexTest {
       .enableWordSearch()
       .enableSorting()
       .build();
-    mapping.stringFieldBuilder("just_multi_field").enableMultiField().build();
 
     Map<String, Object> props = (Map) mapping.getProperty("basic_field");
     assertThat(props.get("type")).isEqualTo("string");
@@ -107,10 +106,6 @@ public class NewIndexTest {
     assertThat(props.get("type")).isEqualTo("multi_field");
     // no need to test values, it's not the scope of this test
     assertThat((Map) props.get("fields")).isNotEmpty();
-
-    props = (Map) mapping.getProperty("just_multi_field");
-    assertThat(props.get("type")).isEqualTo("multi_field");
-    assertThat((Map) props.get("fields")).isNotEmpty();
   }
 
   @Test