Sha256: 5dc033c954bc3debd6e728e56cdca7c7ff1b2502f157677d5b15eb8aa764df41

Contents?: true

Size: 1.56 KB

Versions: 11

Compression:

Stored size: 1.56 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.
      #
      counts.inject(no_counts ? [] : {}) do |result, (key, _)|
        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
      end
    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
picky-4.10.0 lib/picky/search_facets.rb
picky-4.9.0 lib/picky/search_facets.rb
picky-4.8.1 lib/picky/search_facets.rb
picky-4.8.0 lib/picky/search_facets.rb
picky-4.7.0 lib/picky/search_facets.rb
picky-4.6.6 lib/picky/search_facets.rb
picky-4.6.5 lib/picky/search_facets.rb
picky-4.6.4 lib/picky/search_facets.rb
picky-4.6.3 lib/picky/search_facets.rb
picky-4.6.2 lib/picky/search_facets.rb
picky-4.6.1 lib/picky/search_facets.rb