Commit f95c84ae authored by Valery Sizov's avatar Valery Sizov

ES: tests fix, refactoring

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