]> cat aescling's git repositories - mastodon.git/blob - app/services/tag_search_service.rb
64dd76bb778d7c78c20e6e04262ee940b15edfd9
[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 if Chewy.enabled?
10 from_elasticsearch
11 else
12 from_database
13 end
14 end
15
16 private
17
18 def from_elasticsearch
19 query = {
20 function_score: {
21 query: {
22 multi_match: {
23 query: @query,
24 fields: %w(name.edge_ngram name),
25 type: 'most_fields',
26 operator: 'and',
27 },
28 },
29
30 functions: [
31 {
32 field_value_factor: {
33 field: 'usage',
34 modifier: 'log2p',
35 missing: 0,
36 },
37 },
38
39 {
40 gauss: {
41 last_status_at: {
42 scale: '7d',
43 offset: '14d',
44 decay: 0.5,
45 },
46 },
47 },
48 ],
49
50 boost_mode: 'multiply',
51 },
52 }
53
54 filter = {
55 bool: {
56 should: [
57 {
58 term: {
59 reviewed: {
60 value: true,
61 },
62 },
63 },
64
65 {
66 term: {
67 name: {
68 value: @query,
69 },
70 },
71 },
72 ],
73 },
74 }
75
76 TagsIndex.query(query).filter(filter).limit(@limit).offset(@offset).objects.compact
77 end
78
79 def from_database
80 Tag.search_for(@query, @limit, @offset)
81 end
82 end
This page took 0.089094 seconds and 4 git commands to generate.