public
Fork of railsfreaks/acts_as_solr
Description: This plugin adds full text search capabilities and many other nifty features from Apacheā€˜s Solr to any Rails model.
Homepage: http://acts-as-solr.railsfreaks.com
Clone URL: git://github.com/jgp/acts_as_solr.git
Implicitly guess type based on column type if not explicitly set.  Also 
normalize configuration into hash similar to {field => {:type => type}}
tomafro (author)
Thu Jul 17 06:58:02 -0700 2008
commit  8bb34400a51f995a239c4396b9fa1f7784794e0a
tree    7d8906ce7b1aea808853201fe2746db9d7c12f41
parent  a497a0030a813d7157547a4818eb507105f5b07f
...
125
126
127
128
 
129
130
131
...
136
137
138
139
140
141
142
143
144
145
146
147
 
 
 
 
148
149
150
 
 
151
152
153
154
155
156
157
 
158
159
160
...
168
169
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
172
173
...
125
126
127
 
128
129
130
131
...
136
137
138
 
139
140
141
142
 
 
 
 
143
144
145
146
147
 
 
148
149
150
151
152
153
154
155
 
156
157
158
159
...
167
168
169
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
0
@@ -125,7 +125,7 @@ module ActsAsSolr #:nodoc:
0
       solr_configuration.update(solr_options) if solr_options.is_a?(Hash)
0
       Deprecation.validate_index(configuration)
0
       
0
- configuration[:solr_fields] = []
0
+ configuration[:solr_fields] = {}
0
       
0
       after_save :solr_save
0
       after_destroy :solr_destroy
0
@@ -136,25 +136,24 @@ module ActsAsSolr #:nodoc:
0
         process_fields(self.new.attributes.keys.map { |k| k.to_sym })
0
         process_fields(configuration[:additional_fields])
0
       end
0
-
0
     end
0
     
0
     private
0
     def get_field_value(field)
0
- configuration[:solr_fields] << field
0
- type = field.is_a?(Hash) ? field.values[0] : nil
0
- field = field.is_a?(Hash) ? field.keys[0] : field
0
- define_method("#{field}_for_solr".to_sym) do
0
+ field_name, options = determine_field_name_and_options(field)
0
+ configuration[:solr_fields][field_name] = options
0
+
0
+ define_method("#{field_name}_for_solr".to_sym) do
0
         begin
0
- value = self[field] || self.instance_variable_get("@#{field.to_s}".to_sym) || self.send(field.to_sym)
0
- case type
0
+ value = self[field_name] || self.instance_variable_get("@#{field_name.to_s}".to_sym) || self.send(field_name.to_sym)
0
+ case options[:type]
0
             # format dates properly; return nil for nil dates
0
             when :date: value ? value.utc.strftime("%Y-%m-%dT%H:%M:%SZ") : nil
0
             else value
0
           end
0
         rescue
0
           value = ''
0
- logger.debug "There was a problem getting the value for the field '#{field}': #{$!}"
0
+ logger.debug "There was a problem getting the value for the field '#{field_name}': #{$!}"
0
         end
0
       end
0
     end
0
@@ -168,5 +167,33 @@ module ActsAsSolr #:nodoc:
0
       end
0
     end
0
     
0
+ def determine_field_name_and_options(field)
0
+ if field.is_a?(Hash)
0
+ name = field.keys.first
0
+ options = field.values.first
0
+ if options.is_a?(Hash)
0
+ [name, {:type => type_for_field(field)}.merge(options)]
0
+ else
0
+ [name, {:type => options}]
0
+ end
0
+ else
0
+ [field, {:type => type_for_field(field)}]
0
+ end
0
+ end
0
+
0
+ def type_for_field(field)
0
+ if configuration[:facets] && configuration[:facets].include?(field)
0
+ :facet
0
+ elsif column = columns_hash[field.to_s]
0
+ case column.type
0
+ when :string then :text
0
+ when :datetime then :date
0
+ when :time then :date
0
+ else column.type
0
+ end
0
+ else
0
+ :text
0
+ end
0
+ end
0
   end
0
 end
0
\ No newline at end of file
...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
 
 
 
 
 
58
59
60
...
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
45
46
47
48
49
50
0
@@ -39,22 +39,12 @@ module ActsAsSolr #:nodoc:
0
               solr_configuration[:primary_key_field] => record_id(self).to_s}
0
 
0
       # iterate through the fields and add them to the document,
