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.component.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.newindex.DefaultIndexSettingsElement;
28 import org.sonar.server.es.newindex.NewAuthorizedIndex;
29 import org.sonar.server.es.newindex.TypeMapping;
31 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER;
32 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_PREFIX_ANALYZER;
33 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_PREFIX_CASE_INSENSITIVE_ANALYZER;
34 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
35 import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL;
36 import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder;
37 import static org.sonar.server.permission.index.IndexAuthorizationConstants.TYPE_AUTHORIZATION;
39 public class ComponentIndexDefinition implements IndexDefinition {
41 public static final Index DESCRIPTOR = Index.withRelations("components");
42 public static final IndexType.IndexRelationType TYPE_COMPONENT = IndexType.relation(IndexType.main(DESCRIPTOR, TYPE_AUTHORIZATION), "component");
43 public static final String FIELD_UUID = "uuid";
44 public static final String FIELD_PROJECT_UUID = "project_uuid";
45 public static final String FIELD_ORGANIZATION_UUID = "organization_uuid";
46 public static final String FIELD_KEY = "key";
47 public static final String FIELD_NAME = "name";
48 public static final String FIELD_QUALIFIER = "qualifier";
49 public static final String FIELD_LANGUAGE = "language";
51 private static final int DEFAULT_NUMBER_OF_SHARDS = 5;
53 static final DefaultIndexSettingsElement[] NAME_ANALYZERS = {SORTABLE_ANALYZER, SEARCH_PREFIX_ANALYZER, SEARCH_PREFIX_CASE_INSENSITIVE_ANALYZER, SEARCH_GRAMS_ANALYZER};
55 private final Configuration config;
56 private final boolean enableSource;
58 private ComponentIndexDefinition(Configuration config, boolean enableSource) {
60 this.enableSource = enableSource;
63 public ComponentIndexDefinition(Configuration config) {
68 * Keep the document sources in index so that indexer tests can verify content
69 * of indexed documents.
71 public static ComponentIndexDefinition createForTest() {
72 return new ComponentIndexDefinition(new MapSettings().asConfig(), true);
76 public void define(IndexDefinitionContext context) {
77 NewAuthorizedIndex index = context.createWithAuthorization(
80 .setRefreshInterval(MANUAL_REFRESH_INTERVAL)
81 .setDefaultNbOfShards(DEFAULT_NUMBER_OF_SHARDS)
83 .setEnableSource(enableSource);
85 TypeMapping mapping = index.createTypeMapping(TYPE_COMPONENT);
86 mapping.keywordFieldBuilder(FIELD_UUID).disableNorms().build();
87 mapping.keywordFieldBuilder(FIELD_PROJECT_UUID).disableNorms().build();
88 mapping.keywordFieldBuilder(FIELD_KEY).addSubFields(SORTABLE_ANALYZER).build();
89 mapping.textFieldBuilder(FIELD_NAME)
91 .termVectorWithPositionOffsets()
92 .addSubFields(NAME_ANALYZERS)
95 mapping.keywordFieldBuilder(FIELD_QUALIFIER).build();
96 mapping.keywordFieldBuilder(FIELD_LANGUAGE).disableNorms().build();
97 mapping.keywordFieldBuilder(FIELD_ORGANIZATION_UUID).disableNorms().build();