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

ES: one more refactoring

parent 15566190
......@@ -70,7 +70,7 @@ module ApplicationSearch
end
def basic_query_hash(fields, query)
if query.present?
query_hash = if query.present?
{
query: {
filtered: {
......@@ -94,6 +94,25 @@ module ApplicationSearch
track_scores: true
}
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
......@@ -37,21 +37,7 @@ module IssuesSearch
query_hash = basic_query_hash(options[:in], query)
if 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] = {} } }
query_hash = project_ids_filter(query_hash, options[:projects_ids])
self.__elasticsearch__.search(query_hash)
end
......
......@@ -44,8 +44,8 @@ module MergeRequestsSearch
query_hash = basic_query_hash(options[:in], query)
if options[:projects_ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
query_hash[:query][:filtered][:filter] = {
and: [{
or: [
{
terms: {
......@@ -58,16 +58,10 @@ module MergeRequestsSearch
}
}
]
}]
}
end
query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } },
:_score
]
query_hash[:highlight] = highlight_options(options[:in])
self.__elasticsearch__.search(query_hash)
end
end
......
......@@ -23,22 +23,7 @@ module MilestonesSearch
query_hash = basic_query_hash(options[:in], query)
if options[:project_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])
query_hash = project_ids_filter(query_hash, options[:projects_ids])
self.__elasticsearch__.search(query_hash)
end
......
......@@ -33,14 +33,7 @@ module NotesSearch
query_hash[:track_scores] = true
end
if options[:project_ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
terms: {
project_id: [options[:project_ids]].flatten
}
}
end
query_hash = project_ids_filter(query_hash, options[:projects_ids])
query_hash[:sort] = [
{ updated_at_sort: { order: :desc, mode: :min } },
......
......@@ -105,9 +105,6 @@ module ProjectsSearch
query_hash[:sort] = [:_score]
query_hash[:highlight] = highlight_options(options[:in])
self.__elasticsearch__.search(query_hash)
end
end
......
......@@ -38,27 +38,15 @@ module SnippetsSearch
query_hash = basic_query_hash(options[:in], query)
if options[:ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
terms: {
id: [options[:ids]].flatten
}
query_hash[:query][:filtered][:filter] = {
and: [ { terms: { id: [options[: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)
end
def self.elastic_search_code(query, options: {})
options[:in] = %w(title file_name)
query_hash = {
query: {
filtered: {
......@@ -68,11 +56,8 @@ module SnippetsSearch
}
if options[:ids]
query_hash[:query][:filtered][:filter] ||= { and: [] }
query_hash[:query][:filtered][:filter][:and] << {
terms: {
id: [options[:ids]].flatten
}
query_hash[:query][:filtered][:filter] = {
and: [ { terms: { id: [options[:ids]].flatten } } ]
}
end
......@@ -81,7 +66,7 @@ module SnippetsSearch
:_score
]
query_hash[:highlight] = { fields: options[:in].inject({}) { |a, o| a[o.to_sym] = {} } }
query_hash[:highlight] = { fields: {content: {}} }
self.__elasticsearch__.search(query_hash)
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
include Sortable
include CaseSensitivity
include TokenAuthenticatable
include UsersSearch
add_authentication_token_field :authentication_token
......
......@@ -12,7 +12,7 @@ namespace :gitlab do
desc "Create indexes in the Elasticsearch from database records"
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.import
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