Commit 201c1277 authored by Kerri Miller's avatar Kerri Miller

Merge branch '349099-use-reverse-filename-index-for-extension-filter' into 'master'

Advanced Search: Use reverse filename index

See merge request gitlab-org/gitlab!77226
parents 8bade920 30ae308b
---
name: elastic_file_name_reverse_optimization
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/77226
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/349122
milestone: '14.7'
type: development
group: group::global search
default_enabled: false
......@@ -59,6 +59,10 @@ module Elastic
type: 'custom',
tokenizer: 'whitespace',
filter: %w(lowercase asciifolding)
},
whitespace_reverse: {
tokenizer: 'whitespace',
filter: %w(lowercase asciifolding reverse)
}
},
filter: {
......@@ -207,7 +211,8 @@ module Elastic
analyzer: :path_analyzer
indexes :file_name, type: :text,
analyzer: :code_analyzer,
search_analyzer: :code_search_analyzer
search_analyzer: :code_search_analyzer,
fields: { reverse: { type: :text, analyzer: :whitespace_reverse } }
indexes :content, type: :text,
index_options: 'positions',
analyzer: :code_analyzer,
......
......@@ -148,7 +148,13 @@ module Elastic
query = ::Gitlab::Search::Query.new(query) do
filter :filename, field: :file_name
filter :path, parser: ->(input) { "*#{input.downcase}*" }
if Feature.enabled?(:elastic_file_name_reverse_optimization, default_enabled: :yaml)
filter :extension, field: 'file_name.reverse', type: :prefix, parser: ->(input) { input.downcase.reverse + '.' }
else
filter :extension, field: :path, parser: ->(input) { '*.' + input.downcase }
end
filter :blob, field: :oid
end
......
......@@ -46,10 +46,14 @@ RSpec.describe Repository, :elastic do
'commit:match:search_terms')
end
it 'can filter blobs' do
project = create :project, :repository
context 'filtering' do
let(:project) { create :project, :repository }
before do
index!(project)
end
it 'can filter blobs' do
# Finds custom-highlighting/test.gitlab-custom
expect(project.repository.elastic_search('def | popen filename:test')[:blobs][:total_count]).to eq(1)
......@@ -84,6 +88,19 @@ RSpec.describe Repository, :elastic do
expect(project.repository.elastic_search('blob:7e3e39ebb9b2bf433b4ad17313770fbe4051649c')[:blobs][:total_count]).to eq(1)
end
it 'filters by extension when optimization is disabled' do
stub_feature_flags(elastic_file_name_reverse_optimization: false)
# Finds files/markdown/ruby-style-guide.md
expect(project.repository.elastic_search('def | popen extension:md')[:blobs][:total_count]).to eq(1)
# extension filter without search term
count = project.repository.ls_files('master').count { |path| path.split('/')[-1].split('.')[-1].include?('md') }
expect(project.repository.elastic_search('extension:md')[:blobs][:total_count]).to eq(count)
expect(project.repository.elastic_search('extension:md')[:blobs][:total_count]).to be > 0
end
end
def search_and_check!(on, query, type:, per: 1000)
results = on.elastic_search(query, type: type, per: per)["#{type}s".to_sym][:results]
......
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