Module: Stanford::SolrHelper

Included in:
SearchworksHelper
Defined in:
lib/stanford/solr_helper.rb

Overview

Stanford SolrHelper is a controller layer mixin. It is in the controller scope: request params, session etc.

NOTE: Be careful when creating variables here as they may be overriding something that already exists. The ActionController docs: api.rubyonrails.org/classes/ActionController/Base.html

Override these methods in your own controller for customizations:

class HomeController < ActionController::Base

  
  include Stanford::SolrHelper
  
  def solr_search_params
    super.merge :per_page=>10
  end
  

end

Instance Method Summary (collapse)

Instance Method Details

- (Object) advanced_search_facet_params(extra_controller_params = {})

returns a params hash for the advanced search facet field solr query. used primary by the get_facet_pagination method



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stanford/solr_helper.rb', line 34

def advanced_search_facet_params(extra_controller_params={})
  input = params.deep_merge(extra_controller_params)
  {
    :qt => Blacklight.config[:default_qt],
    :per_page => 0,
    :phrase_filters => input[:f],
    "f.callnum_top_facet.facet.sort" => "false",
    "f.format.facet.sort" => "false",
    "f.building_facet.facet.sort" => "false",
    "f.access_facet.facet.sort" => "false",
    "f.language.facet.limit" => 100
  }
end

- (Object) get_advanced_search_facets(extra_controller_params = {})

a solr query method given a user query, return a solr response containing both result docs and facets

  • mixes in the Blacklight::Solr::SpellingSuggestions module

    • the response will have a spelling_suggestions method



52
53
54
# File 'lib/stanford/solr_helper.rb', line 52

def get_advanced_search_facets(extra_controller_params={})
  Blacklight.solr.find self.advanced_search_facet_params(extra_controller_params)
end

- (Object) get_docs_for_field_values(values, field)

given a field name and array of values, get the matching SOLR documents



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/stanford/solr_helper.rb', line 93

def get_docs_for_field_values(values, field)
  value_str = "(\"" + values.join("\" OR \"") + "\")"
  solr_params = {
    :qt => "standard",   # need boolean for OR
    :q => "#{field}:#{value_str}",
    'fl' => "*",
    'facet' => 'false',
    'spellcheck' => 'false'
  }
  
  solr_response = Blacklight.solr.find solr_params
  solr_response.docs
end

- (Object) get_home_facets(extra_controller_params = {})

a solr query method given a user query, return a solr response containing both result docs and facets

  • mixes in the Blacklight::Solr::SpellingSuggestions module

    • the response will have a spelling_suggestions method



60
61
62
# File 'lib/stanford/solr_helper.rb', line 60

def get_home_facets(extra_controller_params={})
  Blacklight.solr.find self.home_facet_params(extra_controller_params)
end

- (Object) get_next_terms(curr_value, field, how_many)

given a field name and a field value, get the next “alphabetic” N

 terms for the field 
 returns array of one element hashes with key=term and value=count

NOTE: terms in index are case sensitive! Okay for shelfkey …



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/stanford/solr_helper.rb', line 68

def get_next_terms(curr_value, field, how_many)
    # TermsComponent Query to get the terms
    solr_params = {
      'terms.fl' => field,
      'terms.lower' => curr_value,
      :per_page => how_many
    }
    solr_response = Blacklight.solr.send_request('/alphaTerms', solr_params)
    
    # create array of one element hashes with key=term and value=count
    result = []
    terms ||= solr_response['terms'] || []
    field_terms ||= terms[1] || []
    # field_terms is an array of value, then num hits, then next value, then hits ...
    i = 0
    until result.length == how_many || i >= field_terms.length do
      term_hash = {field_terms[i] => field_terms[i+1]}
      result << term_hash
      i = i + 2
    end
    
    result
  end

  # given a field name and array of values, get the matching SOLR documents
  def get_docs_for_field_values(values, field)
    value_str = "(\"" + values.join("\" OR \"") + "\")"
    solr_params = {
      :qt => "standard",   # need boolean for OR
      :q => "#{field}:#{value_str}",
      'fl' => "*",
      'facet' => 'false',
      'spellcheck' => 'false'
    }
    
    solr_response = Blacklight.solr.find solr_params
    solr_response.docs
  end


end

- (Object) home_facet_params(extra_controller_params = {})

returns a params hash for a home facet field solr query. used primary by the get_facet_pagination method



22
23
24
25
26
27
28
29
30
# File 'lib/stanford/solr_helper.rb', line 22

def home_facet_params(extra_controller_params={})
  input = params.deep_merge(extra_controller_params)
  {
    :qt => Blacklight.config[:default_qt],
    :per_page => 0,
    :phrase_filters => input[:f],
    "f.callnum_top_facet.facet.sort" => "false"
  }
end