diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-10-11 14:41:59 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk@users.noreply.github.com> | 2017-10-12 14:50:59 +0200 |
commit | 1c69c861bcdb3fd41e5eed0a051b3ea8022642f7 (patch) | |
tree | 7d9f916f5abfa7e47c437bdc1669c7f7fb88a28d | |
parent | 01f503279a25c643069968b262492981fb5826ae (diff) | |
download | sonarqube-1c69c861bcdb3fd41e5eed0a051b3ea8022642f7.tar.gz sonarqube-1c69c861bcdb3fd41e5eed0a051b3ea8022642f7.zip |
SONAR-9928 Index language in component ES index
4 files changed, 36 insertions, 2 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java index a785c575d07..eafeece2962 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java @@ -20,9 +20,13 @@ package org.sonar.server.component.index; import java.util.HashMap; +import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.server.es.BaseDoc; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_KEY; +import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_LANGUAGE; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_PROJECT_UUID; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_QUALIFIER; @@ -34,6 +38,10 @@ public class ComponentDoc extends BaseDoc { super(new HashMap<>(6)); } + public ComponentDoc(Map<String, Object> fields) { + super(fields); + } + @Override public String getId() { return getField(FIELD_UUID); @@ -89,4 +97,14 @@ public class ComponentDoc extends BaseDoc { setField(FIELD_QUALIFIER, s); return this; } + + @CheckForNull + public String getLanguage() { + return getField(FIELD_LANGUAGE); + } + + public ComponentDoc setLanguage(@Nullable String s) { + setField(FIELD_LANGUAGE, s); + return this; + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java index d0b60c84ec3..352dd847b65 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java @@ -40,6 +40,7 @@ public class ComponentIndexDefinition implements IndexDefinition { public static final String FIELD_KEY = "key"; public static final String FIELD_NAME = "name"; public static final String FIELD_QUALIFIER = "qualifier"; + public static final String FIELD_LANGUAGE = "language"; private static final int DEFAULT_NUMBER_OF_SHARDS = 5; @@ -73,5 +74,6 @@ public class ComponentIndexDefinition implements IndexDefinition { .build(); mapping.keywordFieldBuilder(FIELD_QUALIFIER).build(); + mapping.keywordFieldBuilder(FIELD_LANGUAGE).disableNorms().build(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java index e9fd1eb5bb9..f0b195e5288 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java @@ -187,6 +187,7 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe .setName(component.name()) .setKey(component.getDbKey()) .setProjectUuid(component.projectUuid()) + .setLanguage(component.language()) .setQualifier(component.qualifier()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java index 74de66dae6b..85d0fb6a03f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java @@ -40,7 +40,6 @@ import static java.util.Collections.emptySet; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME; import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_TYPE_COMPONENT; @@ -84,6 +83,21 @@ public class ComponentIndexerTest { } @Test + public void map_fields() { + ComponentDto project = db.components().insertPrivateProject(p -> p.setLanguage("java")); + + underTest.indexOnStartup(emptySet()); + + assertThatIndexContainsOnly(project); + ComponentDoc doc = es.getDocuments(INDEX_TYPE_COMPONENT, ComponentDoc.class).get(0); + assertThat(doc.getId()).isEqualTo(project.uuid()); + assertThat(doc.getKey()).isEqualTo(project.getDbKey()); + assertThat(doc.getProjectUuid()).isEqualTo(project.projectUuid()); + assertThat(doc.getName()).isEqualTo(project.name()); + assertThat(doc.getLanguage()).isEqualTo(project.language()); + } + + @Test public void indexOnStartup_does_not_index_non_main_branches() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); @@ -258,7 +272,6 @@ public class ComponentIndexerTest { return underTest.index(db.getSession(), items); } - private void assertThatIndexHasSize(int expectedSize) { assertThat(es.countDocuments(INDEX_TYPE_COMPONENT)).isEqualTo(expectedSize); } |