import org.sonar.server.es.BulkIndexer;
import org.sonar.server.es.EsClient;
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
+import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_PROJECT_UUID;
import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_COMPONENTS;
import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT;
/**
* Copy all components of all projects to the elastic search index.
+ * <p>
+ * <b>Warning</b>: This should only be called on an empty index. It does not purge anything.
*/
public void index() {
try (DbSession dbSession = dbClient.openSession(false)) {
*/
public void index(String projectUuid) {
try (DbSession dbSession = dbClient.openSession(false)) {
+ purge(projectUuid);
index(
dbClient
.componentDao()
}
}
- public void index(ComponentDto... docs) {
+ private void purge(String projectUuid) {
+ BulkIndexer.delete(esClient, INDEX_COMPONENTS, esClient
+ .prepareSearch(INDEX_COMPONENTS)
+ .setTypes(TYPE_COMPONENT)
+ .setFetchSource(false)
+ .setQuery(termQuery(FIELD_PROJECT_UUID, projectUuid)));
+ }
+
+ void index(ComponentDto... docs) {
Future<?> submit = executor.submit(() -> indexNow(docs));
try {
Uninterruptibles.getUninterruptibly(submit);
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.component.ComponentUpdateDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.organization.OrganizationTesting;
import org.sonar.server.es.EsTester;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
+import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME;
import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_COMPONENTS;
import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT;
assertThat(count()).isEqualTo(2);
}
+ @Test
+ public void reindex_project() {
+
+ // insert
+ ComponentDto component = ComponentTesting.newProjectDto(organization, "UUID-1").setName("OldName");
+ insert(component);
+
+ // verify insert
+ index(component);
+ assertMatches("OldName", 1);
+
+ // modify
+ component.setName("NewName");
+ update(component);
+
+ // verify modification
+ index(component);
+ assertMatches("OldName", 0);
+ assertMatches("NewName", 1);
+ }
+
private void insert(ComponentDto component) {
dbClient.componentDao().insert(dbSession, component);
dbSession.commit();
}
+ private void update(ComponentDto component) {
+ ComponentUpdateDto updateComponent = ComponentUpdateDto.copyFrom(component);
+ updateComponent.setBChanged(true);
+ dbClient.componentDao().update(dbSession, updateComponent);
+ dbClient.componentDao().applyBChangesForRootComponentUuid(dbSession, "UUID-1");
+ dbSession.commit();
+ }
+
private void index() {
createIndexer().index();
}
return esTester.countDocuments(INDEX_COMPONENTS, TYPE_COMPONENT);
}
+ private void assertMatches(String nameQuery, int numberOfMatches) {
+ assertThat(
+ esTester.client()
+ .prepareSearch(INDEX_COMPONENTS)
+ .setTypes(TYPE_COMPONENT)
+ .setQuery(termQuery(FIELD_NAME, nameQuery))
+ .get()
+ .getHits()
+ .getHits().length).isEqualTo(numberOfMatches);
+ }
+
private ComponentIndexer createIndexer() {
return new ComponentIndexer(dbTester.getDbClient(), esTester.client());
}