Add group suggestion

This commit is contained in:
noellabo 2020-07-14 19:03:54 +09:00
parent 141a3d8a01
commit c6417be067
5 changed files with 24 additions and 5 deletions

View file

@ -35,6 +35,8 @@ class AccountsIndex < Chewy::Index
field :edge_ngram, type: 'text', analyzer: 'edge_ngram', search_analyzer: 'content'
end
field :actor_type, type: 'keyword', analyzer: 'content'
field :following_count, type: 'long', value: ->(account) { account.following.local.count }
field :followers_count, type: 'long', value: ->(account) { account.followers.local.count }
field :subscribing_count, type: 'long', value: ->(account) { account.subscribing.local.count }

View file

@ -18,6 +18,7 @@ class Api::V1::Accounts::SearchController < Api::BaseController
limit: limit_param(DEFAULT_ACCOUNTS_LIMIT),
resolve: truthy_param?(:resolve),
following: truthy_param?(:following),
group_only: truthy_param?(:group_only),
offset: params[:offset]
)
end

View file

@ -451,9 +451,11 @@ const fetchComposeSuggestionsAccounts = throttle((dispatch, getState, token) =>
}),
params: {
q: token.slice(1),
q: token.replace(/^@@?/, ''),
resolve: false,
limit: 4,
following: token.startsWith('@@'),
group_only: token.startsWith('@@'),
},
}).then(response => {
dispatch(importFetchedAccounts(response.data));

View file

@ -443,9 +443,13 @@ class Account < ApplicationRecord
DeliveryFailureTracker.without_unavailable(urls)
end
def search_for(terms, limit = 10, offset = 0)
def search_for(terms, limit = 10, group = false, offset = 0)
textsearch, query = generate_query_for_search(terms)
sql_where_group = <<-SQL if group
AND accounts.actor_type = 'Group'
SQL
sql = <<-SQL.squish
SELECT
accounts.*,
@ -454,6 +458,7 @@ class Account < ApplicationRecord
WHERE #{query} @@ #{textsearch}
AND accounts.suspended_at IS NULL
AND accounts.moved_to_account_id IS NULL
#{sql_where_group}
ORDER BY rank DESC
LIMIT ? OFFSET ?
SQL
@ -463,9 +468,13 @@ class Account < ApplicationRecord
records
end
def advanced_search_for(terms, account, limit = 10, following = false, offset = 0)
def advanced_search_for(terms, account, limit = 10, following = false, group = false, offset = 0)
textsearch, query = generate_query_for_search(terms)
sql_where_group = <<-SQL if group
AND accounts.actor_type = 'Group'
SQL
if following
sql = <<-SQL.squish
WITH first_degree AS (
@ -484,6 +493,7 @@ class Account < ApplicationRecord
AND #{query} @@ #{textsearch}
AND accounts.suspended_at IS NULL
AND accounts.moved_to_account_id IS NULL
#{sql_where_group}
GROUP BY accounts.id
ORDER BY rank DESC
LIMIT ? OFFSET ?
@ -500,6 +510,7 @@ class Account < ApplicationRecord
WHERE #{query} @@ #{textsearch}
AND accounts.suspended_at IS NULL
AND accounts.moved_to_account_id IS NULL
#{sql_where_group}
GROUP BY accounts.id
ORDER BY rank DESC
LIMIT ? OFFSET ?

View file

@ -61,11 +61,11 @@ class AccountSearchService < BaseService
end
def advanced_search_results
Account.advanced_search_for(terms_for_query, account, limit_for_non_exact_results, options[:following], offset)
Account.advanced_search_for(terms_for_query, account, limit_for_non_exact_results, options[:following], options[:group_only], offset)
end
def simple_search_results
Account.search_for(terms_for_query, limit_for_non_exact_results, offset)
Account.search_for(terms_for_query, limit_for_non_exact_results, options[:group_only], offset)
end
def from_elasticsearch
@ -80,6 +80,9 @@ class AccountSearchService < BaseService
elsif following_ids.any?
should_clauses << { terms: { id: following_ids, boost: 100 } }
end
if options[:group_only]
must_clauses << { term: { actor_type: 'group' } }
end
end
query = { bool: { must: must_clauses, should: should_clauses } }