smart_search 0.0.96 → 0.0.97

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/smart_search.rb +106 -94
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2eea1ae08ee42a272bb6c46e9be0879bdc998e45
4
- data.tar.gz: eb172e38f3cfac374fc8f2dc7f42a0cee93ee152
3
+ metadata.gz: c97bb0dff9b1f7173a7598a8f34999a6314da64f
4
+ data.tar.gz: f94b277431d1ac6b6a0e13e822fdec73636f9ac3
5
5
  SHA512:
6
- metadata.gz: 10d57223b4205896105c73232d15501d2d28f54de3ea6f95f223e444c61d027751ef499b40e03be6ba2eb0587851ed51b0fac509c9fe934b054a123571122969
7
- data.tar.gz: 6938b923f45869af837e34c333f3105cc8e3da3577373bf4c3942253b085ed73aa1a54899aea64245655a886ca832ebc568b8da5651220ccb60c52fc49310962
6
+ metadata.gz: c5292dbce629bba65a4e58ef2434fbcb772aec3f41b7f331d90e85bf2bca9dc2ea3e8ba80cee85d8df3cf7640499a9923869aa6eff1ceb4212a80271fdbbd25d
7
+ data.tar.gz: daa9ba025fabc9a3410f1f182f16d7ec4bcd26d4c9db5f637f50e3b957dad971a191a8def6ad3a6b8d796492ae7dff0c26dc841fea172943e8dd1ac7f7b1c2f1
data/lib/smart_search.rb CHANGED
@@ -31,24 +31,24 @@ module SmartSearch
31
31
 
32
32
  cattr_accessor :condition_default, :group_default, :tags, :order_default, :enable_similarity, :default_template_path
33
33
  send :include, InstanceMethods
34
- self.send(:after_save, :create_search_tags, :if => :update_search_tags?) unless options[:auto] == false
35
- self.send(:before_destroy, :clear_search_tags)
36
- self.enable_similarity ||= true
37
-
38
- attr_accessor :query_score, :dont_update_search_tags
39
-
40
- # options zuweisen
41
- if options[:conditions].is_a?(String) && !options[:conditions].blank?
42
- self.condition_default = options[:conditions]
43
- elsif !options[:conditions].nil?
44
- raise ArgumentError, ":conditions must be a valid SQL Query"
45
- else
46
- self.condition_default = nil
47
- end
34
+ self.send(:after_save, :create_search_tags, :if => :update_search_tags?) unless options[:auto] == false
35
+ self.send(:before_destroy, :clear_search_tags)
36
+ self.enable_similarity ||= true
37
+
38
+ attr_accessor :query_score, :dont_update_search_tags
39
+
40
+ # options zuweisen
41
+ if options[:conditions].is_a?(String) && !options[:conditions].blank?
42
+ self.condition_default = options[:conditions]
43
+ elsif !options[:conditions].nil?
44
+ raise ArgumentError, ":conditions must be a valid SQL Query"
45
+ else
46
+ self.condition_default = nil
47
+ end
48
48
 
49
- self.order_default = options[:order]
49
+ self.order_default = options[:order]
50
50
 
51
- self.tags = options[:on] || []
51
+ self.tags = options[:on] || []
52
52
  end
53
53
  end
54
54
  end
@@ -93,89 +93,91 @@ module SmartSearch
93
93
  # Load ranking from Search tags
94
94
  result_ids = []
95
95
  result_scores = {}
