diff --git a/ee/elastic/docs/20240208160152_add_count_fields_to_projects.yml b/ee/elastic/docs/20240208160152_add_count_fields_to_projects.yml new file mode 100644 index 0000000000000000000000000000000000000000..3c224d5b6cd1605f5eb03ec21a664110fb0274bb --- /dev/null +++ b/ee/elastic/docs/20240208160152_add_count_fields_to_projects.yml @@ -0,0 +1,10 @@ +--- +name: AddCountFieldsToProjects +version: '20240208160152' +description: Add star_count and last_repository_updated_date to projects index. +group: group::global search +milestone: '16.10' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/144557 +obsolete: false +marked_obsolete_by_url: +marked_obsolete_in_milestone: diff --git a/ee/elastic/migrate/20240208160152_add_count_fields_to_projects.rb b/ee/elastic/migrate/20240208160152_add_count_fields_to_projects.rb new file mode 100644 index 0000000000000000000000000000000000000000..87aaf685335782839eaf7bd5cb80001a5e0e2305 --- /dev/null +++ b/ee/elastic/migrate/20240208160152_add_count_fields_to_projects.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class AddCountFieldsToProjects < Elastic::Migration + include Elastic::MigrationUpdateMappingsHelper + + DOCUMENT_TYPE = Project + + private + + def new_mappings + { + star_count: { + type: 'integer' + }, + last_repository_updated_date: { + type: 'date' + } + } + end +end diff --git a/ee/lib/elastic/latest/project_config.rb b/ee/lib/elastic/latest/project_config.rb index a7e0a4072ea7dd665a08f8cdad6aec0cc2c90797..57ae8674c3a097c9990b651d7cc2e52add204418 100644 --- a/ee/lib/elastic/latest/project_config.rb +++ b/ee/lib/elastic/latest/project_config.rb @@ -42,6 +42,9 @@ module ProjectConfig indexes :forked, type: :boolean indexes :owner_id, type: :integer indexes :repository_languages, type: :keyword + + indexes :star_count, type: :integer + indexes :last_repository_updated_date, type: :date end end end diff --git a/ee/lib/elastic/latest/project_instance_proxy.rb b/ee/lib/elastic/latest/project_instance_proxy.rb index e67f066012f7316385758ff8677a7d7f75cac3c4..78102fef3f7b6c6ee53961a4e135b3830e9c6eed 100644 --- a/ee/lib/elastic/latest/project_instance_proxy.rb +++ b/ee/lib/elastic/latest/project_instance_proxy.rb @@ -55,6 +55,8 @@ def as_indexed_json(_options = {}) data['repository_languages'] = target.repository_languages.map(&:name) end + data.merge!(add_count_fields(target)) + unless ::Elastic::DataMigrationService.migration_has_finished?(:migrate_projects_to_separate_index) # Set it as a parent in our `project => child` JOIN field data['join_field'] = es_type @@ -76,6 +78,18 @@ def es_parent "n_#{target.root_ancestor.id}" end + + private + + def add_count_fields(target) + data = {} + if ::Elastic::DataMigrationService.migration_has_finished?(:add_count_fields_to_projects) + data['star_count'] = target.star_count + data['last_repository_updated_date'] = target.last_repository_updated_at + end + + data + end end end end diff --git a/ee/spec/elastic/migrate/20240208160152_add_count_fields_to_projects_spec.rb b/ee/spec/elastic/migrate/20240208160152_add_count_fields_to_projects_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..b46ad8ece641bf6e86ccd7136dddcb9df6e13a3a --- /dev/null +++ b/ee/spec/elastic/migrate/20240208160152_add_count_fields_to_projects_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative 'migration_shared_examples' +require File.expand_path('ee/elastic/migrate/20240208160152_add_count_fields_to_projects.rb') + +RSpec.describe AddCountFieldsToProjects, :elastic, feature_category: :global_search do + let(:version) { 20240208160152 } + + include_examples 'migration adds mapping' +end diff --git a/ee/spec/lib/elastic/latest/project_instance_proxy_spec.rb b/ee/spec/lib/elastic/latest/project_instance_proxy_spec.rb index cddfadf0f34654b11f70218dfcfe055afeaf82ed..241bbc7ad2be2ec46f6c6bf1c07a66c18011f128 100644 --- a/ee/spec/lib/elastic/latest/project_instance_proxy_spec.rb +++ b/ee/spec/lib/elastic/latest/project_instance_proxy_spec.rb @@ -122,6 +122,45 @@ repository_languages: project.repository_languages.map(&:name) ) end + end + end + + describe 'when add_count_fields_to_projects migration is completed' do + before do + stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true) + set_elasticsearch_migration_to(:add_count_fields_to_projects, including: true) + ensure_elasticsearch_index! # ensure objects are indexed + end + + describe '#as_indexed_json' do + it 'serializes project as hash' do + result = proxy.as_indexed_json.with_indifferent_access + + expect(result).to include( + id: project.id, + name: project.name, + path: project.path, + description: project.description, + namespace_id: project.namespace_id, + created_at: project.created_at, + updated_at: project.updated_at, + archived: project.archived, + last_activity_at: project.last_activity_at, + name_with_namespace: project.name_with_namespace, + path_with_namespace: project.path_with_namespace, + traversal_ids: project.elastic_namespace_ancestry, + type: 'project', + visibility_level: project.visibility_level, + schema_version: schema_version, + ci_catalog: project.catalog_resource.present?, + mirror: project.mirror?, + forked: project.forked? || false, + owner_id: project.owner.id, + repository_languages: project.repository_languages.map(&:name), + star_count: project.star_count, + last_repository_updated_date: project.last_repository_updated_at + ) + end it 'contains the expected mappings' do result = proxy.as_indexed_json.with_indifferent_access.keys