ximate 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +32 -5
- data/lib/ximate.rb +2 -2
- data/lib/ximate/activerecord/relation.rb +0 -7
- data/lib/ximate/search.rb +5 -6
- data/lib/ximate/version.rb +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
ApproXIMATE fuzzy search for Ruby on Rails activerecord models.
|
4
4
|
|
5
|
-
|
5
|
+
=== Requirements
|
6
6
|
|
7
7
|
* Rails >= 3.0.1
|
8
8
|
|
9
9
|
|
10
|
-
|
10
|
+
=== Installation
|
11
11
|
|
12
12
|
gem install ximate
|
13
13
|
|
@@ -16,7 +16,7 @@ ApproXIMATE fuzzy search for Ruby on Rails activerecord models.
|
|
16
16
|
In your model puts some like this:
|
17
17
|
|
18
18
|
class Post < ActiveRecord::Base
|
19
|
-
define_index
|
19
|
+
define_index do
|
20
20
|
add_text title, 3
|
21
21
|
add_text keywords.join(' '), 2
|
22
22
|
add_text body(:en)
|
@@ -26,17 +26,44 @@ In your model puts some like this:
|
|
26
26
|
In this case we can perform an appoximate search on title, keywords and body fields of posts
|
27
27
|
where title is most important than keywords than body (title have priority 3, keywords 2 and body 1).
|
28
28
|
|
29
|
+
You can also define indexes for different locales like this:
|
30
|
+
|
31
|
+
class Post < ActiveRecord::Base
|
32
|
+
[:en, :it, :de].each do |locale|
|
33
|
+
define_index(locale) do
|
34
|
+
add_text title(locale)
|
35
|
+
add_text body(locale)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
29
40
|
Then you can perform a search
|
30
41
|
|
31
42
|
Post.asearch('Hello world').where(:public => true).limit(5)
|
32
43
|
|
44
|
+
+asearch+ method is chainable with the usual activerecord methods.
|
33
45
|
You can also order the results by rank (calculate by ximate)
|
34
46
|
|
35
|
-
Post.asearch('Hello world').order('rank DESC').where(:public => true)
|
47
|
+
Post.asearch('Hello world').order('rank DESC').where(:public => true)
|
48
|
+
|
49
|
+
The cool stuff is that if you search with
|
50
|
+
|
51
|
+
Post.asearch('Hello worlds').order('rank DESC').where(:public => true)
|
52
|
+
|
53
|
+
the results will be the same!
|
54
|
+
The error allowed by Ximate can be expressed as a percentage by setting the option <tt>Ximate::OPTIONS[:match_error_percent]</tt>.
|
55
|
+
Default value is 20%.
|
56
|
+
|
57
|
+
Finally you can set some Ximate options in your initializers.
|
58
|
+
|
59
|
+
Ximate::OPTIONS[:match_error_percent] = 20
|
60
|
+
Ximate::OPTIONS[:ignore_word_short_than] = 2
|
61
|
+
Ximate::OPTIONS[:logger] = true
|
62
|
+
Ximate::OPTIONS[:debug] = false
|
36
63
|
|
37
64
|
== Questions or problems?
|
38
65
|
|
39
|
-
If you have any issues
|
66
|
+
If you have any issues please add an {issue on
|
40
67
|
GitHub}[https://github.com/pioz/ximate/issues] or fork the project and
|
41
68
|
send a pull request.
|
42
69
|
|
data/lib/ximate.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'ximate/search'
|
2
|
+
require 'ximate/activerecord/relation'
|
3
3
|
require File.join(File.dirname(__FILE__), '../ext/fuzzy_search')
|
4
4
|
|
5
5
|
ActiveRecord::Base.send(:include, Ximate)
|
@@ -5,7 +5,6 @@ module ActiveRecord
|
|
5
5
|
|
6
6
|
alias_method :orig_initialize, :initialize
|
7
7
|
alias_method :orig_order, :order
|
8
|
-
# alias_method :orig_to_a, :to_a
|
9
8
|
|
10
9
|
def initialize(klass, table)
|
11
10
|
@ranks = {}
|
@@ -28,12 +27,6 @@ module ActiveRecord
|
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
31
|
-
# def to_a
|
32
|
-
# return orig_to_a if @ranks.empty?
|
33
|
-
# orig_to_a.sort do |x, y|
|
34
|
-
# @ranks[y.id] <=> @ranks[x.id]
|
35
|
-
# end
|
36
|
-
# end
|
37
30
|
end
|
38
31
|
|
39
32
|
end
|
data/lib/ximate/search.rb
CHANGED
@@ -13,8 +13,8 @@ module Ximate
|
|
13
13
|
|
14
14
|
module Search
|
15
15
|
def define_index(locale = I18n.default_locale, &block)
|
16
|
-
if ActiveRecord::Base.connection.tables.include?(self.
|
17
|
-
table = self.
|
16
|
+
if ActiveRecord::Base.connection.tables.include?(self.table_name)
|
17
|
+
table = self.table_name.to_sym
|
18
18
|
DATA[locale.to_sym] ||= {}
|
19
19
|
DATA[locale.to_sym][table] ||= {}
|
20
20
|
|
@@ -36,7 +36,7 @@ module Ximate
|
|
36
36
|
module ClassMethods
|
37
37
|
|
38
38
|
def asearch(pattern)
|
39
|
-
table = self.
|
39
|
+
table = self.table_name.to_sym
|
40
40
|
matches = {} # {id => rank, id => rank}
|
41
41
|
lastsearch = {} # Save last 'e' search for every word in pattern to avoid multi-search of the same word
|
42
42
|
pattern.split(' ').each { |w| lastsearch[w] = -1 }
|
@@ -55,7 +55,6 @@ module Ximate
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
|
-
#return select('*, 0 AS RANK').where('1 = 0') if matches.empty?
|
59
58
|
return where('1 = 0') if matches.empty?
|
60
59
|
rel = scoped
|
61
60
|
rel.ranks = matches
|
@@ -89,7 +88,7 @@ module Ximate
|
|
89
88
|
|
90
89
|
def update_index(locale = I18n.default_locale, &block)
|
91
90
|
if DATA[locale]
|
92
|
-
table = self.class.
|
91
|
+
table = self.class.table_name.to_sym
|
93
92
|
remove_index(locale)
|
94
93
|
instance_eval(&block)
|
95
94
|
@words.each do |priority, words|
|
@@ -102,7 +101,7 @@ module Ximate
|
|
102
101
|
end
|
103
102
|
|
104
103
|
def remove_index(locale)
|
105
|
-
table = self.class.
|
104
|
+
table = self.class.table_name.to_sym
|
106
105
|
@words = {}
|
107
106
|
DATA[locale.to_sym][table].each do |word, ids_ranks|
|
108
107
|
ids_ranks.delete(self.id)
|
data/lib/ximate/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ximate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-11-04 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Approximate fuzzy search for Ruby on Rails activerecord models.
|
15
15
|
email:
|