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;
super(new HashMap<>(6));
}
+ public ComponentDoc(Map<String, Object> fields) {
+ super(fields);
+ }
+
@Override
public String getId() {
return getField(FIELD_UUID);
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;
+ }
}
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;
.build();
mapping.keywordFieldBuilder(FIELD_QUALIFIER).build();
+ mapping.keywordFieldBuilder(FIELD_LANGUAGE).disableNorms().build();
}
}
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;
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();
return underTest.index(db.getSession(), items);
}
-
private void assertThatIndexHasSize(int expectedSize) {
assertThat(es.countDocuments(INDEX_TYPE_COMPONENT)).isEqualTo(expectedSize);
}