|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.oss; |
| 8 | + |
| 9 | +import org.elasticsearch.action.ActionListener; |
| 10 | +import org.elasticsearch.cluster.ClusterState; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 12 | +import org.elasticsearch.cluster.metadata.MappingMetaData; |
| 13 | +import org.elasticsearch.cluster.service.ClusterService; |
| 14 | +import org.elasticsearch.common.inject.Inject; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.xpack.core.XPackFeatureSet; |
| 17 | +import org.elasticsearch.xpack.core.XPackField; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +public class IndexFeatureSet implements XPackFeatureSet { |
| 26 | + |
| 27 | + private final ClusterService clusterService; |
| 28 | + |
| 29 | + @Inject |
| 30 | + public IndexFeatureSet(ClusterService clusterService) { |
| 31 | + this.clusterService = clusterService; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public String name() { |
| 36 | + return XPackField.INDEX; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean available() { |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean enabled() { |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Map<String, Object> nativeCodeInfo() { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void usage(ActionListener<Usage> listener) { |
| 56 | + final Set<String> usedFieldTypes = new HashSet<>(); |
| 57 | + final Set<String> usedCharFilters = new HashSet<>(); |
| 58 | + final Set<String> usedTokenizers = new HashSet<>(); |
| 59 | + final Set<String> usedTokenFilters = new HashSet<>(); |
| 60 | + final Set<String> usedAnalyzers = new HashSet<>(); |
| 61 | + final Set<String> usedBuiltInCharFilters = new HashSet<>(); |
| 62 | + final Set<String> usedBuiltInTokenizers = new HashSet<>(); |
| 63 | + final Set<String> usedBuiltInTokenFilters = new HashSet<>(); |
| 64 | + final Set<String> usedBuiltInAnalyzers = new HashSet<>(); |
| 65 | + |
| 66 | + ClusterState state = clusterService.state(); |
| 67 | + if (state != null) { |
| 68 | + |
| 69 | + for (IndexMetaData indexMetaData : state.metaData()) { |
| 70 | + MappingMetaData mappingMetaData = indexMetaData.mapping(); |
| 71 | + if (mappingMetaData != null) { |
| 72 | + visitMapping(mappingMetaData.getSourceAsMap(), fieldMapping -> { |
| 73 | + Object type = fieldMapping.get("type"); |
| 74 | + if (type != null) { |
| 75 | + usedFieldTypes.add(type.toString()); |
| 76 | + } else if (fieldMapping.containsKey("properties")) { |
| 77 | + usedFieldTypes.add("object"); |
| 78 | + } |
| 79 | + |
| 80 | + for (String key : new String[] { "analyzer", "search_analyzer", "search_quote_analyzer" }) { |
| 81 | + Object analyzer = fieldMapping.get(key); |
| 82 | + if (analyzer != null) { |
| 83 | + usedBuiltInAnalyzers.add(analyzer.toString()); |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + Settings indexSettings = indexMetaData.getSettings(); |
| 90 | + |
| 91 | + Map<String, Settings> analyzerSettings = indexSettings.getGroups("index.analysis.analyzer"); |
| 92 | + usedBuiltInAnalyzers.removeAll(analyzerSettings.keySet()); |
| 93 | + for (Settings analyzerSetting : analyzerSettings.values()) { |
| 94 | + usedAnalyzers.add(analyzerSetting.get("type", "custom")); |
| 95 | + usedBuiltInCharFilters.addAll(analyzerSetting.getAsList("char_filter")); |
| 96 | + String tokenizer = analyzerSetting.get("tokenizer"); |
| 97 | + if (tokenizer != null) { |
| 98 | + usedBuiltInTokenizers.add(tokenizer); |
| 99 | + } |
| 100 | + usedBuiltInTokenFilters.addAll(analyzerSetting.getAsList("filter")); |
| 101 | + } |
| 102 | + |
| 103 | + Map<String, Settings> charFilterSettings = indexSettings.getGroups("index.analysis.char_filter"); |
| 104 | + usedBuiltInCharFilters.removeAll(charFilterSettings.keySet()); |
| 105 | + aggregateAnalysisTypes(charFilterSettings.values(), usedCharFilters); |
| 106 | + |
| 107 | + Map<String, Settings> tokenizerSettings = indexSettings.getGroups("index.analysis.tokenizer"); |
| 108 | + usedBuiltInTokenizers.removeAll(tokenizerSettings.keySet()); |
| 109 | + aggregateAnalysisTypes(tokenizerSettings.values(), usedTokenizers); |
| 110 | + |
| 111 | + Map<String, Settings> tokenFilterSettings = indexSettings.getGroups("index.analysis.filter"); |
| 112 | + usedBuiltInTokenFilters.removeAll(tokenFilterSettings.keySet()); |
| 113 | + aggregateAnalysisTypes(tokenFilterSettings.values(), usedTokenFilters); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + listener.onResponse(new IndexFeatureSetUsage(usedFieldTypes, usedCharFilters, usedTokenizers, usedTokenFilters, |
| 118 | + usedAnalyzers, usedBuiltInCharFilters, usedBuiltInTokenizers, usedBuiltInTokenFilters, usedBuiltInAnalyzers)); |
| 119 | + } |
| 120 | + |
| 121 | + static void visitMapping(Map<String, ?> mapping, Consumer<Map<String, ?>> fieldMappingConsumer) { |
| 122 | + Object properties = mapping.get("properties"); |
| 123 | + if (properties != null && properties instanceof Map) { |
| 124 | + @SuppressWarnings("unchecked") |
| 125 | + Map<String, ?> propertiesAsMap = (Map<String, ?>) properties; |
| 126 | + for (Object v : propertiesAsMap.values()) { |
| 127 | + if (v != null && v instanceof Map) { |
| 128 | + |
| 129 | + @SuppressWarnings("unchecked") |
| 130 | + Map<String, ?> fieldMapping = (Map<String, ?>) v; |
| 131 | + fieldMappingConsumer.accept(fieldMapping); |
| 132 | + visitMapping(fieldMapping, fieldMappingConsumer); |
| 133 | + |
| 134 | + // Multi fields |
| 135 | + Object fieldsO = fieldMapping.get("fields"); |
| 136 | + if (fieldsO != null && fieldsO instanceof Map) { |
| 137 | + @SuppressWarnings("unchecked") |
| 138 | + Map<String, ?> fields = (Map<String, ?>) fieldsO; |
| 139 | + for (Object v2 : fields.values()) { |
| 140 | + if (v2 instanceof Map) { |
| 141 | + @SuppressWarnings("unchecked") |
| 142 | + Map<String, ?> fieldMapping2 = (Map<String, ?>) v2; |
| 143 | + fieldMappingConsumer.accept(fieldMapping2); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + static void aggregateAnalysisTypes(Collection<Settings> analysisComponents, Set<String> usedTypes) { |
| 153 | + for (Settings settings : analysisComponents) { |
| 154 | + String type = settings.get("type"); |
| 155 | + if (type != null) { |
| 156 | + usedTypes.add(type); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments