quest_search 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/quest_search/quest_search_on.rb +100 -90
- data/lib/quest_search/version.rb +1 -1
- metadata +3 -3
@@ -1,95 +1,105 @@
|
|
1
1
|
module QuestSearch
|
2
|
-
|
3
|
-
|
2
|
+
module QuestSearchOn
|
3
|
+
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
data/lib/quest_search/version.rb
CHANGED
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.
|
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-
|
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.
|
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
|