Class: SolrLite::FilterQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_query.rb

Overview

Represents an “fq” in Solr. Field is the field to filter by and value the value to filter by. In a Solr query are represented as “fq=field:value”

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, values, is_range = false) ⇒ FilterQuery

Returns a new instance of FilterQuery.



11
12
13
14
15
16
17
# File 'lib/filter_query.rb', line 11

def initialize(field, values, is_range = false)
  if is_range
    init_from_range(field, values.first)
  else
    init_from_values(field, values)
  end
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



8
9
10
# File 'lib/filter_query.rb', line 8

def field
  @field
end

#form_valueObject

Returns the value of attribute form_value.



8
9
10
# File 'lib/filter_query.rb', line 8

def form_value
  @form_value
end

#qs_valueObject

Returns the value of attribute qs_value.



8
9
10
# File 'lib/filter_query.rb', line 8

def qs_value
  @qs_value
end

#remove_urlObject

Returns the value of attribute remove_url.



9
10
11
# File 'lib/filter_query.rb', line 9

def remove_url
  @remove_url
end

#solr_valueObject

Returns the value of attribute solr_value.



8
9
10
# File 'lib/filter_query.rb', line 8

def solr_value
  @solr_value
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/filter_query.rb', line 9

def title
  @title
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/filter_query.rb', line 8

def value
  @value
end

Class Method Details

.from_query_string(qs) ⇒ Object

qs is assumed to be the value taken from the query string in the form ‘field|value` or `field|value1|valueN`.

For range values the format is: ‘field^start,end`

Sometimes(*) the string comes URL encoded, for example:

`field|hello`
`field|hello%20world`

CGI.unespace handles these cases nicer than URL.decode

(*) Values coming from HTML forms submitted via HTTP POST tend to be encoded slighly different than value submitted via HTTP GET requests.

TODO: Should I remove support for multi-values

(e.g. `field|value1|valueN`) since we are
not using them? It will make the code here
and in init_from_values() cleaner.


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/filter_query.rb', line 49

def self.from_query_string(qs)
  is_range = false
  tokens = CGI.unescape(qs).split("|")
  if tokens.count < 2
    tokens = CGI.unescape(qs).split("^")
    return nil if tokens.count != 2
    is_range = true
  end
  field = ""
  values = []
  tokens.each_with_index do |token, i|
    if i == 0
      field = token
    else
      values << token
    end
  end
  FilterQuery.new(field, values, is_range)
end

Instance Method Details

#range_fromObject



19
20
21
22
23
# File 'lib/filter_query.rb', line 19

def range_from()
  tokens = (value || "").split(" - ")
  return nil if tokens.count != 2
  tokens[0].gsub("*","")
end

#range_toObject



25
26
27
28
29
# File 'lib/filter_query.rb', line 25

def range_to()
  tokens = (value || "").split(" - ")
  return nil if tokens.count != 2
  tokens[1].gsub("*","")
end