Sha256: a0a343abb105d06e7cc949856553b7df3665a0b84667c352b016620ec551fe4a

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module Picky

  class Search

    # Returns a list/hash of filtered facets.
    # 
    # Params
    #   category: The category whose facets to return.
    # 
    # Options
    #   counts: Whether you want counts (returns a Hash) or not (returns an Array).
    #   at_least: A minimum count a facet needs to have (inclusive). 
    #   filter: A query to filter the facets with.
    #
    # Usage:
    #   search.facets :name, filter: 'surname:peter', more_than: 0
    #
    def facets category_identifier, options = {}
      raise "#{__method__} cannot be used on searches with more than 1 index yet. Sorry!" if indexes.size > 1
      index = indexes.first
      
      # Get index-specific facet counts.
      #
      counts = index.facets category_identifier, options
      
      # We're done if there is no filter.
      #
      return counts unless filter_query = options[:filter]
      
      # Pre-tokenize filter for reuse.
      #
      tokenized_filter = tokenized filter_query, false
      
      # Extract options.
      #
      no_counts = options[:counts] == false
      minimal_counts = options[:at_least] || 1 # Default needs at least one.
      
      # Get actual counts.
      #
      # TODO Rewrite.
      #
      counts.inject(no_counts ? [] : {}) do |result, key_count|
        key, _ = key_count
        tokenized_query = tokenized "#{category_identifier}:#{key}", false
        total = search_with(tokenized_filter + tokenized_query, 0, 0).total
        next result unless total >= minimal_counts
        if no_counts
          result << key
        else
          result[key] = total; result
        end
        result
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
picky-4.5.12 lib/picky/search_facets.rb