Class: SolrLite::SearchParams
- Inherits:
-
Object
- Object
- SolrLite::SearchParams
- Defined in:
- lib/search_params.rb
Overview
Represents the parameters to send to Solr during a search.
Constant Summary collapse
- DEFAULT_PAGE_SIZE =
20
Instance Attribute Summary collapse
-
#facet_limit ⇒ Object
- Integer
-
Number of facet values to request from Solr.
-
#facets ⇒ Object
- Array
-
Array of FacetField objects to pass to Solr.
-
#fl ⇒ Object
- Array
-
List of fields names to request from Solr.
-
#fq ⇒ Object
- Array
-
Array of FilterQuery objects to pass to Solr.
-
#group_count ⇒ Object
- String
-
The name of the value in the response to hold the number of groups found in a grouped request.
-
#hl ⇒ Object
- Bool
-
Set to true to request hit highlighting information from Solr.
-
#hl_fl ⇒ Object
- String
-
Sets the highlight fields (hl.fl) to request from Solr.
-
#hl_snippets ⇒ Object
- Integer
-
Sets the number of hit highlights to request from Solr.
-
#page ⇒ Object
- Integer
-
Page number to request from Solr.
-
#page_size ⇒ Object
- Integer
-
Number of documents per page to request from Solr.
-
#pf ⇒ Object
- String
-
Sets the pf value to pass to Solr.
-
#q ⇒ Object
- String
-
The q value to pass to Solr.
-
#qf ⇒ Object
- String
-
Sets the qf value to pass to Solr.
-
#sort ⇒ Object
- String
-
Sort string to pass to Solr.
-
#spellcheck ⇒ Object
- Bool
-
True to request Solr to use spellchecking (defaults to false).
Class Method Summary collapse
-
.from_query_string(qs, facet_fields = []) ⇒ SolrLite::SearchParams
Creates a SearchParams object with the values in a query string.
-
.url_trim(value) ⇒ Object
Trims leading and trailing spaces from a URL escaped string and returns the string escaped.
Instance Method Summary collapse
-
#facet_for_field(field) ⇒ SolrLite::FacetField
Returns facet information about a given field.
-
#initialize(q = "", fq = [], facets = []) ⇒ SearchParams
constructor
Creates an instance of the SearchParams class.
-
#set_facet_remove_url(field, value, url) ⇒ Object
Sets the ‘remove_url` value for the given facet and value.
-
#star_row=(start) ⇒ Object
Sets the starting row number and recalculates the current page based on the current page_size.
-
#start_row ⇒ Integer
Calculates the starting row number.
-
#to_form_values ⇒ Array
Returns an array of values that can be added to an HTML form to represent the current search parameters.
-
#to_s ⇒ String
Returns a friendly string version of this object.
-
#to_solr_query_string(extra_fqs = []) ⇒ String
Calculates the query string that needs to be passed to Solr to issue a search with the current search parameters.
-
#to_user_query_string(facet_to_ignore = nil, q_override = nil) ⇒ String
Calculates the query string that we need render on the browser to execute a search with the current parameters.
-
#to_user_query_string_no_q ⇒ String
Calculates the query string that we need render on the browser to execute a search with the current parameters and NO q parameter.
Constructor Details
#initialize(q = "", fq = [], facets = []) ⇒ SearchParams
Creates an instance of the SearchParams class.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/search_params.rb', line 63 def initialize(q = "", fq = [], facets = []) @q = q @fq = fq # array of FilterQuery @facets = facets # array of FacetField @page = 1 @page_size = DEFAULT_PAGE_SIZE @fl = nil @sort = "" @facet_limit = nil @spellcheck = false # Solr's hit highlighting parameters @hl = false @hl_fl = nil @hl_snippets = 1 @group_count = "group_count" @qf = nil @pf = nil end |
Instance Attribute Details
#facet_limit ⇒ Object
- Integer
-
Number of facet values to request from Solr.
31 32 33 |
# File 'lib/search_params.rb', line 31 def facet_limit @facet_limit end |
#facets ⇒ Object
- Array
-
Array of FacetField objects to pass to Solr.
16 17 18 |
# File 'lib/search_params.rb', line 16 def facets @facets end |
#fl ⇒ Object
- Array
-
List of fields names to request from Solr.
25 26 27 |
# File 'lib/search_params.rb', line 25 def fl @fl end |
#fq ⇒ Object
- Array
-
Array of FilterQuery objects to pass to Solr.
13 14 15 |
# File 'lib/search_params.rb', line 13 def fq @fq end |
#group_count ⇒ Object
- String
-
The name of the value in the response to hold the number of groups found
in a grouped request. Defaults to “group_count”, set to nil to omit.
47 48 49 |
# File 'lib/search_params.rb', line 47 def group_count @group_count end |
#hl ⇒ Object
- Bool
-
Set to true to request hit highlighting information from Solr.
37 38 39 |
# File 'lib/search_params.rb', line 37 def hl @hl end |
#hl_fl ⇒ Object
- String
-
Sets the highlight fields (hl.fl) to request from Solr.
40 41 42 |
# File 'lib/search_params.rb', line 40 def hl_fl @hl_fl end |
#hl_snippets ⇒ Object
- Integer
-
Sets the number of hit highlights to request from Solr.
43 44 45 |
# File 'lib/search_params.rb', line 43 def hl_snippets @hl_snippets end |
#page ⇒ Object
- Integer
-
Page number to request from Solr.
19 20 21 |
# File 'lib/search_params.rb', line 19 def page @page end |
#page_size ⇒ Object
- Integer
-
Number of documents per page to request from Solr.
22 23 24 |
# File 'lib/search_params.rb', line 22 def page_size @page_size end |
#pf ⇒ Object
- String
-
Sets the pf value to pass to Solr.
53 54 55 |
# File 'lib/search_params.rb', line 53 def pf @pf end |
#q ⇒ Object
- String
-
The q value to pass to Solr.
10 11 12 |
# File 'lib/search_params.rb', line 10 def q @q end |
#qf ⇒ Object
- String
-
Sets the qf value to pass to Solr.
50 51 52 |
# File 'lib/search_params.rb', line 50 def qf @qf end |
#sort ⇒ Object
- String
-
Sort string to pass to Solr.
28 29 30 |
# File 'lib/search_params.rb', line 28 def sort @sort end |
#spellcheck ⇒ Object
- Bool
-
True to request Solr to use spellchecking (defaults to false).
34 35 36 |
# File 'lib/search_params.rb', line 34 def spellcheck @spellcheck end |
Class Method Details
.from_query_string(qs, facet_fields = []) ⇒ SolrLite::SearchParams
Creates a SearchParams object with the values in a query string. This is the inverse of ‘to_user_query_string()`.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/search_params.rb', line 282 def self.from_query_string(qs, facet_fields = []) params = SearchParams.new params.facets = facet_fields tokens = qs.split("&") tokens.each do |token| values = token.split("=") name = values[0] value = values[1] fq = nil next if value == nil || value.empty? case when name == "q" params.q = url_trim(value) when name == "rows" params.page_size = value.to_i when name == "page" params.page = value.to_i when name == "fq" || name.start_with?("fq_") # Query string contains fq when _we_ build the query string, for # example as the user clicks on different facets on the UI. # A query string can have multiple fq values. # # Query string contains fq_n when _Rails_ pushes HTML FORM values to # the query string. Rails does not like duplicate values in forms # and therefore we force them to be different by appending a number # to them (fq_1, f1_2, ...) fq = FilterQuery.from_query_string(value) if fq != nil params.fq << fq end end end params end |
.url_trim(value) ⇒ Object
Trims leading and trailing spaces from a URL escaped string and returns the string escaped.
319 320 321 322 323 |
# File 'lib/search_params.rb', line 319 def self.url_trim(value) return "" if value == nil trimmed = CGI.unescape(value).strip CGI.escape(trimmed) end |
Instance Method Details
#facet_for_field(field) ⇒ SolrLite::FacetField
Returns facet information about a given field.
87 88 89 |
# File 'lib/search_params.rb', line 87 def facet_for_field(field) @facets.find {|f| f.name == field} end |
#set_facet_remove_url(field, value, url) ⇒ Object
Sets the ‘remove_url` value for the given facet and value.
97 98 99 100 101 102 |
# File 'lib/search_params.rb', line 97 def set_facet_remove_url(field, value, url) facet = facet_for_field(field) if facet != nil facet.set_remove_url_for(value, url) end end |
#star_row=(start) ⇒ Object
Sets the starting row number and recalculates the current page based on the current page_size.
113 114 115 116 117 118 119 120 121 |
# File 'lib/search_params.rb', line 113 def star_row=(start) # recalculate the page if @page_size == 0 @page = 0 else @page = (start / @page_size) + 1 end nil end |
#start_row ⇒ Integer
Calculates the starting row number.
108 109 110 |
# File 'lib/search_params.rb', line 108 def start_row() (@page - 1) * @page_size end |
#to_form_values ⇒ Array
Returns an array of values that can be added to an HTML form to represent the current search parameters. Notice that we do NOT include the ‘q` parameter because there is typically an explicit HTML form value for it on the form.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/search_params.rb', line 250 def to_form_values() values = [] # We create an individual fq_n HTML form value for each # fq value because Rails does not like the same value on the form. @fq.each_with_index do |filter, i| values << {name: "fq_#{i}", value: filter.form_value} end values << {name: "rows", value: @page_size} if @page_size != DEFAULT_PAGE_SIZE values << {name: "page", value: @page} if @page != 1 # Don't surface this to the UI for now # (since we don't let the user change the sorting) # values << {name: "sort", value: @sort} if sort != "" values end |
#to_s ⇒ String
Returns a friendly string version of this object.
271 272 273 |
# File 'lib/search_params.rb', line 271 def to_s() "q=#{@q}\nfq=#{@fq}" end |
#to_solr_query_string(extra_fqs = []) ⇒ String
Calculates the query string that needs to be passed to Solr to issue a search with the current search parameters.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/search_params.rb', line 170 def to_solr_query_string(extra_fqs = []) qs = "" if @q != "" qs += "&q=#{@q}" end # Filter query @fq.each do |filter| qs += "&fq=#{filter.solr_value}" end extra_fqs.each do |filter| qs += "&fq=#{filter.solr_value}" end qs += "&rows=#{@page_size}" qs += "&start=#{start_row()}" if sort != "" qs += "&sort=#{CGI.escape(@sort)}" end if @qf != nil qs += "&qf=#{@qf}" end if @pf != nil qs += "&pf=#{@pf}" end if @spellcheck qs += "&spellcheck=on" end # Hit highlighting parameters if @hl qs += "&hl=true" if @hl_fl != nil qs += "&hl.fl=" + CGI.escape(@hl_fl) end if @hl_snippets > 1 qs += "&hl.snippets=#{@hl_snippets}" end end # Facets if @facets.count > 0 qs += "&facet=on" facet_ranges = @facets.select {|f| f.range == true }.map { |f| f.name } facet_ranges.each do |field_name| qs += "&facet.range=#{field_name}" end @facets.each do |f| qs += "&facet.field=#{f.name}" qs += "&f.#{f.name}.facet.mincount=1" if f.limit != nil qs += "&f.#{f.name}.facet.limit=#{f.limit}" elsif @facet_limit != nil qs += "&f.#{f.name}.facet.limit=#{@facet_limit}" end if f.range qs += "&f.#{f.name}.facet.range.start=#{f.range_start}" qs += "&f.#{f.name}.facet.range.end=#{f.range_end}" qs += "&f.#{f.name}.facet.range.gap=#{f.range_gap}" end end end qs end |
#to_user_query_string(facet_to_ignore = nil, q_override = nil) ⇒ String
Calculates the query string that we need render on the browser to execute a search with the current parameters.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/search_params.rb', line 134 def to_user_query_string(facet_to_ignore = nil, q_override = nil) qs = "" q_value = q_override != nil ? q_override : @q if q_value != "" && @q != "*" qs += "&q=#{@q}" end @fq.each do |filter| if facet_to_ignore != nil && filter.solr_value == facet_to_ignore.solr_value # don't add this to the query string else qs += "&fq=#{filter.qs_value}" end end qs += "&rows=#{@page_size}" if @page_size != DEFAULT_PAGE_SIZE qs += "&page=#{@page}" if @page != 1 # Don't surface this to the UI for now # (since we don't let the user change the sorting) # qs += "&sort=#{@sort}" if sort != "" qs end |
#to_user_query_string_no_q ⇒ String
Calculates the query string that we need render on the browser to execute a search with the current parameters and NO q parameter.
160 161 162 |
# File 'lib/search_params.rb', line 160 def to_user_query_string_no_q() to_user_query_string(nil, '') end |