Commit 9d57a69c authored by Alan (Maciej) Paruszewski's avatar Alan (Maciej) Paruszewski Committed by Tiger Watson

Add fuzzy-search on full path in Groups API

This change adds new column to fuzzy-search in Groups API to filter on
full path.
parent cf605972
......@@ -117,8 +117,12 @@ class Namespace < ApplicationRecord
# query - The search query as a String.
#
# Returns an ActiveRecord::Relation.
def search(query)
fuzzy_search(query, [:name, :path])
def search(query, include_parents: false)
if include_parents
where(id: Route.fuzzy_search(query, [Route.arel_table[:path], Route.arel_table[:name]]).select(:source_id))
else
fuzzy_search(query, [:path, :name])
end
end
def clean_path(path)
......
---
title: Add fuzzy-search on full path in Groups API
merge_request: 45729
author:
type: changed
# frozen_string_literal: true
class AddIndexRouteOnNameTrigramToRoute < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_route_on_name_trigram'
disable_ddl_transaction!
def up
add_concurrent_index :routes, :name, name: INDEX_NAME, using: :gin, opclass: { name: :gin_trgm_ops }
end
def down
remove_concurrent_index_by_name(:routes, INDEX_NAME)
end
end
47158d21bd1a800e5a9da1bfea25870f14cc0b094e5f3e9a4b7608b8a9eca180
\ No newline at end of file
......@@ -21554,6 +21554,8 @@ CREATE INDEX index_reviews_on_merge_request_id ON reviews USING btree (merge_req
CREATE INDEX index_reviews_on_project_id ON reviews USING btree (project_id);
CREATE INDEX index_route_on_name_trigram ON routes USING gin (name gin_trgm_ops);
CREATE UNIQUE INDEX index_routes_on_path ON routes USING btree (path);
CREATE INDEX index_routes_on_path_text_pattern_ops ON routes USING btree (path varchar_pattern_ops);
......
......@@ -46,7 +46,7 @@ module API
find_params.fetch(:all_available, current_user&.can_read_all_resources?)
groups = GroupsFinder.new(current_user, find_params).execute
groups = groups.search(params[:search]) if params[:search].present?
groups = groups.search(params[:search], include_parents: true) if params[:search].present?
groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
order_options = { params[:order_by] => params[:sort] }
order_options["id"] ||= "asc"
......
......@@ -147,7 +147,7 @@ RSpec.describe Namespace do
end
describe '.search' do
let(:namespace) { create(:namespace) }
let_it_be(:namespace) { create(:namespace) }
it 'returns namespaces with a matching name' do
expect(described_class.search(namespace.name)).to eq([namespace])
......@@ -172,6 +172,18 @@ RSpec.describe Namespace do
it 'returns namespaces with a matching path regardless of the casing' do
expect(described_class.search(namespace.path.upcase)).to eq([namespace])
end
it 'returns namespaces with a matching route path' do
expect(described_class.search(namespace.route.path, include_parents: true)).to eq([namespace])
end
it 'returns namespaces with a partially matching route path' do
expect(described_class.search(namespace.route.path[0..2], include_parents: true)).to eq([namespace])
end
it 'returns namespaces with a matching route path regardless of the casing' do
expect(described_class.search(namespace.route.path.upcase, include_parents: true)).to eq([namespace])
end
end
describe '.with_statistics' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment