mwmitchell-rsolr-ext 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rsolr-ext.rb CHANGED
@@ -15,11 +15,12 @@ module RSolr
15
15
 
16
16
  module Ext
17
17
 
18
- VERSION = '0.5.0'
18
+ VERSION = '0.5.1'
19
19
 
20
20
  autoload :Request, 'rsolr-ext/request.rb'
21
21
  autoload :Response, 'rsolr-ext/response.rb'
22
22
  autoload :HashMethodizer, 'rsolr-ext/hash_methodizer.rb'
23
+ autoload :Mapable, 'rsolr-ext/mapable.rb'
23
24
 
24
25
  end
25
26
 
@@ -1,91 +1,59 @@
1
1
  module RSolr::Ext::Request
2
2
 
3
- # A module that provides method mapping capabilities.
4
- # The basic idea is to pass in a hash to the #map method,
5
- # the map method then goes through a list of keys to
6
- # be processed. Each key name can match a key in the input hash.
7
- # If there is a match, a method by the name of "map_#{key}" is
8
- # called with the following args: input[key], output_hash
9
- # The method is responsible for processing the value.
10
- # The return value from the method does nothing.
11
- #
12
- # For example: if the mapped params list has a name, :query,
13
- # there should be a method like: map_query(input_value, output_hash)
14
- module Mapable
3
+ autoload :Queryable, 'rsolr-ext/request/queryable.rb'
4
+
5
+ class Standard
15
6
 
16
- # accepts an input hash.
17
- # prepares a return hash by copying the input.
18
- # runs through all of the keys in MAPPED_PARAMS.
19
- # calls any mapper methods that match the current key in MAPPED_PARAMS.
20
- # The mapped keys from the input hash are deleted.
21
- # returns a new hash.
22
- def map(input)
23
- result = input.dup
24
- self.class::MAPPED_PARAMS.each do |meth|
25
- input_value = result.delete(meth)
26
- next if input_value.to_s.empty?
27
- send("map_#{meth}", input_value, result)
28
- end
29
- result
30
- end
7
+ include RSolr::Ext::Mapable
8
+ include RSolr::Ext::Request::Queryable
31
9
 
32
- # creates an array where the "existing_value" param is first
33
- # and the "new_value" is the last.
34
- # All empty/nil items are removed.
35
- # the return result is either the result of the
36
- # array being joined on a space, or the array itself.
37
- # "auto_join" should be true or false.
38
- def append_to_param(existing_value, new_value, auto_join=true)
39
- values = [existing_value, new_value]
40
- values.delete_if{|v|v.nil?}
41
- auto_join ? values.join(' ') : values
10
+ MAPPED_PARAMS = [
11
+ :per_page,
12
+ :page,
13
+ :phrases, # quoted q param
14
+ :filters, # fq params
15
+ :phrase_filters, # quoted fq params,
16
+ :facets
17
+ ]
18
+
19
+ def map_per_page(value,output)
20
+ output[:rows] = value.to_i
42
21
  end
43
-
44
- end
45
-
46
- # a module to help the creation of solr queries.
47
- module Queryable
48
-
49
- # Wraps a string around double quotes
50
- def quote(value)
51
- %("#{value}")
22
+
23
+ def map_page(value,output)
24
+ raise ':per_page must be set when using :page' unless output[:rows]
25
+ page = value.to_s.to_i-1
26
+ page = page < 1 ? 0 : page
27
+ output[:start] = page * output[:rows]
52
28
  end
53
-
54
- # builds a solr range query from a Range object
55
- def build_range(r)
56
- "[#{r.min} TO #{r.max}]"
29
+
30
+ def map_phrases(value,output)
31
+ output[:q] = append_to_param(output[:q], build_query(value, true))
32
+ end
33
+
34
+ def map_filters(value,output)
35
+ output[:fq] = append_to_param(output[:fq], build_query(value))
36
+ end
37
+
38
+ def map_phrase_filters(value,output)
39
+ output[:fq] = append_to_param(output[:fq], build_query(value, true))
57
40
  end
58
41
 
59
- # builds a solr query fragment
60
- # if "quote_string" is true, the values will be quoted.
61
- # if "value" is a string/symbol, the #to_s method is called
62
- # if the "value" is an array, each item in the array is
63
- # send to build_query (recursive)
64
- # if the "value" is a Hash, a fielded query is built
65
- # where the keys are used as the field names and
66
- # the values are either processed as a Range or
67
- # passed back into build_query (recursive)
68
- def build_query(value, quote_string=false)
69
- case value
70
- when String,Symbol
71
- return quote_string ? quote(value.to_s) : value.to_s
72
- when Array
73
- value.collect do |v|
74
- build_query(v, quote_string)
75
- end.flatten
76
- when Hash
77
- return value.collect do |(k,v)|
78
- if v.is_a?(Range)
79
- "#{k}:#{build_range(v)}"
80
- else
81
- "#{k}:#{build_query(v, quote_string)}"
82
- end
83
- end.flatten
42
+ def map_facets(value,output)
43
+ output[:facet] = true
44
+ if value[:fields]
45
+ fields = value[:fields].is_a?(Array) ? value[:fields] : [value[:fields]]
46
+ fields.each do |f|
47
+ output['facet.field'] ||= []
48
+ output['facet.field'] << f
49
+ end
84
50
  end
85
51
  end
52
+
86
53
  end
87
54
 
88
- autoload :Standard, 'rsolr-ext/request/standard.rb'
89
- autoload :Dismax, 'rsolr-ext/request/dismax.rb'
55
+ class Dismax < Standard
56
+
57
+ end
90
58
 
91
59
  end
data/rsolr-ext.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rsolr-ext"
3
- s.version = "0.5.0"
3
+ s.version = "0.5.1"
4
4
  s.date = "2009-03-17"
5
5
  s.summary = "An extension lib for RSolr"
6
6
  s.email = "goodieboy@gmail.com"
data/test/request_test.rb CHANGED
@@ -11,12 +11,15 @@ class RSolrExtRequestTest < Test::Unit::TestCase
11
11
  :phrases=>{:name=>'This is a phrase'},
12
12
  :filters=>['test', {:price=>(1..10)}],
13
13
  :phrase_filters=>{:manu=>['Apple']},
14
- :q=>'ipod'
14
+ :q=>'ipod',
15
+ :facets=>{:fields=>['cat', 'blah']}
15
16
  )
16
17
  assert_equal "test price:[1 TO 10] manu:\"Apple\"", solr_params[:fq]
17
18
  assert_equal 10, solr_params[:start]
18
19
  assert_equal 10, solr_params[:rows]
19
20
  assert_equal "ipod name:\"This is a phrase\"", solr_params[:q]
21
+ assert_equal ['cat', 'blah'], solr_params['facet.field']
22
+ assert_equal true, solr_params[:facet]
20
23
  end
21
24
 
22
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mwmitchell-rsolr-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Mitchell
@@ -1,3 +0,0 @@
1
- class RSolr::Ext::Request::Dismax < RSolr::Ext::Request::Standard
2
-
3
- end
@@ -1,38 +0,0 @@
1
- class RSolr::Ext::Request::Standard
2
-
3
- include RSolr::Ext::Request::Mapable
4
- include RSolr::Ext::Request::Queryable
5
-
6
- MAPPED_PARAMS = [
7
- :per_page,
8
- :page,
9
- :phrases, # quoted q param
10
- :filters, # fq params
11
- :phrase_filters # quoted fq params
12
- ]
13
-
14
- def map_per_page(value,output)
15
- output[:rows] = value.to_i
16
- end
17
-
18
- def map_page(value,output)
19
- raise ':per_page must be set when using :page' unless output[:rows]
20
- page = value.to_s.to_i-1
21
- page = page < 1 ? 0 : page
22
- output[:start] = page * output[:rows]
23
- end
24
-
25
- def map_phrases(value,output)
26
- output[:q] = append_to_param(output[:q], build_query(value, true))
27
- end
28
-
29
- def map_filters(value,output)
30
- output[:fq] = append_to_param(output[:fq], build_query(value))
31
- end
32
-
33
- def map_phrase_filters(value,output)
34
- output[:fq] = append_to_param(output[:fq], build_query(value, true))
35
- end
36
-
37
-
38
- end