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;
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;
}
/**
- * 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;
}
* 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;
}
* 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;
}
* 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;
}
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",
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;
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);
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");
}
}