sander6-enygma 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/enygma/configuration.rb +33 -12
- data/lib/enygma/search.rb +58 -7
- data/lib/enygma/version.rb +1 -1
- metadata +1 -1
data/lib/enygma/configuration.rb
CHANGED
@@ -111,18 +111,20 @@ module Enygma
|
|
111
111
|
self.instance_eval(&config)
|
112
112
|
end
|
113
113
|
|
114
|
-
attr_reader :
|
114
|
+
attr_reader :indexes, :weights, :index_weights, :latitude, :longitude
|
115
115
|
|
116
116
|
def initialize(attributes = {}, &block)
|
117
|
-
@adapter
|
118
|
-
@table
|
119
|
-
@indexes
|
120
|
-
@target_attr
|
121
|
-
@match_mode
|
122
|
-
@
|
123
|
-
@
|
124
|
-
@
|
125
|
-
@
|
117
|
+
@adapter = @@adapter
|
118
|
+
@table = nil
|
119
|
+
@indexes = []
|
120
|
+
@target_attr = @@target_attr
|
121
|
+
@match_mode = :all
|
122
|
+
@fragment_mode = :exact
|
123
|
+
@weights = {}
|
124
|
+
@index_weights = {}
|
125
|
+
@latitude = 'lat'
|
126
|
+
@longitude = 'lng'
|
127
|
+
@key_prefix = nil
|
126
128
|
attributes.each do |name, value|
|
127
129
|
self.__send__(name, value)
|
128
130
|
end
|
@@ -178,8 +180,12 @@ module Enygma
|
|
178
180
|
return @table
|
179
181
|
end
|
180
182
|
|
181
|
-
def weight(
|
182
|
-
@weights
|
183
|
+
def weight(weights = {})
|
184
|
+
@weights = weights
|
185
|
+
end
|
186
|
+
|
187
|
+
def weight_index(indexes = {})
|
188
|
+
@index_weights = indexes
|
183
189
|
end
|
184
190
|
|
185
191
|
def index(index)
|
@@ -191,12 +197,27 @@ module Enygma
|
|
191
197
|
return @match_mode if mode.nil?
|
192
198
|
@match_mode = mode
|
193
199
|
end
|
200
|
+
|
201
|
+
def fragment_mode(mode = nil)
|
202
|
+
return @fragment_mode if mode.nil?
|
203
|
+
@fragment_mode = mode
|
204
|
+
end
|
194
205
|
|
195
206
|
def target_attr(name = nil)
|
196
207
|
return @target_attr if name.nil?
|
197
208
|
@target_attr = name
|
198
209
|
end
|
199
210
|
|
211
|
+
def latitude_attr(name = nil)
|
212
|
+
return @latitude if name.nil?
|
213
|
+
@latitude = name
|
214
|
+
end
|
215
|
+
|
216
|
+
def longitude_attr(name = nil)
|
217
|
+
return @longitude if name.nil?
|
218
|
+
@latitude = name
|
219
|
+
end
|
220
|
+
|
200
221
|
def sphinx
|
201
222
|
@@sphinx
|
202
223
|
end
|
data/lib/enygma/search.rb
CHANGED
@@ -53,6 +53,18 @@ module Enygma
|
|
53
53
|
:extended => Sphinx::Client::SPH_SORT_EXTENDED,
|
54
54
|
:expression => Sphinx::Client::SPH_SORT_EXPR
|
55
55
|
}
|
56
|
+
|
57
|
+
class InvalidFragementMatchingScheme < StandardError; end
|
58
|
+
|
59
|
+
frag_proc = Proc.new { |s| raise InvalidFragementMatchingScheme }
|
60
|
+
FRAGMENT_MODES = Hash.new(frag_proc).merge({
|
61
|
+
:exact => Proc.new { |s| s },
|
62
|
+
:start => Proc.new { |s| s.split(/\s+/).collect {|f| f + '*'}.join(' ') },
|
63
|
+
:end => Proc.new { |s| s.split(/\s+/).collect {|f| '*' + f }.join(' ') },
|
64
|
+
:fragment => Proc.new { |s| s.split(/\s+/).collect {|f| '*' + f + '*' }.join(' ') },
|
65
|
+
:fuzzy => Proc.new { |s| '*' + s.delete(' ').split(//).join('*') + '*' },
|
66
|
+
:textmate => Proc.new { |s| '*' + s.delete(' ').split(//).join('*') + '*' }
|
67
|
+
})
|
56
68
|
|
57
69
|
def initialize(*args, &block)
|
58
70
|
overrides = args.last.is_a?(Hash) ? args.pop : {}
|
@@ -71,11 +83,12 @@ module Enygma
|
|
71
83
|
}
|
72
84
|
@sphinx = Sphinx::Client.new
|
73
85
|
|
74
|
-
@indexes
|
75
|
-
@term
|
76
|
-
@target_attr
|
77
|
-
@match_mode
|
78
|
-
@
|
86
|
+
@indexes = config.indexes
|
87
|
+
@term = overrides[:term] || ""
|
88
|
+
@target_attr = config.target_attr
|
89
|
+
@match_mode = MATCH_MODES[config.match_mode]
|
90
|
+
@fragment_mode = config.fragment_mode
|
91
|
+
@key_prefix = config.key_prefix || ''
|
79
92
|
|
80
93
|
@latitude = config.latitude
|
81
94
|
@longitude = config.longitude
|
@@ -85,6 +98,13 @@ module Enygma
|
|
85
98
|
@max = @sphinx.instance_variable_get(:@maxmatches)
|
86
99
|
@cutoff = @sphinx.instance_variable_get(:@cutoff)
|
87
100
|
|
101
|
+
@weights = config.weights.inject({}) { |weights, (attr, weight)| weights.merge({ attr.to_s => weight })} || {}
|
102
|
+
@index_weights = config.index_weights.inject({}) { |weights, (index, weight)| weights.merge({ Enygma.indexify(index) => weight })} || {}
|
103
|
+
@sphinx.SetFieldWeights(@weights)
|
104
|
+
@sphinx.SetIndexWeights(@index_weights)
|
105
|
+
|
106
|
+
@fields = []
|
107
|
+
|
88
108
|
@return_attributes = []
|
89
109
|
|
90
110
|
@sphinx.SetServer(config.sphinx[:host], config.sphinx[:port])
|
@@ -113,6 +133,11 @@ module Enygma
|
|
113
133
|
self
|
114
134
|
end
|
115
135
|
|
136
|
+
def in_fields(*fields)
|
137
|
+
@fields = fields
|
138
|
+
self
|
139
|
+
end
|
140
|
+
|
116
141
|
def using_match_mode(match_mode)
|
117
142
|
@match_mode = MATCH_MODES[match_mode]
|
118
143
|
@sphinx.SetMatchMode(@match_mode)
|
@@ -124,7 +149,12 @@ module Enygma
|
|
124
149
|
self
|
125
150
|
end
|
126
151
|
alias_method :using_index, :using_indexes
|
127
|
-
|
152
|
+
|
153
|
+
def using_fragment_matching(mode)
|
154
|
+
@fragment_mode = mode
|
155
|
+
self
|
156
|
+
end
|
157
|
+
|
128
158
|
def filter(attribute, values, exclude = false)
|
129
159
|
attribute = attribute.to_s
|
130
160
|
case values
|
@@ -203,6 +233,18 @@ module Enygma
|
|
203
233
|
set_limits
|
204
234
|
self
|
205
235
|
end
|
236
|
+
|
237
|
+
def weight(weights = {})
|
238
|
+
@weights.merge!(weights.inject({}) { |weights, (attr, weight)| weights.merge({ attr.to_s => weight })})
|
239
|
+
@sphinx.SetFieldWeights(@weights)
|
240
|
+
self
|
241
|
+
end
|
242
|
+
|
243
|
+
def weight_index(weights = {})
|
244
|
+
@index_weights.merge!(weights.inject({}) { |weights, (index, weight)| weights.merge({ Enygma.indexify(index) => weight })})
|
245
|
+
@sphinx.SetIndexWeights(@index_weights)
|
246
|
+
self
|
247
|
+
end
|
206
248
|
|
207
249
|
def within(distance)
|
208
250
|
Enygma::GeoDistanceProxy.new(self, distance)
|
@@ -237,7 +279,10 @@ module Enygma
|
|
237
279
|
end
|
238
280
|
|
239
281
|
def query_sphinx
|
240
|
-
|
282
|
+
query_string = mutate_query_for_field_searching(@term)
|
283
|
+
query_string = FRAGMENT_MODES[@fragment_mode][query_string]
|
284
|
+
query_indexes = @indexes.join(', ')
|
285
|
+
sphinx_response = @sphinx.Query(query_string, query_indexes)
|
241
286
|
raise InvalidSphinxQuery unless sphinx_response
|
242
287
|
sphinx_response
|
243
288
|
end
|
@@ -246,5 +291,11 @@ module Enygma
|
|
246
291
|
ids = results['matches'].collect { |match| match['attrs'][@target_attr] }.uniq
|
247
292
|
@database[:adapter].query(:table => @database[:table], :ids => ids, :key_prefix => @key_prefix)
|
248
293
|
end
|
294
|
+
|
295
|
+
def mutate_query_for_field_searching(query)
|
296
|
+
return query if @fields.empty?
|
297
|
+
self.using_match_mode(:extended2)
|
298
|
+
"@(#{@fields.collect {|f| f.to_s}.join(',')}) #{query}"
|
299
|
+
end
|
249
300
|
end
|
250
301
|
end
|
data/lib/enygma/version.rb
CHANGED