96
- SmartSearchTag.connection.select_all("select entry_id, sum(boost) as score, group_concat(search_tags) as grouped_tags
96
+ SmartSearchTag.connection.select_all("select entry_id, sum(boost) as score, group_concat(search_tags) as grouped_tags
97
97
  from smart_search_tags where `table_name`= '#{self.table_name}' and
98
98
 
99
99
  (#{tags.join(' OR ')}) group by entry_id having (#{tags.join(' AND ').gsub('search_tags', 'grouped_tags')}) order by score DESC").each do |r|
100
- result_ids << r["entry_id"].to_i
101
- result_scores[r["entry_id"].to_i] = r['score'].to_f
102
- end
103
-
104
- # Enable unscoped searching
105
- if options[:unscoped] == true
106
- results = self.unscoped.where(:id => result_ids)
107
- else
108
- results = self.where(:id => result_ids)
109
- end
100
+ result_ids << r["entry_id"].to_i
101
+ result_scores[r["entry_id"].to_i] = r['score'].to_f
102
+ end
110
103
 
111
- if options[:conditions]
112
- results = results.where(options[:conditions])
113
- end
104
+ # Enable unscoped searching
105
+ if options[:unscoped] == true
106
+ results = self.unscoped.where(:id => result_ids)
107
+ else
108
+ results = self.where(:id => result_ids)
109
+ end
114
110
 
115
- if !self.condition_default.blank?
116
- results = results.where(self.condition_default)
117
- end
111
+ if options[:conditions]
112
+ results = results.where(options[:conditions])
113
+ end
118
114
 
119
- if options[:group]
120
- results = results.group(options[:group])
121
- end
115
+ if !self.condition_default.blank?
116
+ results = results.where(self.condition_default)
117
+ end
122
118
 
123
- if options[:order] || self.order_default
124
- results = results.order(options[:order] || self.order_default)
125
- else
126
- ordered_results = []
127
- results.each do |r|
128
- r.query_score = result_scores[r.id]
129
- ordered_results[result_ids.index(r.id)] = r
130
- end
119
+ if options[:group]
120
+ results = results.group(options[:group])
121
+ end
131
122
 
132
- results = ordered_results.compact
123
+ if options[:order] || self.order_default
124
+ results = results.order(options[:order] || self.order_default)
125
+ else
126
+ ordered_results = []
127
+ results.each do |r|
128
+ r.query_score = result_scores[r.id]
129
+ ordered_results[result_ids.index(r.id)] = r
133
130
  end
134
131
 
135
- return results
136
- else
137
- raise "#{self.inspect} is not a SmartSearch"
132
+ results = ordered_results.compact
138
133
  end
139
- end
140
134
 
141
- # reload search_tags for entire table based on the attributes defined in ':on' option passed to the 'smart_search' method
142
- def set_search_index
143
- s = self.all.size.to_f
144
- self.all.each_with_index do |a, i|
145
- a.create_search_tags
146
- done = ((i+1).to_f/s)*100
147
- end
135
+ return results
136
+ else
137
+ raise "#{self.inspect} is not a SmartSearch"
148
138
  end
139
+ end
149
140
 
150
- # Load all search tags for this table into similarity index
151
- def set_similarity_index
152
- search_tags_list = self.connection.select_all("SELECT search_tags from #{SmartSearchTag.table_name} where `table_name` = #{self.table_name}").map {|r| r["search_tags"]}
153
-
154
- SmartSimilarity.create_from_text(search_tags_list.join(" "))
141
+ # reload search_tags for entire table based on the attributes defined in ':on' option passed to the 'smart_search' method
142
+ def set_search_index
143
+ s = self.all.size.to_f
144
+ self.all.each_with_index do |a, i|
145
+ a.create_search_tags
146
+ done = ((i+1).to_f/s)*100
155
147
  end
148
+ end
149
+
150
+ # Load all search tags for this table into similarity index
151
+ def set_similarity_index
152
+ search_tags_list = self.connection.select_all("SELECT search_tags from #{SmartSearchTag.table_name} where `table_name` = #{self.table_name}").map {|r| r["search_tags"]}
156
153
 
154
+ SmartSimilarity.create_from_text(search_tags_list.join(" "))
157
155
  end
158
156
 
159
- # Instance Methods for ActiveRecord
160
- module InstanceMethods
157
+ end
161
158
 
162
- # Load the result template path for this instance
163
- def result_template_path
164
- self.class.result_template_path
165
- end
159
+ # Instance Methods for ActiveRecord
160
+ module InstanceMethods
166
161
 
167
- def dont_update_search_tags!
168
- self.dont_update_search_tags = true
169
- end
162
+ # Load the result template path for this instance
163
+ def result_template_path
164
+ self.class.result_template_path
165
+ end
170
166
 
171
- def update_search_tags?
172
- !self.dont_update_search_tags
173
- end
167
+ def dont_update_search_tags!
168
+ self.dont_update_search_tags = true
169
+ end
174
170
 
175
- # create search tags for this very record based on the attributes defined in ':on' option passed to the 'Class.smart_search' method
176
- def create_search_tags
177
- tags = []
171
+ def update_search_tags?
172
+ !self.dont_update_search_tags
173
+ end
178
174
 
175
+ # create search tags for this very record based on the attributes defined in ':on' option passed to the 'Class.smart_search' method
176
+ def create_search_tags
177
+ # storing tags must never fail the systems
178
+ begin
179
+ tags = []
180
+
179
181
  self.class.tags.each do |tag|
180
182
 
181
183
  if !tag.is_a?(Hash)
@@ -228,37 +230,47 @@ module SmartSearch
228
230
  end
229
231
  end
230
232
  end
231
-
233
+
234
+ rescue Exception => e
235
+ Rails.logger.error "SMART SEARCH FAILED TO TO STORE SEARCH TAGS #{self.class.name} #{self.id}"
236
+ Rails.logger.error e.message
237
+ Rails.logger.error puts e.backtrace
232
238
  end
239
+
240
+
233
241
 
234
- # Remove search data for the instance from the index
235
- def clear_search_tags
236
- if !self.id.nil?
237
- SmartSearchTag.connection.execute("DELETE from #{SmartSearchTag.table_name} where `table_name` = '#{self.class.table_name}' and entry_id = #{self.id}") rescue nil
238
- end
239
- end
242
+
240
243
 
241
244
  end
242
245
 
246
+ # Remove search data for the instance from the index
247
+ def clear_search_tags
248
+ if !self.id.nil?
249
+ SmartSearchTag.connection.execute("DELETE from #{SmartSearchTag.table_name} where `table_name` = '#{self.class.table_name}' and entry_id = #{self.id}") rescue nil
250
+ end
251
+ end
252
+
253
+ end
243
254
 
244
- class Config
245
255
 
246
- cattr_accessor :search_models
247
- cattr_accessor :public_models
256
+ class Config
248
257
 
249
- self.search_models = []
250
- self.public_models = []
258
+ cattr_accessor :search_models
259
+ cattr_accessor :public_models
251
260
 
252
- def self.get_search_models
253
- self.search_models.map {|m| m.constantize}
254
- end
261
+ self.search_models = []
262
+ self.public_models = []
255
263
 
256
- def self.get_public_models
257
- self.public_models.map {|m| m.constantize}
258
- end
264
+ def self.get_search_models
265
+ self.search_models.map {|m| m.constantize}
266
+ end
259
267
 
268
+ def self.get_public_models
269
+ self.public_models.map {|m| m.constantize}
260
270
  end
261
271
 
272
+ end
273
+
262
274
 
263
275
  end
264
276
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.96
4
+ version: 0.0.97
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Eck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-06 00:00:00.000000000 Z
11
+ date: 2018-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails