Class: Sunspot::Search::StandardSearch
- Inherits:
-
AbstractSearch
- Object
- AbstractSearch
- Sunspot::Search::StandardSearch
- Defined in:
- lib/sunspot/search/standard_search.rb
Overview
This class encapsulates the results of a Solr search. It provides access to search results, total result count, facets, and pagination information. Instances of Search are returned by the Sunspot.search and Sunspot.new_search methods.
Instance Attribute Summary
Attributes inherited from AbstractSearch
Instance Method Summary collapse
- #request_handler ⇒ Object
-
#solr_spellcheck ⇒ Object
Return the raw spellcheck block from the Solr response.
-
#spellcheck_collation(*terms) ⇒ Object
Provide a collated query.
-
#spellcheck_suggestion_for(term) ⇒ Object
Return the suggestion with the single highest frequency.
-
#spellcheck_suggestions ⇒ Object
Reformat the oddly-formatted spellcheck suggestion array into a more useful hash.
Methods inherited from AbstractSearch
#add_date_facet, #add_field_facet, #add_field_stats, #add_group, #add_json_facet, #add_query_facet, #add_range_facet, #build, #dynamic_facet, #execute, #execute!, #facet, #facet_response, #group, #group_response, #highlights_for, #hits, #initialize, #inspect, #json_facet_response, #json_facet_stats, #query_time, #results, #stats, #stats_response, #total
Methods included from HitEnumerable
#data_accessor_for, #each_hit_with_result, #hits, #populate_hits, #verified_hits
Constructor Details
This class inherits a constructor from Sunspot::Search::AbstractSearch
Instance Method Details
#request_handler ⇒ Object
10 11 12 |
# File 'lib/sunspot/search/standard_search.rb', line 10 def request_handler super || :select end |
#solr_spellcheck ⇒ Object
Return the raw spellcheck block from the Solr response
15 16 17 |
# File 'lib/sunspot/search/standard_search.rb', line 15 def solr_spellcheck @solr_spellcheck ||= @solr_result['spellcheck'] || {} end |
#spellcheck_collation(*terms) ⇒ Object
Provide a collated query. If the user provides a query string, tokenize it on whitespace and replace terms strictly not present in the index. Otherwise return Solr’s suggested collation.
Solr’s suggested collation is more liberal, replacing even terms that are present in the index.
Mix and match in your views for a blend of strict and liberal collations.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sunspot/search/standard_search.rb', line 55 def spellcheck_collation(*terms) if solr_spellcheck['suggestions'] && solr_spellcheck['suggestions'].length > 0 collation = terms.join(" ").dup if terms # If we are given a query string, tokenize it and strictly replace # the terms that aren't present in the index if terms.length > 0 terms.each do |term| if (spellcheck_suggestions[term]||{})['origFreq'] == 0 collation[term] = spellcheck_suggestion_for(term) end end end # If no query was given, or all terms are present in the index, # return Solr's suggested collation. if terms.length == 0 collation = solr_spellcheck['collations'][-1] end collation else nil end end |
#spellcheck_suggestion_for(term) ⇒ Object
Return the suggestion with the single highest frequency. Requires the extended results format.
41 42 43 44 45 |
# File 'lib/sunspot/search/standard_search.rb', line 41 def spellcheck_suggestion_for(term) spellcheck_suggestions[term]['suggestion'].sort_by do |suggestion| suggestion['freq'] end.last['word'] end |
#spellcheck_suggestions ⇒ Object
Reformat the oddly-formatted spellcheck suggestion array into a more useful hash.
Original: [term, suggestion, term, suggestion, …, “correctlySpelled”, bool, “collation”, str]
"collation" is only included if spellcheck.collation was set to true
Returns: { term => suggestion, term => suggestion }
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sunspot/search/standard_search.rb', line 25 def spellcheck_suggestions unless defined?(@spellcheck_suggestions) @spellcheck_suggestions = {} count = ((solr_spellcheck['suggestions'] || []).length) / 2 (0..(count - 1)).each do |i| break if ["correctlySpelled", "collation"].include? solr_spellcheck[i] term = solr_spellcheck['suggestions'][i * 2] suggestion = solr_spellcheck['suggestions'][(i * 2) + 1] @spellcheck_suggestions[term] = suggestion end end @spellcheck_suggestions end |