3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.measure.index;
22 import org.sonar.api.config.Configuration;
23 import org.sonar.api.config.internal.MapSettings;
24 import org.sonar.server.es.Index;
25 import org.sonar.server.es.IndexDefinition;
26 import org.sonar.server.es.IndexType;
27 import org.sonar.server.es.IndexType.IndexRelationType;
28 import org.sonar.server.es.newindex.NewAuthorizedIndex;
29 import org.sonar.server.es.newindex.TypeMapping;
30 import org.sonar.server.permission.index.IndexAuthorizationConstants;
32 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER;
33 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
34 import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL;
35 import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder;
37 import javax.inject.Inject;
39 public class ProjectMeasuresIndexDefinition implements IndexDefinition {
41 public static final Index DESCRIPTOR = Index.withRelations("projectmeasures");
42 public static final IndexType.IndexMainType TYPE_AUTHORIZATION = IndexType.main(DESCRIPTOR, IndexAuthorizationConstants.TYPE_AUTHORIZATION);
43 public static final IndexRelationType TYPE_PROJECT_MEASURES = IndexType.relation(TYPE_AUTHORIZATION, "projectmeasure");
45 public static final String FIELD_UUID = "uuid";
48 * Project key. Only projects and applications (qualifier=TRK, APP)
50 public static final String FIELD_KEY = "key";
51 public static final String FIELD_NAME = "name";
52 public static final String FIELD_QUALIFIER = "qualifier";
53 public static final String FIELD_ANALYSED_AT = "analysedAt";
54 public static final String FIELD_QUALITY_GATE_STATUS = "qualityGateStatus";
55 public static final String FIELD_TAGS = "tags";
56 public static final String FIELD_MEASURES = "measures";
57 public static final String SUB_FIELD_MEASURES_KEY = "key";
58 public static final String SUB_FIELD_MEASURES_VALUE = "value";
59 public static final String FIELD_MEASURES_MEASURE_KEY = FIELD_MEASURES + "." + SUB_FIELD_MEASURES_KEY;
60 public static final String FIELD_MEASURES_MEASURE_VALUE = FIELD_MEASURES + "." + SUB_FIELD_MEASURES_VALUE;
61 public static final String FIELD_LANGUAGES = "languages";
62 public static final String FIELD_NCLOC_DISTRIBUTION = "nclocLanguageDistribution";
63 public static final String SUB_FIELD_DISTRIB_LANGUAGE = "language";
64 public static final String SUB_FIELD_DISTRIB_NCLOC = "ncloc";
65 public static final String FIELD_NCLOC_DISTRIBUTION_LANGUAGE = FIELD_NCLOC_DISTRIBUTION + "." + SUB_FIELD_DISTRIB_LANGUAGE;
66 public static final String FIELD_NCLOC_DISTRIBUTION_NCLOC = FIELD_NCLOC_DISTRIBUTION + "." + SUB_FIELD_DISTRIB_NCLOC;
68 private final Configuration config;
69 private final boolean enableSource;
71 private ProjectMeasuresIndexDefinition(Configuration config, boolean enableSource) {
73 this.enableSource = enableSource;
77 public ProjectMeasuresIndexDefinition(Configuration config) {
82 * Keep the document sources in index so that indexer tests can verify content
83 * of indexed documents.
85 public static ProjectMeasuresIndexDefinition createForTest() {
86 return new ProjectMeasuresIndexDefinition(new MapSettings().asConfig(), true);
90 public void define(IndexDefinitionContext context) {
91 NewAuthorizedIndex index = context.createWithAuthorization(
94 .setRefreshInterval(MANUAL_REFRESH_INTERVAL)
95 .setDefaultNbOfShards(5)
97 .setEnableSource(enableSource);
99 TypeMapping mapping = index.createTypeMapping(TYPE_PROJECT_MEASURES);
100 mapping.keywordFieldBuilder(FIELD_UUID).disableNorms().build();
101 mapping.keywordFieldBuilder(FIELD_KEY).disableNorms().addSubFields(SORTABLE_ANALYZER).build();
102 mapping.keywordFieldBuilder(FIELD_QUALIFIER).disableNorms().build();
103 mapping.keywordFieldBuilder(FIELD_NAME).addSubFields(SORTABLE_ANALYZER, SEARCH_GRAMS_ANALYZER).build();
104 mapping.keywordFieldBuilder(FIELD_QUALITY_GATE_STATUS).build();
105 mapping.keywordFieldBuilder(FIELD_TAGS).build();
106 mapping.keywordFieldBuilder(FIELD_LANGUAGES).build();
107 mapping.nestedFieldBuilder(FIELD_MEASURES)
108 .addKeywordField(SUB_FIELD_MEASURES_KEY)
109 .addDoubleField(SUB_FIELD_MEASURES_VALUE)
111 mapping.nestedFieldBuilder(FIELD_NCLOC_DISTRIBUTION)
112 .addKeywordField(SUB_FIELD_DISTRIB_LANGUAGE)
113 .addIntegerField(SUB_FIELD_DISTRIB_NCLOC)
115 mapping.createDateTimeField(FIELD_ANALYSED_AT);