Commit 5c6312f3 authored by Valery Sizov's avatar Valery Sizov

ES: one more refactoring

parent 15566190
...@@ -70,7 +70,7 @@ module ApplicationSearch ...@@ -70,7 +70,7 @@ module ApplicationSearch
end end
def basic_query_hash(fields, query) def basic_query_hash(fields, query)
if query.present? query_hash = if query.present?
{ {
query: { query: {
filtered: { filtered: {
...@@ -94,6 +94,25 @@ module ApplicationSearch ...@@ -94,6 +94,25 @@ module ApplicationSearch
track_scores: true track_scores: true
} }
end end
query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } },
:_score
]
query_hash[:highlight] = highlight_options(fields)
query_hash
end
def project_ids_filter(query_hash, project_ids)
if project_ids
query_hash[:query][:filtered][:filter] = {
and: [ { terms: { project_id: project_ids } } ]
}
end
query_hash
end end
end end
end end
...@@ -37,21 +37,7 @@ module IssuesSearch ...@@ -37,21 +37,7 @@ module IssuesSearch
query_hash = basic_query_hash(options[:in], query) query_hash = basic_query_hash(options[:in], query)
if options[:projects_ids] query_hash = project_ids_filter(query_hash, options[:projects_ids])
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
terms: {
project_id: [options[:projects_ids]].flatten
}
}
end
query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } },
:_score
]
query_hash[:highlight] = { fields: options[:in].inject({}) { |a, o| a[o.to_sym] = {} } }
self.__elasticsearch__.search(query_hash) self.__elasticsearch__.search(query_hash)
end end
......
...@@ -44,8 +44,8 @@ module MergeRequestsSearch ...@@ -44,8 +44,8 @@ module MergeRequestsSearch
query_hash = basic_query_hash(options[:in], query) query_hash = basic_query_hash(options[:in], query)
if options[:projects_ids] if options[:projects_ids]
query_hash[:query][:filtered][:filter] ||= { and: [] } query_hash[:query][:filtered][:filter] = {
query_hash[:query][:filtered][:filter][:and] << { and: [{
or: [ or: [
{ {
terms: { terms: {
...@@ -58,16 +58,10 @@ module MergeRequestsSearch ...@@ -58,16 +58,10 @@ module MergeRequestsSearch
} }
} }
] ]
}]
} }
end end
query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } },
:_score
]
query_hash[:highlight] = highlight_options(options[:in])
self.__elasticsearch__.search(query_hash) self.__elasticsearch__.search(query_hash)
end end
end end
......
...@@ -23,22 +23,7 @@ module MilestonesSearch ...@@ -23,22 +23,7 @@ module MilestonesSearch
query_hash = basic_query_hash(options[:in], query) query_hash = basic_query_hash(options[:in], query)
if options[:project_ids] query_hash = project_ids_filter(query_hash, options[:projects_ids])
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
terms: {
project_id: [options[:project_ids]].flatten
}
}
end
query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } },
:_score
]
query_hash[:highlight] = highlight_options(options[:in])
self.__elasticsearch__.search(query_hash) self.__elasticsearch__.search(query_hash)
end end
......
...@@ -33,14 +33,7 @@ module NotesSearch ...@@ -33,14 +33,7 @@ module NotesSearch
query_hash[:track_scores] = true query_hash[:track_scores] = true
end end
if options[:project_ids] query_hash = project_ids_filter(query_hash, options[:projects_ids])
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
terms: {
project_id: [options[:project_ids]].flatten
}
}
end
query_hash[:sort] = [ query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } }, { updated_at_sort: { order: :desc, mode: :min } },
......
...@@ -105,9 +105,6 @@ module ProjectsSearch ...@@ -105,9 +105,6 @@ module ProjectsSearch
query_hash[:sort] = [:_score] query_hash[:sort] = [:_score]
query_hash[:highlight] = highlight_options(options[:in])
self.__elasticsearch__.search(query_hash) self.__elasticsearch__.search(query_hash)
end end
end end
......
...@@ -38,27 +38,15 @@ module SnippetsSearch ...@@ -38,27 +38,15 @@ module SnippetsSearch
query_hash = basic_query_hash(options[:in], query) query_hash = basic_query_hash(options[:in], query)
if options[:ids] if options[:ids]
query_hash[:query][:filtered][:filter] ||= { and: [] } query_hash[:query][:filtered][:filter] = {
query_hash[:query][:filtered][:filter][:and] << { and: [ { terms: { id: [options[:ids]].flatten } } ]
terms: {
id: [options[:ids]].flatten
}
} }
end end
query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } },
:_score
]
query_hash[:highlight] = { fields: options[:in].inject({}) { |a, o| a[o.to_sym] = {} } }
self.__elasticsearch__.search(query_hash) self.__elasticsearch__.search(query_hash)
end end
def self.elastic_search_code(query, options: {}) def self.elastic_search_code(query, options: {})
options[:in] = %w(title file_name)
query_hash = { query_hash = {
query: { query: {
filtered: { filtered: {
...@@ -68,11 +56,8 @@ module SnippetsSearch ...@@ -68,11 +56,8 @@ module SnippetsSearch
} }
if options[:ids] if options[:ids]
query_hash[:query][:filtered][:filter] ||= { and: [] } query_hash[:query][:filtered][:filter] = {
query_hash[:query][:filtered][:filter][:and] << { and: [ { terms: { id: [options[:ids]].flatten } } ]
terms: {
id: [options[:ids]].flatten
}
} }
end end
...@@ -81,7 +66,7 @@ module SnippetsSearch ...@@ -81,7 +66,7 @@ module SnippetsSearch
:_score :_score
] ]
query_hash[:highlight] = { fields: options[:in].inject({}) { |a, o| a[o.to_sym] = {} } } query_hash[:highlight] = { fields: {content: {}} }
self.__elasticsearch__.search(query_hash) self.__elasticsearch__.search(query_hash)
end end
......
module UsersSearch
extend ActiveSupport::Concern
included do
include ApplicationSearch
mappings do
indexes :id, type: :integer
indexes :email, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, analyzer: :my_analyzer
indexes :name, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, analyzer: :my_analyzer
indexes :username, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, analyzer: :my_analyzer
indexes :bio, type: :string
indexes :skype, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, analyzer: :my_analyzer
indexes :linkedin, type: :string
indexes :twitter, type: :string, index_options: 'offsets', search_analyzer: :search_analyzer, analyzer: :my_analyzer
indexes :state, type: :string
indexes :website_url, type: :string
indexes :created_at, type: :date
indexes :admin, type: :boolean
end
def as_indexed_json(options = {})
as_json.merge({
name_sort: name.downcase,
updated_at_sort: updated_at,
created_at_sort: created_at
})
end
def self.elastic_search(query, options: {})
options[:in] = %w(name^3 username^2 email)
query_hash = basic_query_hash(options[:in], query)
query_hash[:query][:filtered][:filter] ||= { and: [] }
if options[:uids]
query_hash[:query][:filtered][:filter][:and] << {
ids: {
values: options[:uids]
}
}
end
if options[:active]
query_hash[:query][:filtered][:filter][:and] << {
terms: {
state: ["active"]
}
}
end
query_hash[:sort] = [:_score]
query_hash[:highlight] = highlight_options(options[:in])
self.__elasticsearch__.search(query_hash)
end
end
end
...@@ -73,7 +73,6 @@ class User < ActiveRecord::Base ...@@ -73,7 +73,6 @@ class User < ActiveRecord::Base
include Sortable include Sortable
include CaseSensitivity include CaseSensitivity
include TokenAuthenticatable include TokenAuthenticatable
include UsersSearch
add_authentication_token_field :authentication_token add_authentication_token_field :authentication_token
......
...@@ -12,7 +12,7 @@ namespace :gitlab do ...@@ -12,7 +12,7 @@ namespace :gitlab do
desc "Create indexes in the Elasticsearch from database records" desc "Create indexes in the Elasticsearch from database records"
task create_index: :environment do task create_index: :environment do
[Project, User, Issue, MergeRequest, Snippet, Note, Milestone].each do |klass| [Project, Issue, MergeRequest, Snippet, Note, Milestone].each do |klass|
klass.__elasticsearch__.create_index! klass.__elasticsearch__.create_index!
klass.import klass.import
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