mongoid_text_search 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/lib/.DS_Store +0 -0
- data/lib/mongoid_text_search/keyword_generator.rb +34 -0
- data/lib/mongoid_text_search/version.rb +5 -0
- data/lib/mongoid_text_search.rb +50 -0
- data/mongoid_text_search.gemspec +23 -0
- metadata +89 -0
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Overview
|
2
|
+
|
3
|
+
This gem is used in addition to Mongoid gem (up to version 2.0.0.beta.20), to provide simple search function.
|
4
|
+
It takes one or many string fields. Those strings will be cleaned up and transform into array so they can be indexed.
|
5
|
+
Searching later on will simply partially matches the input keywords with the indexed keywords in the arrays.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
gem "mongoid-simple_search"
|
10
|
+
|
11
|
+
## Getting Started
|
12
|
+
|
13
|
+
Add the following into your model.
|
14
|
+
|
15
|
+
include Mongoid::SimpleSearch
|
16
|
+
|
17
|
+
Specify which field(s) to be included as the keywords.
|
18
|
+
|
19
|
+
simple_search_for_fields :title, :description
|
20
|
+
|
21
|
+
The above will create three new fields (array type): title_keywords, description_keywords, and keywords.
|
22
|
+
|
23
|
+
## Available Class Methods
|
24
|
+
|
25
|
+
Convert any string into an array of clean keywords.
|
26
|
+
|
27
|
+
generate_clean_keywords(string)
|
28
|
+
|
29
|
+
Search documents with an array of keywords to match.
|
30
|
+
Use the previous method to generate an array of clean keywords from a string.
|
31
|
+
|
32
|
+
with_keywords(array)
|
33
|
+
|
34
|
+
There are also dynamic search methods for each field that was passed in simple_search_for_fields method.
|
35
|
+
|
36
|
+
with_title_keywords(array)
|
37
|
+
with_description_keywords(array)
|
data/Rakefile
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module TextSearch
|
3
|
+
module KeywordGenerator
|
4
|
+
Separator = " "
|
5
|
+
StripPunctuationRegex = /[^a-zA-Z0-9]/
|
6
|
+
PunctuationReplacement = ""
|
7
|
+
IgnoredWords = [
|
8
|
+
]
|
9
|
+
|
10
|
+
def clean_keywords(field)
|
11
|
+
clean_keywords = []
|
12
|
+
keywords = []
|
13
|
+
if field.is_a?(String)
|
14
|
+
keywords = field.squeeze(Separator).split(Separator)
|
15
|
+
elsif field.is_a?(Array)
|
16
|
+
keywords = field
|
17
|
+
end
|
18
|
+
for keyword in keywords
|
19
|
+
if keyword.is_a?(String)
|
20
|
+
keyword.downcase!
|
21
|
+
keyword.strip!
|
22
|
+
keyword.gsub!(StripPunctuationRegex, PunctuationReplacement)
|
23
|
+
if keyword.size > 2 && !IgnoredWords.include?(keyword) && !clean_keywords.include?(keyword)
|
24
|
+
clean_keywords << keyword
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return clean_keywords
|
29
|
+
end
|
30
|
+
|
31
|
+
extend self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "mongoid_text_search/keyword_generator"
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module TextSearch
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
before_save :set_keywords
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def search_fields
|
13
|
+
@search_fields ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def text_search_for_fields(*fields)
|
17
|
+
@search_fields = fields
|
18
|
+
|
19
|
+
# Create keyword field that will store all the keywords.
|
20
|
+
field :keywords, :type => Array
|
21
|
+
index :keywords
|
22
|
+
|
23
|
+
# Create a method for searching based on keywords of all specified field.
|
24
|
+
singleton = class << self; self end
|
25
|
+
singleton.send :define_method, "with_keywords" do |keywords|
|
26
|
+
if !keywords.nil? && keywords.count > 0
|
27
|
+
where(:keywords.all => keywords.collect!{ |keyword| /.*#{keyword}.*/ })
|
28
|
+
else
|
29
|
+
where() # Select everything.
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def generate_clean_keywords(string)
|
35
|
+
KeywordGenerator.clean_keywords(string)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module InstanceMethods
|
40
|
+
def set_keywords
|
41
|
+
self.keywords = []
|
42
|
+
new_keywords = []
|
43
|
+
for field in self.class.search_fields
|
44
|
+
new_keywords.concat(KeywordGenerator.clean_keywords(send("#{field}")))
|
45
|
+
end
|
46
|
+
self.keywords = new_keywords.uniq
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "mongoid_text_search/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mongoid_text_search"
|
7
|
+
s.version = Mongoid::TextSearch::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Steve Randy Tantra"]
|
10
|
+
s.email = ["steve.randy@gmail.com"]
|
11
|
+
s.homepage = "http://steverandytantra.com"
|
12
|
+
s.summary = %q{Simple full text search for Mongoid.}
|
13
|
+
s.description = %q{This gem is used in addition to Mongoid gem (up to version 2.0.0.beta.20), to provide simple full text search function. It takes one or many string or array of strings fields. The strings will be cleaned up and transform into array so they can be indexed. Searching later on will simply partially matches the input keywords with the indexed keywords in the arrays.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "mongoid_text_search"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency("mongoid", "<=2.0.0.beta.20")
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_text_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Steve Randy Tantra
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2011-01-31 00:00:00 +07:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: mongoid
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - <=
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- beta
|
32
|
+
- 20
|
33
|
+
version: 2.0.0.beta.20
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: This gem is used in addition to Mongoid gem (up to version 2.0.0.beta.20), to provide simple full text search function. It takes one or many string or array of strings fields. The strings will be cleaned up and transform into array so they can be indexed. Searching later on will simply partially matches the input keywords with the indexed keywords in the arrays.
|
37
|
+
email:
|
38
|
+
- steve.randy@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- CHANGELOG.md
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/.DS_Store
|
52
|
+
- lib/mongoid_text_search.rb
|
53
|
+
- lib/mongoid_text_search/keyword_generator.rb
|
54
|
+
- lib/mongoid_text_search/version.rb
|
55
|
+
- mongoid_text_search.gemspec
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://steverandytantra.com
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: mongoid_text_search
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Simple full text search for Mongoid.
|
88
|
+
test_files: []
|
89
|
+
|