ximate 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@ ApproXIMATE fuzzy search for Ruby on Rails activerecord models.
4
4
 
5
5
  == Requirements
6
6
 
7
- * Rails >= 3.0.0
7
+ * Rails >= 3.0.1
8
8
 
9
9
 
10
10
  == Installation
@@ -17,15 +17,22 @@ In your model puts some like this:
17
17
 
18
18
  class Post < ActiveRecord::Base
19
19
  define_index(:en) do
20
- add_text title
21
- add_text keywords.join(' ')
20
+ add_text title, 3
21
+ add_text keywords.join(' '), 2
22
22
  add_text body(:en)
23
23
  end
24
24
  end
25
25
 
26
+ In this case we can perform an appoximate search on title, keywords and body fields of posts
27
+ where title is most important than keywords than body (title have priority 3, keywords 2 and body 1).
28
+
26
29
  Then you can perform a search
27
30
 
28
- Post.asearch('Economy').where(:public => true).limit(5)
31
+ Post.asearch('Hello world').where(:public => true).limit(5)
32
+
33
+ You can also order the results by rank (calculate by ximate)
34
+
35
+ Post.asearch('Hello world').order('rank DESC').where(:public => true).limit(5)
29
36
 
30
37
  == Questions or problems?
31
38
 
@@ -3,20 +3,33 @@ module ActiveRecord
3
3
  class Relation
4
4
  attr_accessor :ranks
5
5
 
6
- alias_method :orig_to_a, :to_a
7
6
  alias_method :orig_initialize, :initialize
7
+ alias_method :orig_order, :order
8
+ # alias_method :orig_to_a, :to_a
8
9
 
9
10
  def initialize(klass, table)
10
11
  @ranks = {}
11
12
  orig_initialize(klass, table)
12
13
  end
13
14
 
14
- def to_a
15
- return orig_to_a if @ranks.empty?
16
- orig_to_a.sort do |x, y|
17
- @ranks[y.id] <=> @ranks[x.id]
15
+ def order(*args)
16
+ if !@ranks.empty? && args[0] =~ /^rank/i
17
+ tokens = args[0].split(' ')
18
+ verse = tokens[1] if tokens[1] =~ /^(asc|desc)$/i
19
+ verse ||= 'ASC'
20
+ id_ordered = @ranks.keys.sort{|x,y| @ranks[x] <=> @ranks[y]}
21
+ orig_order("FIELD(id,#{id_ordered.join(',')}) #{verse}")
22
+ else
23
+ orig_order(args)
18
24
  end
19
25
  end
26
+
27
+ # def to_a
28
+ # return orig_to_a if @ranks.empty?
29
+ # orig_to_a.sort do |x, y|
30
+ # @ranks[y.id] <=> @ranks[x.id]
31
+ # end
32
+ # end
20
33
  end
21
34
 
22
35
  end
data/lib/ximate/search.rb CHANGED
@@ -3,7 +3,6 @@ module Ximate
3
3
  DATA = {}
4
4
  OPTIONS = {:match_error_percent => 20,
5
5
  :ignore_word_short_than => 2,
6
- #:order_by_rank => true,
7
6
  :logger => true,
8
7
  :debug => false}
9
8
 
@@ -54,25 +53,26 @@ module Ximate
54
53
  end
55
54
  end
56
55
  end
57
- return select('*, 0 AS RANK').where('1 = 0') if matches.empty?
58
- #rel = scoped
59
- #rel.ranks = matches if OPTIONS[:order_by_rank]
60
- #rel.where("#{table}.id IN (#{matches.keys.join(',')})")
61
- select("*, #{gen_if_select(matches)} AS RANK").where("#{table}.id IN (#{matches.keys.join(',')})")
56
+ #return select('*, 0 AS RANK').where('1 = 0') if matches.empty?
57
+ return where('1 = 0') if matches.empty?
58
+ rel = scoped
59
+ rel.ranks = matches
60
+ rel.where("#{table}.id IN (#{matches.keys.join(',')})")
61
+ # select("*, #{gen_if_select(matches)} AS RANK").where("#{table}.id IN (#{matches.keys.join(',')})")
62
62
  end
63
63
 
64
- private
65
-
66
- def gen_if_select(matches)
67
- tmp = 'IF(id=myid,myrank,if)'
68
- str = 'IF(id=myid,myrank,if)'
69
- matches.each do |id, rank|
70
- str.gsub!('myid', id.to_s)
71
- str.gsub!('myrank', rank.to_s)
72
- str.gsub!('if', tmp)
73
- end
74
- return str.gsub(tmp, '0')
75
- end
64
+ # private
65
+ #
66
+ # def gen_if_select(matches)
67
+ # tmp = 'IF(id=myid,myrank,if)'
68
+ # str = 'IF(id=myid,myrank,if)'
69
+ # matches.each do |id, rank|
70
+ # str.gsub!('myid', id.to_s)
71
+ # str.gsub!('myrank', rank.to_s)
72
+ # str.gsub!('if', tmp)
73
+ # end
74
+ # return str.gsub(tmp, '0')
75
+ # end
76
76
 
77
77
  end
78
78
 
@@ -1,3 +1,3 @@
1
1
  module Ximate
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/ximate.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require File.join(File.dirname(__FILE__), 'ximate/search')
2
- #require File.join(File.dirname(__FILE__), 'ximate/activerecord/relation')
2
+ require File.join(File.dirname(__FILE__), 'ximate/activerecord/relation')
3
3
  require File.join(File.dirname(__FILE__), '../ext/fuzzy_search')
4
4
 
5
5
  ActiveRecord::Base.send(:include, Ximate)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ximate
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Enrico Pilotto
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-11 00:00:00 +02:00
13
+ date: 2011-05-13 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  requirements: []
63
63
 
64
64
  rubyforge_project: ximate
65
- rubygems_version: 1.6.1
65
+ rubygems_version: 1.6.2
66
66
  signing_key:
67
67
  specification_version: 3
68
68
  summary: Approximate fuzzy search for Ruby on Rails