高级搜索开发技巧
Kibana
使用Kibana与您的Elasticsearch集群交互。
查看下载说明。
查看索引状态
运行
bundle exec rake gitlab:elastic:info以查看集群的状态和信息。
从零开始创建所有索引并填充本地数据
选项1:Rake任务
运行
bundle exec rake gitlab:elastic:index这将触发Search::Elastic::TriggerIndexingWorker异步运行。
运行
Elastic::ProcessInitialBookkeepingService.new.execute直到它显示[0, 0],表示队列中没有更多引用(refs)。
选项2:手动操作
手动执行Search::Elastic::TriggerIndexingWorker中的步骤。
有时Sidekiq无法正确拾取作业,因此您可能需要重启Sidekiq,或者如果您更喜欢在Rails控制台中运行这些步骤:
task_executor_service = Search::RakeTaskExecutorService.new(logger: ::Gitlab::Elasticsearch::Logger.build)
task_executor_service.execute(:recreate_index)
task_executor_service.execute(:clear_index_status)
task_executor_service.execute(:clear_reindex_status)
task_executor_service.execute(:resume_indexing)
task_executor_service.execute(:index_namespaces)
task_executor_service.execute(:index_projects)
task_executor_service.execute(:index_snippets)
task_executor_service.execute(:index_users)运行
Elastic::ProcessInitialBookkeepingService.new.execute直到它显示[0, 0],表示队列中没有更多引用(refs)。
选项3:重新索引任务
首先删除现有索引,然后为目标索引创建一个ReindexingTask。这会基于当前配置创建新索引,然后将数据复制过去。
Search::Elastic::ReindexingTask.create!(targets: %w[MergeRequest])运行
ElasticClusterReindexingCronWorker.new.perform重复此操作,直到
Search::Elastic::ReindexingTask.last.state为success。
索引数据
要添加并索引数据库记录,调用track!方法并执行簿记器:
Elastic::ProcessBookkeepingService.track!(MergeRequest.first)
Elastic::ProcessBookkeepingService.track!(*MergeRequest.all)
Elastic::ProcessBookkeepingService.new.execute依赖关联索引更新
您可以使用elastic_index_dependant_association自动更新索引中依赖关联的记录,当特定字段更改时。例如,当项目的visibility_level更改时,重新索引所有工作项:
elastic_index_dependant_association :work_items, on_change: :visibility_level, depends_on_finished_migration: :add_mapping_migrationdepends_on_finished_migration参数是可选的,确保仅在指定的高级搜索迁移完成后才进行更新(例如添加必要字段的映射迁移)。
测试
Elasticsearch测试不会在每个合并请求上运行。向合并请求添加~pipeline:run-search-tests或~group::global search标签,以使用生产版本的Elasticsearch和PostgreSQL运行测试。
高级搜索迁移
测试更改索引映射的迁移
- 确保索引尚未应用这些更改。记住迁移定时任务在后台运行,因此该迁移可能已经应用。
-
可选。在GitLab 18.0及更高版本中, 要禁用迁移worker,运行以下命令:
settings = ApplicationSetting.last # 确保此设置不会返回`nil` settings.elastic_migration_worker_enabled = false settings.save! -
检查迁移是否待处理:
::Elastic::DataMigrationService.pending_migrations。 -
确认迁移未完成:
Elastic::DataMigrationService.pending_migrations.first.completed?。 -
确保映射尚未应用
- 可以通过在Kibana中检查:
GET gitlab-development-some-index/_mapping - 或发送curl请求:
curl "http://localhost:9200/gitlab-development-some-index/_mappings" | jq
- 可以通过在Kibana中检查:
-
- 跟踪日志以查看记录的消息:
tail -f log/elasticsearch.log。 - 通过以下方式之一执行迁移:
- 运行
Elastic::MigrationWorker.new.perform迁移worker。 在GitLab 18.0及更高版本中,必须启用elastic_migration_worker_enabled应用程序设置。 - 使用待处理的迁移:
::Elastic::DataMigrationService.pending_migrations.first.migrate。 - 使用版本:
Elastic::DataMigrationService[20250220214819].migrate,将版本替换为迁移版本。
- 运行
- 查看迁移状态。
- 在Kibana中查看迁移记录:
GET gitlab-development-migrations/_doc/20250220214819(更改版本)。这包含诸如开始时间以及状态等详细信息。 - 检查Kibana中映射是否已更改:
GET gitlab-development-some-index/_mapping。
- 在Kibana中查看迁移记录: