Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ComponentIndexDefinition.java 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.component.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.newindex.DefaultIndexSettingsElement;
  27. import org.sonar.server.es.newindex.NewAuthorizedIndex;
  28. import org.sonar.server.es.newindex.TypeMapping;
  29. import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER;
  30. import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_PREFIX_ANALYZER;
  31. import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_PREFIX_CASE_INSENSITIVE_ANALYZER;
  32. import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
  33. import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL;
  34. import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder;
  35. import static org.sonar.server.permission.index.IndexAuthorizationConstants.TYPE_AUTHORIZATION;
  36. public class ComponentIndexDefinition implements IndexDefinition {
  37. public static final Index DESCRIPTOR = Index.withRelations("components");
  38. public static final IndexType.IndexRelationType TYPE_COMPONENT = IndexType.relation(IndexType.main(DESCRIPTOR, TYPE_AUTHORIZATION), "component");
  39. public static final String FIELD_UUID = "uuid";
  40. public static final String FIELD_PROJECT_UUID = "project_uuid";
  41. public static final String FIELD_KEY = "key";
  42. public static final String FIELD_NAME = "name";
  43. public static final String FIELD_QUALIFIER = "qualifier";
  44. private static final int DEFAULT_NUMBER_OF_SHARDS = 5;
  45. static final DefaultIndexSettingsElement[] NAME_ANALYZERS = {SORTABLE_ANALYZER, SEARCH_PREFIX_ANALYZER, SEARCH_PREFIX_CASE_INSENSITIVE_ANALYZER, SEARCH_GRAMS_ANALYZER};
  46. private final Configuration config;
  47. private final boolean enableSource;
  48. private ComponentIndexDefinition(Configuration config, boolean enableSource) {
  49. this.config = config;
  50. this.enableSource = enableSource;
  51. }
  52. public ComponentIndexDefinition(Configuration config) {
  53. this(config, false);
  54. }
  55. /**
  56. * Keep the document sources in index so that indexer tests can verify content
  57. * of indexed documents.
  58. */
  59. public static ComponentIndexDefinition createForTest() {
  60. return new ComponentIndexDefinition(new MapSettings().asConfig(), true);
  61. }
  62. @Override
  63. public void define(IndexDefinitionContext context) {
  64. NewAuthorizedIndex index = context.createWithAuthorization(
  65. DESCRIPTOR,
  66. newBuilder(config)
  67. .setRefreshInterval(MANUAL_REFRESH_INTERVAL)
  68. .setDefaultNbOfShards(DEFAULT_NUMBER_OF_SHARDS)
  69. .build())
  70. .setEnableSource(enableSource);
  71. TypeMapping mapping = index.createTypeMapping(TYPE_COMPONENT);
  72. mapping.keywordFieldBuilder(FIELD_UUID).disableNorms().build();
  73. mapping.keywordFieldBuilder(FIELD_PROJECT_UUID).disableNorms().build();
  74. mapping.keywordFieldBuilder(FIELD_KEY).addSubFields(SORTABLE_ANALYZER).build();
  75. mapping.textFieldBuilder(FIELD_NAME)
  76. .withFieldData()
  77. // required by highlighting
  78. .store()
  79. .termVectorWithPositionOffsets()
  80. .addSubFields(NAME_ANALYZERS)
  81. .build();
  82. mapping.keywordFieldBuilder(FIELD_QUALIFIER).build();
  83. }
  84. }