0
- configuration[:solr_fields].each do |field|
0
- field_name = field
0
- field_type = configuration[:facets] && configuration[:facets].include?(field) ? :facet : :text
0
- field_boost= solr_configuration[:default_boost]
0
-
0
- if field.is_a?(Hash)
0
- field_name = field.keys.pop
0
- if field.values.pop.respond_to?(:each_pair)
0
- attributes = field.values.pop
0
- field_type = get_solr_field_type(attributes[:type]) if attributes[:type]
0
- field_boost= attributes[:boost] if attributes[:boost]
0
- else
0
- field_type = get_solr_field_type(field.values.pop)
0
- field_boost= field[:boost] if field[:boost]
0
- end
0
- end
0
+ configuration[:solr_fields].each do |field_name, options|
0
+ #field_type = configuration[:facets] && configuration[:facets].include?(field) ? :facet : :text
0
+
0
+ field_boost = options[:boost] || solr_configuration[:default_boost]
0
+ field_type = get_solr_field_type(options[:type])
0
+
0
         value = self.send("#{field_name}_for_solr")
0
         value = set_value_if_nil(field_type) if value.to_s == ""
0
         
...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
 
 
 
108
109
110
...
91
92
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
95
96
97
98
99
100
0
@@ -91,20 +91,10 @@ module ActsAsSolr #:nodoc:
0
     # on the acts_as_solr call
0
     def replace_types(strings, include_colon=true)
0
       suffix = include_colon ? ":" : ""
0
- if configuration[:solr_fields] && configuration[:solr_fields].is_a?(Array)
0
- configuration[:solr_fields].each do |solr_field|
0
- field_type = get_solr_field_type(:text)
0
- if solr_field.is_a?(Hash)
0
- solr_field.each do |name,value|
0
- if value.respond_to?(:each_pair)
0
- field_type = get_solr_field_type(value[:type]) if value[:type]
0
- else
0
- field_type = get_solr_field_type(value)
0
- end
0
- field = "#{name.to_s}_#{field_type}#{suffix}"
0
- strings.each_with_index {|s,i| strings[i] = s.gsub(/#{name.to_s}_t#{suffix}/,field) }
0
- end
0
- end
0
+ if configuration[:solr_fields]
0
+ configuration[:solr_fields].each do |name, options|
0
+ field = "#{name.to_s}_#{get_solr_field_type(options[:type])}#{suffix}"
0
+ strings.each_with_index {|s,i| strings[i] = s.gsub(/#{name.to_s}_t#{suffix}/,field) }
0
         end
0
       end
0
       strings
...
2
3
4
 
 
5
6
7
...
2
3
4
5
6
7
8
9
0
@@ -2,6 +2,8 @@ class CreatePosts < ActiveRecord::Migration
0
   def self.up
0
     create_table :posts, :force => true do |t|
0
       t.column :name, :string
0
+ t.column :reply_counter, :integer
0
+ t.column :posted_at, :datetime
0
     end
0
   end
0
 
...
24
25
26
 
 
 
 
27
28
29
...
304
305
306
307
 
308
309
310
...
378
379
380
381
 
382
383
384
...
24
25
26
27
28
29
30
31
32
33
...
308
309
310
 
311
312
313
314
...
382
383
384
 
385
386
387
388
0
@@ -24,6 +24,10 @@ class ActsAsSolrTest < Test::Unit::TestCase
0
     assert_equal 0, Post.count_by_solr('aardvark')
0
   end
0
 
0
+ def test_type_determined_from_database_if_not_explicitly_set
0
+ assert Post.configuration[:solr_fields][:posted_at][:type] == :date
0
+ end
0
+
0
   # Testing basic solr search:
0
   # Model.find_by_solr 'term'
0
   # Note that you're able to mix free-search with fields and boolean operators
0
@@ -304,7 +308,7 @@ class ActsAsSolrTest < Test::Unit::TestCase
0
     
0
     books.records.each { |book| assert_not_nil book.solr_score }
0
     assert_equal 0.52808195, books.docs.first.solr_score
0
- assert_equal 0.2428928, books.docs.last.solr_score
0
+ assert_equal 0.25081474, books.docs.last.solr_score
0
   end
0
   
0
   # Making sure nothing breaks when html entities are inside
0
@@ -378,7 +382,7 @@ class ActsAsSolrTest < Test::Unit::TestCase
0
     
0
     books = Book.find_by_solr 'ruby^10 OR splinter', {:scores => true, :order => 'score desc' }
0
     assert_equal 0.52808195, books.docs.first.solr_score
0
- assert_equal 0.2428928, books.docs.last.solr_score
0
+ assert_equal 0.25081474, books.docs.last.solr_score
0
   end
0
   
0
   # Search based on fields with the :date format

Comments

    No one has commented yet.