quest_search 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,95 +1,105 @@
1
1
  module QuestSearch
2
- module QuestSearchOn
3
- extend ActiveSupport::Concern
2
+ module QuestSearchOn
3
+ extend ActiveSupport::Concern
4
4
 
5
- module ClassMethods
6
- def quest_search_on(*fields)
7
- if fields.size.zero?
8
- raise "No arguments given, please specify which fields should be indexed and searched."
9
- end
10
-
11
- options = fields.extract_options!
12
-
13
- @order = options[:order] || "id desc"
14
-
15
- cattr_accessor :quest_search_fields
16
-
17
- self.quest_search_fields = fields
18
- end
19
-
20
- def quest_search_for(query, page_no = 1, page_size = 2**32)
21
- words = query.to_s.split " "
22
- conditions = words.map do |w|
23
- self.quest_search_fields.map do |f|
24
- replace_bind_variables("#{f} like ?", ["%#{w}%"])
25
- end.join " OR "
26
- end.join " OR "
27
-
28
- offset = (page_no * page_size) - page_size
29
- offset_ends = offset + page_size
30
-
31
- results = self.where(conditions).order(@order).sort do |a, b|
32
- b.points_for(query) <=> a.points_for(query)
33
- end
34
-
35
- end_results = []
36
-
37
- results.each_with_index do |r, index|
38
- if index == offset_ends
39
- break
40
- end
41
-
42
- if index >= offset
43
- end_results.push r
44
- end
45
- end
46
-
47
- end_results
48
- end
49
- end
50
-
51
- def points_for(query)
52
- query = query.to_s.downcase
53
- @quest_search_relevance ||= {}
54
- return @quest_search_relevance[query] if @quest_search_relevance[query]
55
- words = query.split " "
56
-
57
- self.search_word_matches = []
58
- self.search_word_matches_count = 0
59
- self.search_word_total_matches_count = 0
60
-
61
- points = words.map do |w|
62
- matches = 0
63
-
64
- points = self.quest_search_fields.map do |field|
65
- content = self.send(field).to_s.downcase
66
-
67
- matches = content.scan(w).size
68
-
69
- # one point for partial word matches
70
- (content.scan(w).size +
71
- # one point for partial query matches
72
- content.scan(query).size +
73
- # two points for exact word match
74
- (content == w ? 2 : 0) +
75
- # two points for exact query match
76
- (content == query ? 2 : 0))
77
- end.sum
78
-
79
- if matches != 0
80
- self.search_word_matches.push [ 'word' => w, 'count' => matches ]
81
- self.search_word_matches_count += 1
82
- self.search_word_total_matches_count += matches
83
- end
84
-
85
- points
86
- end.sum
87
-
88
- self.relevance = points
89
-
90
- @quest_search_relevance[query] = points
91
- end
92
- end
5
+ module ClassMethods
6
+ def quest_search_on(*fields)
7
+ if fields.size.zero?
8
+ raise "No arguments given, please specify which fields should be indexed and searched."
9
+ end
10
+
11
+ options = fields.extract_options!
12
+
13
+ @order = options[:order] || "id desc"
14
+
15
+ cattr_accessor :quest_search_fields
16
+
17
+ self.quest_search_fields = fields
18
+ end
19
+
20
+ def quest_search_for(query, page_no = 1, page_size = 2**32, sort = nil)
21
+ words = query.to_s.split " "
22
+ conditions = words.map do |w|
23
+ self.quest_search_fields.map do |f|
24
+ replace_bind_variables("#{f} like ?", ["%#{w}%"])
25
+ end.join " OR "
26
+ end.join " OR "
27
+
28
+ offset = (page_no * page_size) - page_size
29
+ offset_ends = offset + page_size
30
+
31
+ @order = sort || @order
32
+
33
+ results = self.where(conditions).order(@order)
34
+
35
+ if !sort
36
+ results.sort do |a, b|
37
+ b.points_for(query) <=> a.points_for(query)
38
+ end
39
+ else
40
+ results.each do |r|
41
+ r.points_for(query)
42
+ end
43
+ end
44
+
45
+ end_results = []
46
+
47
+ results.each_with_index do |r, index|
48
+ if index == offset_ends
49
+ break
50
+ end
51
+
52
+ if index >= offset
53
+ end_results.push r
54
+ end
55
+ end
56
+
57
+ end_results
58
+ end
59
+ end
60
+
61
+ def points_for(query)
62
+ query = query.to_s.downcase
63
+ @quest_search_relevance ||= {}
64
+ return @quest_search_relevance[query] if @quest_search_relevance[query]
65
+ words = query.split " "
66
+
67
+ self.search_word_matches = []
68
+ self.search_word_matches_count = 0
69
+ self.search_word_total_matches_count = 0
70
+
71
+ points = words.map do |w|
72
+ matches = 0
73
+
74
+ points = self.quest_search_fields.map do |field|
75
+ content = self.send(field).to_s.downcase
76
+
77
+ matches = content.scan(w).size
78
+
79
+ # one point for partial word matches
80
+ (content.scan(w).size +
81
+ # one point for partial query matches
82
+ content.scan(query).size +
83
+ # two points for exact word match
84
+ (content == w ? 2 : 0) +
85
+ # two points for exact query match
86
+ (content == query ? 2 : 0))
87
+ end.sum
88
+
89
+ if matches != 0
90
+ self.search_word_matches.push [ 'word' => w, 'count' => matches ]
91
+ self.search_word_matches_count += 1
92
+ self.search_word_total_matches_count += matches
93
+ end
94
+
95
+ points
96
+ end.sum
97
+
98
+ self.relevance = points
99
+
100
+ @quest_search_relevance[query] = points
101
+ end
102
+ end
93
103
  end
94
104
 
95
105
  ActiveRecord::Base.send :include, QuestSearch::QuestSearchOn
@@ -1,3 +1,3 @@
1
1
  module QuestSearch
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quest_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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-11-22 00:00:00.000000000 Z
12
+ date: 2011-11-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Quest is a gem that lets you search your model in a simple and intuitive
15
15
  way.
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project: quest_search
50
- rubygems_version: 1.8.11
50
+ rubygems_version: 1.8.10
51
51
  signing_key:
52
52
  specification_version: 3
53
53
  summary: Extremely naive full text search implementation for ActiveRecord based on