From aa0cae74eda0e6cf94b9a2386451514623e6723e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 4 May 2015 12:57:08 +0200 Subject: [PATCH] SONAR-6465 Refactor string multi field definition --- .../java/org/sonar/server/es/NewIndex.java | 52 ++++++++----------- .../user/index/UserIndexDefinition.java | 18 ++----- .../org/sonar/server/es/NewIndexTest.java | 5 -- 3 files changed, 27 insertions(+), 48 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java b/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java index 8af1cb72770..dd26b7edf84 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java @@ -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 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 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 hash = new TreeMap<>(); - if (multiField) { + if (!subFields.isEmpty()) { hash.put("type", "multi_field"); - Map 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 multiFields = new TreeMap<>(subFields); multiFields.put(fieldName, ImmutableMap.of( "type", "string", "index", "not_analyzed", diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java b/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java index 5b88a695edb..c5b272329b4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndexDefinition.java @@ -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 hash = (Map) mapping.getProperty(field); - if (hash == null) { - throw new IllegalStateException(String.format("Field %s is not defined", field)); - } - Map multiField = (Map) hash.get("fields"); - multiField.put(SEARCH_SUB_SUFFIX, ImmutableSortedMap.of( + private Map buildGramSearchField() { + return ImmutableSortedMap.of( "type", "string", "index", "analyzed", "index_analyzer", "index_ngrams", - "search_analyzer", "search_ngrams")); + "search_analyzer", "search_ngrams"); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/NewIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/NewIndexTest.java index 20310ec57f8..28c7a00750a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/NewIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/NewIndexTest.java @@ -91,7 +91,6 @@ public class NewIndexTest { .enableWordSearch() .enableSorting() .build(); - mapping.stringFieldBuilder("just_multi_field").enableMultiField().build(); Map 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 -- 2.39.5