Commit c9d4a405 authored by Terri Chu's avatar Terri Chu

Merge branch 'remove-type-from-es-query' into 'master'

Remove type filter from issues/mr/notes ES queries

See merge request gitlab-org/gitlab!73072
parents f1187d18 656454bd
...@@ -65,10 +65,10 @@ module Elastic ...@@ -65,10 +65,10 @@ module Elastic
} }
end end
def basic_query_hash(fields, query, count_only: false) def basic_query_hash(fields, query, options = {})
fields = CustomLanguageAnalyzers.add_custom_analyzers_fields(fields) fields = CustomLanguageAnalyzers.add_custom_analyzers_fields(fields)
fields = remove_fields_boost(fields) if count_only fields = remove_fields_boost(fields) if options[:count_only]
query_hash = query_hash =
if query.present? if query.present?
...@@ -82,31 +82,7 @@ module Elastic ...@@ -82,31 +82,7 @@ module Elastic
} }
} }
must = [] build_query_filter(simple_query_string, options)
filter = [{
term: {
type: {
_name: context.name(:doc, :is_a, self.es_type),
value: self.es_type
}
}
}]
if count_only
filter << simple_query_string
else
must << simple_query_string
end
{
query: {
bool: {
must: must,
filter: filter
}
}
}
else else
{ {
query: { query: {
...@@ -118,7 +94,7 @@ module Elastic ...@@ -118,7 +94,7 @@ module Elastic
} }
end end
if count_only if options[:count_only]
query_hash[:size] = 0 query_hash[:size] = 0
else else
query_hash[:highlight] = highlight_options(fields) query_hash[:highlight] = highlight_options(fields)
...@@ -127,6 +103,40 @@ module Elastic ...@@ -127,6 +103,40 @@ module Elastic
query_hash query_hash
end end
def build_query_filter(simple_query_string, options)
must = []
filter = if options[:no_join_project]
[]
else
[
{
term: {
type: {
_name: context.name(:doc, :is_a, self.es_type),
value: self.es_type
}
}
}
]
end
if options[:count_only]
filter << simple_query_string
else
must << simple_query_string
end
{
query: {
bool: {
must: must,
filter: filter
}
}
}
end
def iid_query_hash(iid) def iid_query_hash(iid)
{ {
query: { query: {
......
...@@ -6,6 +6,9 @@ module Elastic ...@@ -6,6 +6,9 @@ module Elastic
include StateFilter include StateFilter
def elastic_search(query, options: {}) def elastic_search(query, options: {})
options[:features] = 'issues'
options[:no_join_project] = true
query_hash = query_hash =
if query =~ /#(\d+)\z/ if query =~ /#(\d+)\z/
iid_query_hash(Regexp.last_match(1)) iid_query_hash(Regexp.last_match(1))
...@@ -13,11 +16,9 @@ module Elastic ...@@ -13,11 +16,9 @@ module Elastic
# iid field can be added here as lenient option will # iid field can be added here as lenient option will
# pardon format errors, like integer out of range. # pardon format errors, like integer out of range.
fields = %w[iid^3 title^2 description] fields = %w[iid^3 title^2 description]
basic_query_hash(fields, query, count_only: options[:count_only]) basic_query_hash(fields, query, options)
end end
options[:features] = 'issues'
options[:no_join_project] = true
context.name(:issue) do context.name(:issue) do
query_hash = context.name(:authorized) { authorization_filter(query_hash, options) } query_hash = context.name(:authorized) { authorization_filter(query_hash, options) }
query_hash = context.name(:confidentiality) { confidentiality_filter(query_hash, options) } query_hash = context.name(:confidentiality) { confidentiality_filter(query_hash, options) }
......
...@@ -6,6 +6,9 @@ module Elastic ...@@ -6,6 +6,9 @@ module Elastic
include StateFilter include StateFilter
def elastic_search(query, options: {}) def elastic_search(query, options: {})
options[:features] = 'merge_requests'
options[:no_join_project] = Elastic::DataMigrationService.migration_has_finished?(:add_new_data_to_merge_requests_documents)
query_hash = query_hash =
if query =~ /\!(\d+)\z/ if query =~ /\!(\d+)\z/
iid_query_hash(Regexp.last_match(1)) iid_query_hash(Regexp.last_match(1))
...@@ -14,11 +17,9 @@ module Elastic ...@@ -14,11 +17,9 @@ module Elastic
# pardon format errors, like integer out of range. # pardon format errors, like integer out of range.
fields = %w[iid^3 title^2 description] fields = %w[iid^3 title^2 description]
basic_query_hash(fields, query, count_only: options[:count_only]) basic_query_hash(fields, query, options)
end end
options[:features] = 'merge_requests'
options[:no_join_project] = Elastic::DataMigrationService.migration_has_finished?(:add_new_data_to_merge_requests_documents)
context.name(:merge_request) do context.name(:merge_request) do
query_hash = context.name(:authorized) { project_ids_filter(query_hash, options) } query_hash = context.name(:authorized) { project_ids_filter(query_hash, options) }
query_hash = context.name(:match) { state_filter(query_hash, options) } query_hash = context.name(:match) { state_filter(query_hash, options) }
......
...@@ -6,7 +6,7 @@ module Elastic ...@@ -6,7 +6,7 @@ module Elastic
def elastic_search(query, options: {}) def elastic_search(query, options: {})
options[:in] = %w[title^2 description] options[:in] = %w[title^2 description]
query_hash = basic_query_hash(options[:in], query, count_only: options[:count_only]) query_hash = basic_query_hash(options[:in], query, options)
query_hash = context.name(:milestone, :related) { project_ids_filter(query_hash, options) } query_hash = context.name(:milestone, :related) { project_ids_filter(query_hash, options) }
search(query_hash, options) search(query_hash, options)
......
...@@ -11,9 +11,10 @@ module Elastic ...@@ -11,9 +11,10 @@ module Elastic
def elastic_search(query, options: {}) def elastic_search(query, options: {})
options[:in] = ['note'] options[:in] = ['note']
query_hash = basic_query_hash(%w[note], query, count_only: options[:count_only])
options[:no_join_project] = true options[:no_join_project] = true
query_hash = basic_query_hash(%w[note], query, options)
context.name(:note) do context.name(:note) do
query_hash = context.name(:authorized) { project_ids_filter(query_hash, options) } query_hash = context.name(:authorized) { project_ids_filter(query_hash, options) }
query_hash = context.name(:confidentiality) { confidentiality_filter(query_hash, options) } query_hash = context.name(:confidentiality) { confidentiality_filter(query_hash, options) }
......
...@@ -6,7 +6,7 @@ module Elastic ...@@ -6,7 +6,7 @@ module Elastic
def elastic_search(query, options: {}) def elastic_search(query, options: {})
options[:in] = %w[name^10 name_with_namespace^2 path_with_namespace path^9 description] options[:in] = %w[name^10 name_with_namespace^2 path_with_namespace path^9 description]
query_hash = basic_query_hash(options[:in], query, count_only: options[:count_only]) query_hash = basic_query_hash(options[:in], query, options)
filters = [{ terms: { _name: context.name(:doc, :is_a, es_type), type: [es_type] } }] filters = [{ terms: { _name: context.name(:doc, :is_a, es_type), type: [es_type] } }]
......
...@@ -78,8 +78,7 @@ RSpec.describe Issue, :elastic do ...@@ -78,8 +78,7 @@ RSpec.describe Issue, :elastic do
it "names elasticsearch queries" do it "names elasticsearch queries" do
described_class.elastic_search('*').total_count described_class.elastic_search('*').total_count
assert_named_queries('doc:is_a:issue', assert_named_queries('issue:match:search_terms',
'issue:match:search_terms',
'issue:authorized:project') 'issue:authorized:project')
end end
......
...@@ -43,8 +43,7 @@ RSpec.describe MergeRequest, :elastic do ...@@ -43,8 +43,7 @@ RSpec.describe MergeRequest, :elastic do
it "names elasticsearch queries" do it "names elasticsearch queries" do
described_class.elastic_search('*').total_count described_class.elastic_search('*').total_count
assert_named_queries('doc:is_a:merge_request', assert_named_queries('merge_request:match:search_terms',
'merge_request:match:search_terms',
'merge_request:authorized:project') 'merge_request:authorized:project')
end end
......
...@@ -58,8 +58,7 @@ RSpec.describe Note, :elastic, :clean_gitlab_redis_shared_state do ...@@ -58,8 +58,7 @@ RSpec.describe Note, :elastic, :clean_gitlab_redis_shared_state do
it "names elasticsearch queries" do it "names elasticsearch queries" do
described_class.elastic_search('*').total_count described_class.elastic_search('*').total_count
assert_named_queries("doc:is_a:note", assert_named_queries("note:match:search_terms",
"note:match:search_terms",
"note:authorized") "note:authorized")
end end
......
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