]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6465 Prevent side effect of multi-field mapping creation
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 4 May 2015 08:43:53 +0000 (10:43 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 6 May 2015 08:57:06 +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 ece5f4d0804e39d94f31df5ec6b9630c1f704dd5..8af1cb72770c91809c2b719004a9381b0d299c02 100644 (file)
@@ -174,7 +174,7 @@ public class NewIndex {
   public static class StringFieldBuilder {
     private final NewIndexType indexType;
     private final String fieldName;
-    private boolean sortable = false, wordSearch = false, gramSearch = false, docValues = false, disableSearch = false;
+    private boolean multiField = false, sortable = false, wordSearch = false, gramSearch = false, docValues = false, disableSearch = false;
 
     private StringFieldBuilder(NewIndexType indexType, String fieldName) {
       this.indexType = indexType;
@@ -186,10 +186,19 @@ public class NewIndex {
       return this;
     }
 
+    /**
+     * Prepare the structure to add sub-fields
+     */
+    public StringFieldBuilder enableMultiField() {
+      this.multiField = true;
+      return this;
+    }
+
     /**
      * Create an inner-field named "sort" with analyzer "sortable"
      */
     public StringFieldBuilder enableSorting() {
+      this.enableMultiField();
       this.sortable = true;
       return this;
     }
@@ -198,6 +207,7 @@ public class NewIndex {
      * Create an inner-field named "words" with analyzer "words"
      */
     public StringFieldBuilder enableWordSearch() {
+      this.enableMultiField();
       this.wordSearch = true;
       return this;
     }
@@ -206,6 +216,7 @@ public class NewIndex {
      * Create a inner-field named "grams" with analyzer "grams"
      */
     public StringFieldBuilder enableGramSearch() {
+      this.enableMultiField();
       this.gramSearch = true;
       return this;
     }
@@ -223,7 +234,7 @@ public class NewIndex {
     public void build() {
       validate();
       Map<String, Object> hash = new TreeMap<>();
-      if (wordSearch || sortable || gramSearch) {
+      if (multiField) {
         hash.put("type", "multi_field");
         Map<String, Object> multiFields = new TreeMap<>();
 
index 923502cf2a3ef2f95a2be7afbc23d46d11addf51..5b88a695edb5b4b115b4a7b3e87cf268ee3fe0c2 100644 (file)
@@ -80,9 +80,9 @@ public class UserIndexDefinition implements IndexDefinition {
     NewIndex.NewIndexType mapping = index.createType(TYPE_USER);
     mapping.setAttribute("_id", ImmutableMap.of("path", FIELD_LOGIN));
 
-    mapping.stringFieldBuilder(FIELD_LOGIN).enableGramSearch().build();
+    mapping.stringFieldBuilder(FIELD_LOGIN).enableMultiField().build();
     addSubSearchField(mapping, FIELD_LOGIN);
-    mapping.stringFieldBuilder(FIELD_NAME).enableGramSearch().build();
+    mapping.stringFieldBuilder(FIELD_NAME).enableMultiField().build();
     addSubSearchField(mapping, FIELD_NAME);
     mapping.stringFieldBuilder(FIELD_EMAIL).enableSorting().build();
     mapping.createDateTimeField(FIELD_CREATED_AT);
index 28c7a00750a7b02a81822b678631325e6abcf4c5..20310ec57f83c37f86932b2eded005565043a998 100644 (file)
@@ -91,6 +91,7 @@ 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");
@@ -106,6 +107,10 @@ 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