Sha256: 2e5411a7ab589edc906630907437075c4edff6d76e1caeb74bc332561b02f635

Contents?: true

Size: 1.41 KB

Versions: 62

Compression:

Stored size: 1.41 KB

Contents

module Workarea
  module Search
    class TermsFacet < Facet
      def type
        :terms
      end

      def system_name
        @system_name ||= name.systemize
      end

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

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

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

      def sorting
        FacetSorting.new(system_name.to_sym)
      end

      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

      def value_to_param(value)
        value.to_s
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.15 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.36 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.14 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.35 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.13 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.34 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.12 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.33 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.11 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.10 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.32 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.9 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.31 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.8 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.30 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.7 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.29 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.6 app/queries/workarea/search/terms_facet.rb
workarea-core-3.4.28 app/queries/workarea/search/terms_facet.rb
workarea-core-3.5.5 app/queries/workarea/search/terms_facet.rb