2017-03-21 21:32:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AccountSearchService < BaseService
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-26 09:21:36 -05:00
|
|
|
attr_reader :query, :limit, :offset, :options, :account
|
2017-04-14 21:17:07 -04:00
|
|
|
|
2022-11-09 23:49:30 -05:00
|
|
|
MENTION_ONLY_RE = /\A#{Account::MENTION_RE}\z/i
|
|
|
|
|
2022-10-26 06:10:02 -04:00
|
|
|
# Min. number of characters to look for non-exact matches
|
|
|
|
MIN_QUERY_LENGTH = 5
|
|
|
|
|
2023-08-08 03:09:14 -04:00
|
|
|
class QueryBuilder
|
|
|
|
def initialize(query, account, options = {})
|
|
|
|
@query = query
|
|
|
|
@account = account
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def build
|
|
|
|
AccountsIndex.query(
|
|
|
|
bool: {
|
|
|
|
must: {
|
|
|
|
function_score: {
|
|
|
|
query: {
|
|
|
|
bool: {
|
|
|
|
must: must_clauses,
|
2023-12-19 05:59:43 -05:00
|
|
|
must_not: must_not_clauses,
|
2023-08-08 03:09:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
functions: [
|
|
|
|
reputation_score_function,
|
|
|
|
followers_score_function,
|
|
|
|
time_distance_function,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
should: should_clauses,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def must_clauses
|
|
|
|
if @account && @options[:following]
|
|
|
|
[core_query, only_following_query]
|
|
|
|
else
|
|
|
|
[core_query]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-19 05:59:43 -05:00
|
|
|
def must_not_clauses
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2023-08-08 03:09:14 -04:00
|
|
|
def should_clauses
|
|
|
|
if @account && !@options[:following]
|
|
|
|
[boost_following_query]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This function limits results to only the accounts the user is following
|
|
|
|
def only_following_query
|
|
|
|
{
|
|
|
|
terms: {
|
|
|
|
id: following_ids,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# This function promotes accounts the user is following
|
|
|
|
def boost_following_query
|
|
|
|
{
|
|
|
|
terms: {
|
|
|
|
id: following_ids,
|
|
|
|
boost: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# This function deranks accounts that follow more people than follow them
|
|
|
|
def reputation_score_function
|
|
|
|
{
|
|
|
|
script_score: {
|
|
|
|
script: {
|
|
|
|
source: "(Math.max(doc['followers_count'].value, 0) + 0.0) / (Math.max(doc['followers_count'].value, 0) + Math.max(doc['following_count'].value, 0) + 1)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# This function promotes accounts that have more followers
|
|
|
|
def followers_score_function
|
|
|
|
{
|
|
|
|
script_score: {
|
|
|
|
script: {
|
|
|
|
source: "(Math.max(doc['followers_count'].value, 0) / (Math.max(doc['followers_count'].value, 0) + 1))",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# This function deranks accounts that haven't posted in a long time
|
|
|
|
def time_distance_function
|
|
|
|
{
|
|
|
|
gauss: {
|
|
|
|
last_status_at: {
|
|
|
|
scale: '30d',
|
|
|
|
offset: '30d',
|
|
|
|
decay: 0.3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def following_ids
|
|
|
|
@following_ids ||= @account.active_relationships.pluck(:target_account_id) + [@account.id]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AutocompleteQueryBuilder < QueryBuilder
|
|
|
|
private
|
|
|
|
|
|
|
|
def core_query
|
|
|
|
{
|
|
|
|
multi_match: {
|
|
|
|
query: @query,
|
|
|
|
type: 'bool_prefix',
|
2023-08-23 09:40:09 -04:00
|
|
|
fields: %w(username^2 username.*^2 display_name display_name.*),
|
2023-08-08 03:09:14 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FullQueryBuilder < QueryBuilder
|
|
|
|
private
|
|
|
|
|
|
|
|
def core_query
|
|
|
|
{
|
|
|
|
multi_match: {
|
|
|
|
query: @query,
|
|
|
|
type: 'most_fields',
|
|
|
|
fields: %w(username^2 display_name^2 text text.*),
|
|
|
|
operator: 'and',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-26 09:21:36 -05:00
|
|
|
def call(query, account = nil, options = {})
|
2024-05-24 09:13:23 -04:00
|
|
|
MastodonOTELTracer.in_span('AccountSearchService#call') do |span|
|
|
|
|
@query = query&.strip&.gsub(/\A@/, '')
|
|
|
|
@limit = options[:limit].to_i
|
|
|
|
@offset = options[:offset].to_i
|
|
|
|
@options = options
|
|
|
|
@account = account
|
|
|
|
|
|
|
|
span.add_attributes(
|
|
|
|
'search.offset' => @offset,
|
|
|
|
'search.limit' => @limit,
|
|
|
|
'search.backend' => Chewy.enabled? ? 'elasticsearch' : 'database'
|
|
|
|
)
|
2017-04-14 21:17:07 -04:00
|
|
|
|
2024-05-24 09:13:23 -04:00
|
|
|
search_service_results.compact.uniq.tap do |results|
|
|
|
|
span.set_attribute('search.results.count', results.size)
|
|
|
|
end
|
|
|
|
end
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2017-03-21 21:32:27 -04:00
|
|
|
|
2017-04-14 21:17:07 -04:00
|
|
|
def search_service_results
|
2019-08-15 19:24:03 -04:00
|
|
|
return [] if query.blank? || limit < 1
|
2017-03-21 21:32:27 -04:00
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
[exact_match] + search_results
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
def exact_match
|
|
|
|
return unless offset.zero? && username_complete?
|
2017-04-14 21:17:07 -04:00
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
return @exact_match if defined?(@exact_match)
|
2019-08-11 07:14:51 -04:00
|
|
|
|
2023-02-18 17:09:40 -05:00
|
|
|
match = if options[:resolve]
|
|
|
|
ResolveAccountService.new.call(query)
|
|
|
|
elsif domain_is_local?
|
|
|
|
Account.find_local(query_username)
|
|
|
|
else
|
|
|
|
Account.find_remote(query_username, query_domain)
|
|
|
|
end
|
2020-07-26 17:58:36 -04:00
|
|
|
|
|
|
|
match = nil if !match.nil? && !account.nil? && options[:following] && !account.following?(match)
|
|
|
|
|
|
|
|
@exact_match = match
|
2019-08-15 19:24:03 -04:00
|
|
|
end
|
2019-08-11 07:14:51 -04:00
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
def search_results
|
|
|
|
return [] if limit_for_non_exact_results.zero?
|
2019-08-11 07:14:51 -04:00
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
@search_results ||= begin
|
2019-09-26 12:06:08 -04:00
|
|
|
results = from_elasticsearch if Chewy.enabled?
|
|
|
|
results ||= from_database
|
|
|
|
results
|
2019-08-15 19:24:03 -04:00
|
|
|
end
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
def from_database
|
|
|
|
if account
|
|
|
|
advanced_search_results
|
|
|
|
else
|
|
|
|
simple_search_results
|
|
|
|
end
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
def advanced_search_results
|
2022-07-04 20:41:40 -04:00
|
|
|
Account.advanced_search_for(terms_for_query, account, limit: limit_for_non_exact_results, following: options[:following], offset: offset)
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
def simple_search_results
|
2022-07-04 20:41:40 -04:00
|
|
|
Account.search_for(terms_for_query, limit: limit_for_non_exact_results, offset: offset)
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
2017-03-21 21:32:27 -04:00
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
def from_elasticsearch
|
2023-08-08 03:09:14 -04:00
|
|
|
query_builder = begin
|
|
|
|
if options[:use_searchable_text]
|
|
|
|
FullQueryBuilder.new(terms_for_query, account, options.slice(:following))
|
|
|
|
else
|
|
|
|
AutocompleteQueryBuilder.new(terms_for_query, account, options.slice(:following))
|
2019-08-15 19:24:03 -04:00
|
|
|
end
|
|
|
|
end
|
2017-04-14 21:17:07 -04:00
|
|
|
|
2023-08-08 03:09:14 -04:00
|
|
|
records = query_builder.build.limit(limit_for_non_exact_results).offset(offset).objects.compact
|
2019-08-11 07:14:51 -04:00
|
|
|
|
2023-12-13 02:47:32 -05:00
|
|
|
ActiveRecord::Associations::Preloader.new(records: records, associations: [:account_stat, { user: :role }]).call
|
2019-08-11 07:14:51 -04:00
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
records
|
2019-09-26 12:06:08 -04:00
|
|
|
rescue Faraday::ConnectionFailed, Parslet::ParseFailed
|
|
|
|
nil
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
2017-03-21 21:32:27 -04:00
|
|
|
|
2019-08-11 07:14:51 -04:00
|
|
|
def limit_for_non_exact_results
|
2022-10-26 06:10:02 -04:00
|
|
|
return 0 if @account.nil? && query.size < MIN_QUERY_LENGTH
|
|
|
|
|
2019-08-15 19:24:03 -04:00
|
|
|
if exact_match?
|
2019-08-11 07:14:51 -04:00
|
|
|
limit - 1
|
|
|
|
else
|
|
|
|
limit
|
|
|
|
end
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def terms_for_query
|
|
|
|
if domain_is_local?
|
|
|
|
query_username
|
|
|
|
else
|
2019-08-15 19:24:03 -04:00
|
|
|
query
|
2017-04-14 21:17:07 -04:00
|
|
|
end
|
2017-03-21 21:32:27 -04:00
|
|
|
end
|
2019-08-15 19:24:03 -04:00
|
|
|
|
|
|
|
def split_query_string
|
|
|
|
@split_query_string ||= query.split('@')
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_username
|
|
|
|
@query_username ||= split_query_string.first || ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_domain
|
|
|
|
@query_domain ||= query_without_split? ? nil : split_query_string.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_without_split?
|
|
|
|
split_query_string.size == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def domain_is_local?
|
|
|
|
@domain_is_local ||= TagManager.instance.local_domain?(query_domain)
|
|
|
|
end
|
|
|
|
|
|
|
|
def exact_match?
|
|
|
|
exact_match.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_complete?
|
2022-11-09 23:49:30 -05:00
|
|
|
query.include?('@') && "@#{query}".match?(MENTION_ONLY_RE)
|
2019-08-15 19:24:03 -04:00
|
|
|
end
|
2017-03-21 21:32:27 -04:00
|
|
|
end
|