Commit f95c84ae authored by Valery Sizov's avatar Valery Sizov

ES: tests fix, refactoring

parent 6040e1ba
......@@ -14,13 +14,13 @@ module ApplicationSearch
default_field: :name
},
analysis: {
:analyzer => {
:my_analyzer => {
analyzer: {
my_analyzer:{
type: "custom",
tokenizer: "ngram_tokenizer",
filter: %w(lowercase asciifolding name_ngrams)
},
:search_analyzer => {
search_analyzer: {
type: "custom",
tokenizer: "standard",
filter: %w(lowercase asciifolding)
......@@ -68,5 +68,32 @@ module ApplicationSearch
{ fields: es_fields }
end
def basic_query_hash(fields, query)
if query.present?
{
query: {
filtered: {
query: {
multi_match: {
fields: fields,
query: query,
operator: :and
}
},
},
}
}
else
query_hash = {
query: {
filtered: {
query: { match_all: {} }
}
},
track_scores: true
}
end
end
end
end
......@@ -35,24 +35,7 @@ module IssuesSearch
def self.elastic_search(query, options: {})
options[:in] = %w(title^2 description)
query_hash = {
query: {
filtered: {
query: {
multi_match: {
fields: options[:in],
query: "#{query}",
operator: :and
}
},
},
}
}
if query.blank?
query_hash[:query][:filtered][:query] = { match_all: {}}
query_hash[:track_scores] = true
end
query_hash = basic_query_hash(options[:in], query)
if options[:projects_ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
......
......@@ -41,35 +41,22 @@ module MergeRequestsSearch
def self.elastic_search(query, options: {})
options[:in] = %w(title^2 description)
query_hash = {
query: {
filtered: {
query: {
multi_match: {
fields: options[:in],
query: "#{query}",
operator: :and
}
},
},
}
}
if query.blank?
query_hash[:query][:filtered][:query] = { match_all: {}}
query_hash[:track_scores] = true
end
query_hash = basic_query_hash(options[:in], query)
if options[:projects_ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
or: [
{
terms: {
source_project_id: [options[:projects_ids]].flatten
}
},
{
terms: {
target_project_id: [options[:projects_ids]].flatten
}
}
]
}
end
......
......@@ -21,24 +21,7 @@ module MilestonesSearch
def self.elastic_search(query, options: {})
options[:in] = %w(title^2 description)
query_hash = {
query: {
filtered: {
query: {
multi_match: {
fields: options[:in],
query: "#{query}",
operator: :and
}
},
},
}
}
if query.blank?
query_hash[:query][:filtered][:query] = { match_all: {}}
query_hash[:track_scores] = true
end
query_hash = basic_query_hash(options[:in], query)
if options[:project_ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
......
......@@ -23,13 +23,13 @@ module NotesSearch
query_hash = {
query: {
filtered: {
query: {match: {note: query}},
query: { match: { note: query } },
},
}
}
if query.blank?
query_hash[:query][:filtered][:query] = { match_all: {}}
query_hash[:query][:filtered][:query] = { match_all: {} }
query_hash[:track_scores] = true
end
......
......@@ -32,28 +32,12 @@ module ProjectsSearch
def self.elastic_search(query, options: {})
options[:in] = %w(name^10 name_with_namespace^2 path_with_namespace path^9)
query_hash = {
query: {
filtered: {
query: {
multi_match: {
fields: options[:in],
query: "#{query}",
operator: :and
}
},
},
},
}
if query.blank?
query_hash[:query][:filtered][:query] = { match_all: {} }
query_hash[:track_scores] = true
end
query_hash = basic_query_hash(options[:in], query)
filters = []
if options[:abandoned]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
filters << {
range: {
last_pushed_at: {
lte: "now-6M/m"
......@@ -63,8 +47,7 @@ module ProjectsSearch
end
if options[:with_push]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
filters << {
not: {
missing: {
field: :last_pushed_at,
......@@ -76,8 +59,7 @@ module ProjectsSearch
end
if options[:namespace_id]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
filters << {
terms: {
namespace_id: [options[:namespace_id]].flatten
}
......@@ -85,8 +67,7 @@ module ProjectsSearch
end
if options[:non_archived]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
filters << {
terms: {
archived: [!options[:non_archived]].flatten
}
......@@ -94,8 +75,7 @@ module ProjectsSearch
end
if options[:visibility_levels]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
filters << {
terms: {
visibility_level: [options[:visibility_levels]].flatten
}
......@@ -103,8 +83,7 @@ module ProjectsSearch
end
if !options[:owner_id].blank?
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
filters << {
nested: {
path: :owner,
filter: {
......@@ -115,14 +94,15 @@ module ProjectsSearch
end
if options[:pids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
filters << {
ids: {
values: options[:pids]
}
}
end
query_hash[:query][:filtered][:filter] = { and: filters }
query_hash[:sort] = [:_score]
query_hash[:highlight] = highlight_options(options[:in])
......
......@@ -35,24 +35,7 @@ module SnippetsSearch
def self.elastic_search(query, options: {})
options[:in] = %w(title file_name)
query_hash = {
query: {
filtered: {
query: {
multi_match: {
fields: options[:in],
query: "#{query}",
operator: :and
}
},
},
}
}
if query.blank?
query_hash[:query][:filtered][:query] = { match_all: {}}
query_hash[:track_scores] = true
end
query_hash = basic_query_hash(options[:in], query)
if options[:ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
......@@ -79,7 +62,7 @@ module SnippetsSearch
query_hash = {
query: {
filtered: {
query: {match: {content: query}},
query: { match: { content: query } },
},
}
}
......
......@@ -30,27 +30,11 @@ module UsersSearch
def self.elastic_search(query, options: {})
options[:in] = %w(name^3 username^2 email)
query_hash = {
query: {
filtered: {
query: {
multi_match: {
fields: options[:in],
query: "#{query}",
operator: :and
}
},
},
}
}
query_hash = basic_query_hash(options[:in], query)
if query.blank?
query_hash[:query][:filtered][:query] = { match_all: {}}
query_hash[:track_scores] = true
end
query_hash[:query][:filtered][:filter] ||= { and: [] }
if options[:uids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
ids: {
values: options[:uids]
......@@ -59,7 +43,6 @@ module UsersSearch
end
if options[:active]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
terms: {
state: ["active"]
......
......@@ -60,7 +60,7 @@ module Gitlab
project.repository.search(
query,
type: :blob,
options: {highlight: true}
options: { highlight: true }
)[:blobs][:results].response
else
Kaminari.paginate_array(
......@@ -75,7 +75,7 @@ module Gitlab
project.wiki.search(
query,
type: :blob,
options: {highlight: true}
options: { highlight: true }
)[:blobs][:results].response
else
Kaminari.paginate_array([])
......
......@@ -67,9 +67,9 @@ module Gitlab
}
if query =~ /#(\d+)\z/
issues = Issue.where(project_id: limit_project_ids).where(iid: $1)
Issue.where(project_id: limit_project_ids).where(iid: $1)
else
issues = Issue.elastic_search(query, options: opt)
Issue.elastic_search(query, options: opt)
end
end
......@@ -78,7 +78,7 @@ module Gitlab
projects_ids: limit_project_ids
}
milestones = Milestone.elastic_search(query, options: opt)
Milestone.elastic_search(query, options: opt)
end
def merge_requests
......@@ -87,9 +87,9 @@ module Gitlab
}
if query =~ /[#!](\d+)\z/
merge_requests = MergeRequest.in_projects(limit_project_ids).where(iid: $1)
MergeRequest.in_projects(limit_project_ids).where(iid: $1)
else
merge_requests = MergeRequest.elastic_search(query, options: opt)
MergeRequest.elastic_search(query, options: opt)
end
end
......
......@@ -16,8 +16,8 @@ module Gitlab
# We process whole list of items then paginate it. Not too smart
# Should be refactored in the CE side first to prevent conflicts hell
Kaminari.paginate_array(
snippet_blobs.records.map do
|snippet| chunk_snippet(snippet)
snippet_blobs.records.map do |snippet|
chunk_snippet(snippet)
end
).page(page).per(per_page)
else
......
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