Class: Workarea::Search::TermsFacet

Inherits:
Facet show all
Defined in:
app/queries/workarea/search/terms_facet.rb

Instance Attribute Summary

Attributes inherited from Filter

#name, #options, #search

Instance Method Summary collapse

Methods inherited from Facet

#aggregation_filter, #params_for, #query_clause, #selected?, #selections, #useless?

Methods inherited from Filter

#current_value, #display_name, #initialize, #params_for, #query_clause, #selected?, #useless?, #valid_params

Constructor Details

This class inherits a constructor from Workarea::Search::Filter

Instance Method Details

#aggregationObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/queries/workarea/search/terms_facet.rb', line 17

def aggregation
  {
    system_name => {
      filter: aggregation_filter,
      aggs: {
        system_name => {
          terms: {
            field: "facets.#{system_name}",
            size: facet_size
          }.merge(sorting.to_h)
        }
      }
    }
  }
end

#facet_sizeObject



33
34
35
# File 'app/queries/workarea/search/terms_facet.rb', line 33

def facet_size
  Workarea.config.search_facet_result_sizes[system_name.to_sym] || Workarea.config.default_search_facet_result_sizes
end

#post_filter_clauseObject



12
13
14
15
# File 'app/queries/workarea/search/terms_facet.rb', line 12

def post_filter_clause
  return unless selections.present?
  { terms: { "facets.#{system_name}" => selections } }
end

#resultsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/queries/workarea/search/terms_facet.rb', line 41

def results
  @results ||=
    begin
      results = Array.wrap(
        search.response.dig('aggregations', system_name, system_name, 'buckets')
      )

      results = results.each_with_object({}) do |result, memo|
        next unless result['doc_count'].positive?
        memo[result['key']] = result['doc_count']
      end

      sorting.apply(results, facet_size)
    end
end

#sortingObject



37
38
39
# File 'app/queries/workarea/search/terms_facet.rb', line 37

def sorting
  FacetSorting.new(system_name.to_sym)
end

#system_nameObject



8
9
10
# File 'app/queries/workarea/search/terms_facet.rb', line 8

def system_name
  @system_name ||= name.systemize
end

#typeObject



4
5
6
# File 'app/queries/workarea/search/terms_facet.rb', line 4

def type
  :terms
end

#value_to_param(value) ⇒ Object



57
58
59
# File 'app/queries/workarea/search/terms_facet.rb', line 57

def value_to_param(value)
  value.to_s
end