Class: FullSearch::MultiSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/full_search/multi_search.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, groups:) ⇒ MultiSearch

Returns a new instance of MultiSearch.



9
10
11
12
# File 'lib/full_search/multi_search.rb', line 9

def initialize(query:, groups:)
  @query = query.to_s.strip
  @groups = groups
end

Class Method Details

.call(query:, groups:) ⇒ Object



5
6
7
# File 'lib/full_search/multi_search.rb', line 5

def self.call(query:, groups:)
  new(query: query, groups: groups).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/full_search/multi_search.rb', line 14

def call
  IndexCache.with_cache do
    searched = groups.map do |group|
      model = fetch(group, :model)
      raise FullSearch::NotConfiguredError, "#{model} is not full_search configured" unless model.full_search_dsl

      filters = group[:filters] || {}
      limit = positive_integer(group[:limit], 8)
      offset = positive_integer(group[:offset], 0)
      raw_limit = limit + 1

      relation = model.full_search(
        query,
        filters: filters,
        limit: raw_limit,
        offset: offset,
        highlight: group[:highlight],
        highlight_fields: group[:highlight_fields],
        matching_strategy: group[:matching_strategy],
        per_strategy_limit: group[:per_strategy_limit],
        scope: group[:scope],
        includes: group[:includes]
      )

      records = relation.to_a
      has_more = records.size > limit
      records = records.first(limit) if has_more

      group.slice(:key, :label, :icon, :model).merge(
        results: records,
        has_more: has_more,
        total_count: records.size
      )
    end

    {groups: searched, total_count: searched.sum { |g| g[:total_count] }}
  end
end