You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectMeasuresIndexDefinition.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.measure.index;
  21. import org.sonar.api.config.Configuration;
  22. import org.sonar.api.config.internal.MapSettings;
  23. import org.sonar.server.es.Index;
  24. import org.sonar.server.es.IndexDefinition;
  25. import org.sonar.server.es.IndexType;
  26. import org.sonar.server.es.IndexType.IndexRelationType;
  27. import org.sonar.server.es.newindex.NewAuthorizedIndex;
  28. import org.sonar.server.es.newindex.TypeMapping;
  29. import org.sonar.server.permission.index.IndexAuthorizationConstants;
  30. import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER;
  31. import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
  32. import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL;
  33. import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder;
  34. public class ProjectMeasuresIndexDefinition implements IndexDefinition {
  35. public static final Index DESCRIPTOR = Index.withRelations("projectmeasures");
  36. public static final IndexType.IndexMainType TYPE_AUTHORIZATION = IndexType.main(DESCRIPTOR, IndexAuthorizationConstants.TYPE_AUTHORIZATION);
  37. public static final IndexRelationType TYPE_PROJECT_MEASURES = IndexType.relation(TYPE_AUTHORIZATION, "projectmeasure");
  38. public static final String FIELD_UUID = "uuid";
  39. /**
  40. * Project key. Only projects and applications (qualifier=TRK, APP)
  41. */
  42. public static final String FIELD_KEY = "key";
  43. public static final String FIELD_NAME = "name";
  44. public static final String FIELD_QUALIFIER = "qualifier";
  45. public static final String FIELD_ANALYSED_AT = "analysedAt";
  46. public static final String FIELD_QUALITY_GATE_STATUS = "qualityGateStatus";
  47. public static final String FIELD_TAGS = "tags";
  48. public static final String FIELD_MEASURES = "measures";
  49. public static final String SUB_FIELD_MEASURES_KEY = "key";
  50. public static final String SUB_FIELD_MEASURES_VALUE = "value";
  51. public static final String FIELD_MEASURES_MEASURE_KEY = FIELD_MEASURES + "." + SUB_FIELD_MEASURES_KEY;
  52. public static final String FIELD_MEASURES_MEASURE_VALUE = FIELD_MEASURES + "." + SUB_FIELD_MEASURES_VALUE;
  53. public static final String FIELD_LANGUAGES = "languages";
  54. public static final String FIELD_NCLOC_DISTRIBUTION = "nclocLanguageDistribution";
  55. public static final String SUB_FIELD_DISTRIB_LANGUAGE = "language";
  56. public static final String SUB_FIELD_DISTRIB_NCLOC = "ncloc";
  57. public static final String FIELD_NCLOC_DISTRIBUTION_LANGUAGE = FIELD_NCLOC_DISTRIBUTION + "." + SUB_FIELD_DISTRIB_LANGUAGE;
  58. public static final String FIELD_NCLOC_DISTRIBUTION_NCLOC = FIELD_NCLOC_DISTRIBUTION + "." + SUB_FIELD_DISTRIB_NCLOC;
  59. private final Configuration config;
  60. private final boolean enableSource;
  61. private ProjectMeasuresIndexDefinition(Configuration config, boolean enableSource) {
  62. this.config = config;
  63. this.enableSource = enableSource;
  64. }
  65. public ProjectMeasuresIndexDefinition(Configuration config) {
  66. this(config, false);
  67. }
  68. /**
  69. * Keep the document sources in index so that indexer tests can verify content
  70. * of indexed documents.
  71. */
  72. public static ProjectMeasuresIndexDefinition createForTest() {
  73. return new ProjectMeasuresIndexDefinition(new MapSettings().asConfig(), true);
  74. }
  75. @Override
  76. public void define(IndexDefinitionContext context) {
  77. NewAuthorizedIndex index = context.createWithAuthorization(
  78. DESCRIPTOR,
  79. newBuilder(config)
  80. .setRefreshInterval(MANUAL_REFRESH_INTERVAL)
  81. .setDefaultNbOfShards(5)
  82. .build())
  83. .setEnableSource(enableSource);
  84. TypeMapping mapping = index.createTypeMapping(TYPE_PROJECT_MEASURES);
  85. mapping.keywordFieldBuilder(FIELD_UUID).disableNorms().build();
  86. mapping.keywordFieldBuilder(FIELD_KEY).disableNorms().addSubFields(SORTABLE_ANALYZER).build();
  87. mapping.keywordFieldBuilder(FIELD_QUALIFIER).disableNorms().build();
  88. mapping.keywordFieldBuilder(FIELD_NAME).addSubFields(SORTABLE_ANALYZER, SEARCH_GRAMS_ANALYZER).build();
  89. mapping.keywordFieldBuilder(FIELD_QUALITY_GATE_STATUS).build();
  90. mapping.keywordFieldBuilder(FIELD_TAGS).build();
  91. mapping.keywordFieldBuilder(FIELD_LANGUAGES).build();
  92. mapping.nestedFieldBuilder(FIELD_MEASURES)
  93. .addKeywordField(SUB_FIELD_MEASURES_KEY)
  94. .addDoubleField(SUB_FIELD_MEASURES_VALUE)
  95. .build();
  96. mapping.nestedFieldBuilder(FIELD_NCLOC_DISTRIBUTION)
  97. .addKeywordField(SUB_FIELD_DISTRIB_LANGUAGE)
  98. .addIntegerField(SUB_FIELD_DISTRIB_NCLOC)
  99. .build();
  100. mapping.createDateTimeField(FIELD_ANALYSED_AT);
  101. }
  102. }