quest_search 0.0.2 → 0.0.3
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 +79 -0
- data/lib/quest_search/version.rb +3 -0
- data/lib/quest_search.rb +5 -0
- data/quest_search.gemspec +24 -0
- metadata +5 -1
@@ -0,0 +1,79 @@
|
|
1
|
+
module QuestSearch
|
2
|
+
module QuestSearchOn
|
3
|
+
extend ActiveSupport::Concern
|
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
|
+
@limit = options[:limit] || 1000
|
15
|
+
|
16
|
+
cattr_accessor :quest_search_fields
|
17
|
+
|
18
|
+
self.quest_search_fields = fields
|
19
|
+
end
|
20
|
+
|
21
|
+
def quest_search_for(query, page_no = 1, page_size = nil)
|
22
|
+
words = query.to_s.split " "
|
23
|
+
conditions = words.map do |w|
|
24
|
+
self.quest_search_fields.map do |f|
|
25
|
+
replace_bind_variables("#{f} like ?", ["%#{w}%"])
|
26
|
+
end.join " OR "
|
27
|
+
end.join " OR "
|
28
|
+
|
29
|
+
self.where(conditions).order(@order).limit(@limit).sort do |a, b|
|
30
|
+
b.points_for(query) <=> a.points_for(query)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def points_for(query)
|
36
|
+
query = query.to_s.downcase
|
37
|
+
@quest_search_relevance ||= {}
|
38
|
+
return @quest_search_relevance[query] if @quest_search_relevance[query]
|
39
|
+
words = query.split " "
|
40
|
+
|
41
|
+
self.search_word_matches = []
|
42
|
+
self.search_word_matches_count = 0
|
43
|
+
self.search_word_total_matches_count = 0
|
44
|
+
|
45
|
+
points = words.map do |w|
|
46
|
+
matches = 0
|
47
|
+
|
48
|
+
points = self.quest_search_fields.map do |field|
|
49
|
+
content = self.send(field).to_s.downcase
|
50
|
+
|
51
|
+
matches = content.scan(w).size
|
52
|
+
|
53
|
+
# one point for partial word matches
|
54
|
+
(content.scan(w).size +
|
55
|
+
# one point for partial query matches
|
56
|
+
content.scan(query).size +
|
57
|
+
# two points for exact word match
|
58
|
+
(content == w ? 2 : 0) +
|
59
|
+
# two points for exact query match
|
60
|
+
(content == query ? 2 : 0))
|
61
|
+
end.sum
|
62
|
+
|
63
|
+
if matches != 0
|
64
|
+
self.search_word_matches.push [ 'word' => w, 'count' => matches ]
|
65
|
+
self.search_word_matches_count += 1
|
66
|
+
self.search_word_total_matches_count += matches
|
67
|
+
end
|
68
|
+
|
69
|
+
points
|
70
|
+
end.sum
|
71
|
+
|
72
|
+
self.relevance = points
|
73
|
+
|
74
|
+
@quest_search_relevance[query] = points
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
ActiveRecord::Base.send :include, QuestSearch::QuestSearchOn
|
data/lib/quest_search.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "quest_search/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "quest_search"
|
7
|
+
s.version = QuestSearch::VERSION
|
8
|
+
s.authors = ["Fredrik Sundström"]
|
9
|
+
s.email = ["fredrik.sundstrom@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/fredriksundstrom/quest"
|
11
|
+
s.summary = "Extremely naive full text search implementation for ActiveRecord based on naive-search by Tomas Jogin (http://github.com/tjogin/naive-search). Orders results by relevance and gives you detailed info on the search results."
|
12
|
+
s.description = "Quest is a gem that lets you search your model in a simple and intuitive way."
|
13
|
+
|
14
|
+
s.rubyforge_project = "quest_search"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
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.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -23,6 +23,10 @@ files:
|
|
23
23
|
- Gemfile
|
24
24
|
- README
|
25
25
|
- Rakefile
|
26
|
+
- lib/quest_search.rb
|
27
|
+
- lib/quest_search/quest_search_on.rb
|
28
|
+
- lib/quest_search/version.rb
|
29
|
+
- quest_search.gemspec
|
26
30
|
homepage: http://github.com/fredriksundstrom/quest
|
27
31
|
licenses: []
|
28
32
|
post_install_message:
|