From bbea92754dcb6f644da0b661c7770ee4c6fcfeaa Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Wed, 11 Oct 2017 17:27:09 +0200 Subject: SONAR-9928 Index and filter organization in component ES index --- .../org/sonar/server/component/index/ComponentDoc.java | 10 ++++++++++ .../org/sonar/server/component/index/ComponentIndex.java | 4 +++- .../server/component/index/ComponentIndexDefinition.java | 2 ++ .../sonar/server/component/index/ComponentIndexer.java | 1 + .../org/sonar/server/component/index/ComponentQuery.java | 16 ++++++++++++++++ .../server/component/index/ComponentIndexSearchTest.java | 14 ++++++++++++++ .../server/component/index/ComponentIndexerTest.java | 5 ++++- 7 files changed, 50 insertions(+), 2 deletions(-) (limited to 'server') 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 eafeece2962..3a8c1fd3132 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 @@ -28,6 +28,7 @@ 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_ORGANIZATION_UUID; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_PROJECT_UUID; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_QUALIFIER; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_UUID; @@ -107,4 +108,13 @@ public class ComponentDoc extends BaseDoc { setField(FIELD_LANGUAGE, s); return this; } + + public String getOrganization() { + return getField(FIELD_ORGANIZATION_UUID); + } + + public ComponentDoc setOrganization(String s) { + setField(FIELD_ORGANIZATION_UUID, s); + return this; + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java index 489699055c5..8a492913ef4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java @@ -58,6 +58,7 @@ import static org.elasticsearch.index.query.QueryBuilders.termsQuery; 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_ORGANIZATION_UUID; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_QUALIFIER; import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_TYPE_COMPONENT; import static org.sonar.server.component.index.ComponentIndexDefinition.NAME_ANALYZERS; @@ -94,7 +95,8 @@ public class ComponentIndex { esQuery.must(ComponentTextSearchQueryFactory.createQuery(componentTextSearchQuery, ComponentTextSearchFeatureRepertoire.values())); }); setEmptiable(query.getQualifiers(), q -> esQuery.must(termsQuery(FIELD_QUALIFIER, q))); - setNullable(query.getLanguage(), l -> esQuery.must(termsQuery(FIELD_LANGUAGE, l))); + setNullable(query.getLanguage(), l -> esQuery.must(termQuery(FIELD_LANGUAGE, l))); + setNullable(query.getOrganizationUuid(), o -> esQuery.must(termQuery(FIELD_ORGANIZATION_UUID, o))); requestBuilder.setQuery(esQuery); requestBuilder.addSort(SORTABLE_ANALYZER.subField(FIELD_NAME), SortOrder.ASC); 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 352dd847b65..923cf6746a2 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 @@ -37,6 +37,7 @@ public class ComponentIndexDefinition implements IndexDefinition { public static final IndexType INDEX_TYPE_COMPONENT = new IndexType("components", "component"); public static final String FIELD_UUID = "uuid"; public static final String FIELD_PROJECT_UUID = "project_uuid"; + public static final String FIELD_ORGANIZATION_UUID = "organization_uuid"; public static final String FIELD_KEY = "key"; public static final String FIELD_NAME = "name"; public static final String FIELD_QUALIFIER = "qualifier"; @@ -75,5 +76,6 @@ public class ComponentIndexDefinition implements IndexDefinition { mapping.keywordFieldBuilder(FIELD_QUALIFIER).build(); mapping.keywordFieldBuilder(FIELD_LANGUAGE).disableNorms().build(); + mapping.keywordFieldBuilder(FIELD_ORGANIZATION_UUID).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 f0b195e5288..2063fb35e0f 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()) + .setOrganization(component.getOrganizationUuid()) .setLanguage(component.language()) .setQualifier(component.qualifier()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java index 10a5bbb8c86..206447f5563 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentQuery.java @@ -21,21 +21,30 @@ package org.sonar.server.component.index; import java.util.Collection; +import javax.annotation.CheckForNull; import static java.util.Collections.emptySet; import static java.util.Collections.unmodifiableCollection; public class ComponentQuery { + private final String organizationUuid; private final String query; private final Collection qualifiers; private final String language; private ComponentQuery(Builder builder) { + this.organizationUuid = builder.organizationUuid; this.query = builder.query; this.qualifiers = builder.qualifiers; this.language = builder.language; } + @CheckForNull + public String getOrganizationUuid() { + return organizationUuid; + } + + @CheckForNull public String getQuery() { return query; } @@ -44,6 +53,7 @@ public class ComponentQuery { return qualifiers; } + @CheckForNull public String getLanguage() { return language; } @@ -53,6 +63,7 @@ public class ComponentQuery { } public static class Builder { + private String organizationUuid; private String query; private Collection qualifiers = emptySet(); private String language; @@ -61,6 +72,11 @@ public class ComponentQuery { // enforce static factory method } + public Builder setOrganization(String organizationUuid) { + this.organizationUuid = organizationUuid; + return this; + } + public Builder setQuery(String query) { this.query = query; return this; diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java index 52e3b3e981d..99d7d9583f7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexSearchTest.java @@ -31,6 +31,7 @@ import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.organization.OrganizationDto; import org.sonar.server.es.EsTester; import org.sonar.server.es.SearchIdResult; import org.sonar.server.es.SearchOptions; @@ -106,6 +107,19 @@ public class ComponentIndexSearchTest { assertThat(result.getIds()).containsExactlyInAnyOrder(file.uuid()); } + @Test + public void filter_by_organization() { + OrganizationDto organization = db.organizations().insert(); + OrganizationDto anotherOrganization = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organization); + ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization); + index(project, anotherProject); + + SearchIdResult result = underTest.search(ComponentQuery.builder().setOrganization(organization.getUuid()).build(), new SearchOptions()); + + assertThat(result.getIds()).containsExactlyInAnyOrder(project.uuid()); + } + @Test public void order_by_name_case_insensitive() { ComponentDto project2 = db.components().insertPrivateProject(p -> p.setName("PROJECT 2")); 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 85d0fb6a03f..b2bac18453d 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 @@ -32,6 +32,7 @@ import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentUpdateDto; import org.sonar.db.es.EsQueueDto; +import org.sonar.db.organization.OrganizationDto; import org.sonar.server.es.EsTester; import org.sonar.server.es.IndexingResult; import org.sonar.server.es.ProjectIndexer; @@ -84,7 +85,8 @@ public class ComponentIndexerTest { @Test public void map_fields() { - ComponentDto project = db.components().insertPrivateProject(p -> p.setLanguage("java")); + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organization, p -> p.setLanguage("java")); underTest.indexOnStartup(emptySet()); @@ -95,6 +97,7 @@ public class ComponentIndexerTest { assertThat(doc.getProjectUuid()).isEqualTo(project.projectUuid()); assertThat(doc.getName()).isEqualTo(project.name()); assertThat(doc.getLanguage()).isEqualTo(project.language()); + assertThat(doc.getOrganization()).isEqualTo(project.getOrganizationUuid()); } @Test -- cgit v1.2.3