3 * Copyright (C) 2009-2019 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.impl.config.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 public class ProjectMeasuresIndexDefinition implements IndexDefinition {
39 public static final Index DESCRIPTOR = Index.withRelations("projectmeasures");
40 public static final IndexType.IndexMainType TYPE_AUTHORIZATION = IndexType.main(DESCRIPTOR, IndexAuthorizationConstants.TYPE_AUTHORIZATION);
41 public static final IndexRelationType TYPE_PROJECT_MEASURES = IndexType.relation(TYPE_AUTHORIZATION, "projectmeasure");
43 public static final String FIELD_UUID = "uuid";
44 public static final String FIELD_ORGANIZATION_UUID = "organizationUuid";
47 * Project key. Only projects (qualifier=TRK)
49 public static final String FIELD_KEY = "key";
50 public static final String FIELD_NAME = "name";
51 public static final String FIELD_ANALYSED_AT = "analysedAt";
52 public static final String FIELD_QUALITY_GATE_STATUS = "qualityGateStatus";
53 public static final String FIELD_TAGS = "tags";
54 public static final String FIELD_MEASURES = "measures";
55 public static final String FIELD_MEASURES_KEY = "key";
56 public static final String FIELD_MEASURES_VALUE = "value";
57 public static final String FIELD_LANGUAGES = "languages";
58 public static final String FIELD_NCLOC_LANGUAGE_DISTRIBUTION = "nclocLanguageDistribution";
59 public static final String FIELD_DISTRIB_LANGUAGE = "language";
60 public static final String FIELD_DISTRIB_NCLOC = "ncloc";
62 private final Configuration config;
63 private final boolean enableSource;
65 private ProjectMeasuresIndexDefinition(Configuration config, boolean enableSource) {
67 this.enableSource = enableSource;
70 public ProjectMeasuresIndexDefinition(Configuration config) {
75 * Keep the document sources in index so that indexer tests can verify content
76 * of indexed documents.
78 public static ProjectMeasuresIndexDefinition createForTest() {
79 return new ProjectMeasuresIndexDefinition(new MapSettings().asConfig(), true);
83 public void define(IndexDefinitionContext context) {
84 NewAuthorizedIndex index = context.createWithAuthorization(
87 .setRefreshInterval(MANUAL_REFRESH_INTERVAL)
88 .setDefaultNbOfShards(5)
90 .setEnableSource(enableSource);
92 TypeMapping mapping = index.createTypeMapping(TYPE_PROJECT_MEASURES);
93 mapping.keywordFieldBuilder(FIELD_UUID).disableNorms().build();
94 mapping.keywordFieldBuilder(FIELD_ORGANIZATION_UUID).disableNorms().build();
95 mapping.keywordFieldBuilder(FIELD_KEY).disableNorms().addSubFields(SORTABLE_ANALYZER).build();
96 mapping.keywordFieldBuilder(FIELD_NAME).addSubFields(SORTABLE_ANALYZER, SEARCH_GRAMS_ANALYZER).build();
97 mapping.keywordFieldBuilder(FIELD_QUALITY_GATE_STATUS).build();
98 mapping.keywordFieldBuilder(FIELD_TAGS).build();
99 mapping.keywordFieldBuilder(FIELD_LANGUAGES).build();
100 mapping.nestedFieldBuilder(FIELD_MEASURES)
101 .addKeywordField(FIELD_MEASURES_KEY)
102 .addDoubleField(FIELD_MEASURES_VALUE)
104 mapping.nestedFieldBuilder(FIELD_NCLOC_LANGUAGE_DISTRIBUTION)
105 .addKeywordField(FIELD_DISTRIB_LANGUAGE)
106 .addIntegerField(FIELD_DISTRIB_NCLOC)
108 mapping.createDateTimeField(FIELD_ANALYSED_AT);