lib/picky/search_facets.rb in picky-4.10.0 vs lib/picky/search_facets.rb in picky-4.11.0

- old
+ new

@@ -14,10 +14,12 @@ # # Usage: # search.facets :name, filter: 'surname:peter', more_than: 0 # def facets category_identifier, options = {} + # TODO Make it work. How should it work with multiple indexes? + # 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. # @@ -25,29 +27,63 @@ # We're done if there is no filter. # return counts unless filter_query = options[:filter] + # Pre-tokenize query token category. + # + predefined_categories = [index[category_identifier]] + + # Pre-tokenize key token – replace text below. + # Note: The original is not important. + # + # TODO Don't use predefined. + # + key_token = Query::Token.new '', nil, predefined_categories + # Pre-tokenize filter for reuse. # - tokenized_filter = tokenized filter_query, false + tokenized_filter_query = tokenized filter_query, false + tokenized_filter_query.tokens.push key_token # 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 + if no_counts + facets_without_counts counts, minimal_counts, tokenized_filter_query, key_token.text + else + facets_with_counts counts, minimal_counts, tokenized_filter_query, key_token.text + end + end + def facets_without_counts counts, minimal_counts, tokenized_filter_query, last_token_text + counts.inject([]) do |result, (key, _)| + # Replace only the key token text because that + # is the only information that changes in between + # queries. + # + last_token_text.replace key + total = search_with(tokenized_filter_query, 0, 0).total + next result unless total >= minimal_counts - if no_counts - result << key - else - result[key] = total; result - end + result << key + end + end + def facets_with_counts counts, minimal_counts, tokenized_filter_query, last_token_text + counts.inject({}) do |result, (key, _)| + # Replace only the key token text because that + # is the only information that changes in between + # queries. + # + last_token_text.replace key + total = search_with(tokenized_filter_query, 0, 0).total + + next result unless total >= minimal_counts + result[key] = total + result end end end