From 4c1a2f226b279b9b173c65695a07182717a9ffe3 Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Thu, 12 Oct 2017 10:09:24 +0200 Subject: [PATCH] SONAR-9965 avoid obsolete subfields in Elasticsearch keyword fields Each field with subfields used to have one obsolete additional subfield with the same name as the name itself. This one is now removed. Those obsolete subfields also sometimes caused issues with long text values (like rule descriptions or test messages). --- .../java/org/sonar/server/es/NewIndex.java | 66 ++++++++----------- 1 file changed, 29 insertions(+), 37 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 263fa0d9b46..13701a250e4 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 @@ -372,57 +372,49 @@ public class NewIndex { private NewIndexType buildWithSubfields() { Map hash = new TreeMap<>(); hash.put("type", getFieldType()); - - Map multiFields = new TreeMap<>(subFields); - - if (termVectorWithPositionOffsets) { - multiFields.entrySet().forEach(entry -> { - Object subFieldMapping = entry.getValue(); - if (subFieldMapping instanceof Map) { - entry.setValue( - addFieldToMapping( - (Map) subFieldMapping, - FIELD_TERM_VECTOR, "with_positions_offsets")); - } - }); - hash.put(FIELD_TERM_VECTOR, "with_positions_offsets"); + hash.put(INDEX, disableSearch ? INDEX_NOT_SEARCHABLE : INDEX_SEARCHABLE); + hash.put("norms", "false"); + hash.put("store", valueOf(store)); + if (FIELD_TYPE_KEYWORD.equals(getFieldType())) { + hash.put("doc_values", valueOf(!disabledDocValues)); } if (getFieldData()) { - multiFields.entrySet().forEach(entry -> { - Object subFieldMapping = entry.getValue(); - if (subFieldMapping instanceof Map) { - entry.setValue( - addFieldToMapping( - (Map) subFieldMapping, - FIELD_FIELDDATA, FIELDDATA_ENABLED)); - } - }); hash.put(FIELD_FIELDDATA, FIELDDATA_ENABLED); } + if (termVectorWithPositionOffsets) { + hash.put(FIELD_TERM_VECTOR, "with_positions_offsets"); + } + hash.put("fields", configureSubFields()); + return indexType.setProperty(fieldName, hash); + } + + private Map configureSubFields() { + Map multiFields = new TreeMap<>(subFields); + + // apply this fields configuration to all subfields + multiFields.entrySet().forEach(entry -> { + Object subFieldMapping = entry.getValue(); + if (subFieldMapping instanceof Map) { + entry.setValue(configureSubField((Map) subFieldMapping)); + } + }); + return multiFields; + } - Map subHash = new TreeMap<>(); - subHash.put("type", getFieldType()); + private Map configureSubField(Map subFieldMapping) { + Map subHash = new TreeMap<>(subFieldMapping); subHash.put(INDEX, INDEX_SEARCHABLE); subHash.put("norms", "false"); subHash.put("store", valueOf(store)); - if (FIELD_TYPE_KEYWORD.equals(getFieldType())) { - subHash.put("doc_values", valueOf(!disabledDocValues)); + if (termVectorWithPositionOffsets) { + subHash.put(FIELD_TERM_VECTOR, "with_positions_offsets"); } - multiFields.put(fieldName, subHash); - hash.put("fields", multiFields); - - return indexType.setProperty(fieldName, hash); + return subHash; } protected abstract boolean getFieldData(); protected abstract String getFieldType(); - - private static SortedMap addFieldToMapping(Map source, String key, String value) { - SortedMap mutable = new TreeMap<>(source); - mutable.put(key, value); - return ImmutableSortedMap.copyOf(mutable); - } } public static class KeywordFieldBuilder extends StringFieldBuilder { -- 2.39.5