]> cat aescling's git repositories - mastodon.git/blob - app/services/tag_search_service.rb
Fix search error when ElasticSearch is enabled but not available (#11954)
[mastodon.git] / app / services / tag_search_service.rb
1 # frozen_string_literal: true
2
3 class TagSearchService < BaseService
4 def call(query, options = {})
5 @query = query.strip.gsub(/\A#/, '')
6 @offset = options[:offset].to_i
7 @limit = options[:limit].to_i
8
9 results = from_elasticsearch if Chewy.enabled?
10 results ||= from_database
11
12 results
13 end
14
15 private
16
17 def from_elasticsearch
18 query = {
19 function_score: {
20 query: {
21 multi_match: {
22 query: @query,
23 fields: %w(name.edge_ngram name),
24 type: 'most_fields',
25 operator: 'and',
26 },
27 },
28
29 functions: [
30 {
31 field_value_factor: {
32 field: 'usage',
33 modifier: 'log2p',
34 missing: 0,
35 },
36 },
37
38 {
39 gauss: {
40 last_status_at: {
41 scale: '7d',
42 offset: '14d',
43 decay: 0.5,
44 },
45 },
46 },
47 ],
48
49 boost_mode: 'multiply',
50 },
51 }
52
53 filter = {
54 bool: {
55 should: [
56 {
57 term: {
58 reviewed: {
59 value: true,
60 },
61 },
62 },
63
64 {
65 term: {
66 name: {
67 value: @query,
68 },
69 },
70 },
71 ],
72 },
73 }
74
75 TagsIndex.query(query).filter(filter).limit(@limit).offset(@offset).objects.compact
76 rescue Faraday::ConnectionFailed, Parslet::ParseFailed
77 nil
78 end
79
80 def from_database
81 Tag.search_for(@query, @limit, @offset)
82 end
83 end
This page took 0.083445 seconds and 4 git commands to generate.