]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9928 Index language in component ES index
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 11 Oct 2017 12:41:59 +0000 (14:41 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Thu, 12 Oct 2017 12:50:59 +0000 (14:50 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentDoc.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java

index a785c575d07f6164e479c589754c198859a88228..eafeece2962f607f18a15d2448d098f191afba40 100644 (file)
 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;
+  }
 }
index d0b60c84ec32c52f12417018f95c118e0f9e95ee..352dd847b65996aab4427d9643bbc6c0d2111314 100644 (file)
@@ -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();
   }
 }
index e9fd1eb5bb9dc7dbd8f160b77ba6efbe2d1732d8..f0b195e528833c1045edf417b9e81c9fd39662c2 100644 (file)
@@ -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());
   }
 }
index 74de66dae6b892253802e7981ff8d8e7704fe21c..85d0fb6a03f77c2c3b8ce2ead68b66813fada545 100644 (file)
@@ -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;
@@ -83,6 +82,21 @@ public class ComponentIndexerTest {
     assertThatIndexContainsOnly(project1, project2);
   }
 
+  @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();
@@ -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);
   }