facile_search 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +15 -0
- data/facile_search.gemspec +30 -0
- data/lib/facile_search/inverted_index.rb +55 -0
- data/lib/facile_search/meta_data.rb +19 -0
- data/lib/facile_search/tokenizer/ngram.rb +14 -0
- data/lib/facile_search/version.rb +4 -0
- data/lib/facile_search.rb +9 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df9c6537e798b558d1322a4f9b4708261e5a95b6
|
4
|
+
data.tar.gz: 87270a7bbd1d922517e48594e5a7c2b1b22d866c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90f3969afac89a9a114806a71d720a7b24376cab41e219ecd0cec50d7e3b054dbabd318e56cc399950fb95bcb92d2f3871389d4b9bbee928fa33f7e7bf9cd473
|
7
|
+
data.tar.gz: 06b25f0d288f36320cbca8fe019a7cd69d273559da08a2724a09b09ada2a580a1f8721a3c590baca9f4fead1ccd265b30d3c1f572f153b57c890d1543e5b0087
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 i2bskn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# FacileSearch
|
2
|
+
|
3
|
+
Simple search with inverted index.
|
4
|
+
(Index data is stored in the Redis.)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'facile_search'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install facile_search
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
meta = FacileSearch::MetaData.new(namespace: "example_index", text_field: "text")
|
26
|
+
index = FacileSearch::InvertedIndex.new(meta)
|
27
|
+
sample = Struct.new(:id, :text).new(1, "some text")
|
28
|
+
index.indexing(sample) # => "OK"
|
29
|
+
index.search(["text"]) # => [1]
|
30
|
+
```
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it ( https://github.com/i2bskn/facile_search/fork )
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create a new Pull Request
|
39
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
task :default => :spec
|
5
|
+
|
6
|
+
desc "Run all specs"
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Console with library"
|
12
|
+
task :console do
|
13
|
+
sh "pry -I lib -r bundler/setup -r facile_search"
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'facile_search/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "facile_search"
|
8
|
+
spec.version = FacileSearch::VERSION
|
9
|
+
spec.authors = ["i2bskn"]
|
10
|
+
spec.email = ["i2bskn@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple search with inverted index.}
|
13
|
+
spec.description = %q{Simple search with inverted index.}
|
14
|
+
spec.homepage = "https://github.com/i2bskn/facile_search"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "redis-objects"
|
23
|
+
spec.add_dependency "oj"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "facile_search/tokenizer/ngram"
|
2
|
+
|
3
|
+
module FacileSearch
|
4
|
+
class InvertedIndex
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
attr_reader :meta_data
|
8
|
+
|
9
|
+
def_delegators :meta_data, :lock_key, *MetaData::BASE_KEYS
|
10
|
+
|
11
|
+
def initialize(meta_data)
|
12
|
+
@meta_data = meta_data
|
13
|
+
end
|
14
|
+
|
15
|
+
def indexing(obj)
|
16
|
+
tokens = tokenizer.tokenize(obj.public_send(text_field))
|
17
|
+
id = obj.public_send(id_field)
|
18
|
+
lock = Redis::Lock.new(lock_key)
|
19
|
+
begin
|
20
|
+
lock.lock do
|
21
|
+
values = redis.hmget(namespace, tokens).map {|ids|
|
22
|
+
array = deserialize(ids)
|
23
|
+
array << id
|
24
|
+
serialize(array.uniq)
|
25
|
+
}
|
26
|
+
redis.hmset(namespace, *tokens.zip(values).flatten(1))
|
27
|
+
end
|
28
|
+
rescue Redis::Lock::LockTimeout
|
29
|
+
raise
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def search(queries)
|
34
|
+
queries.each_with_object([]) {|query, obj|
|
35
|
+
tokens = tokenizer.tokenize(query)
|
36
|
+
return [] if tokens.size.zero?
|
37
|
+
obj << redis.hmget(namespace, *tokens).map {|ids| deserialize(ids) }.inject(&:&)
|
38
|
+
}.inject(&:&)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def serialize(obj)
|
43
|
+
Oj.dump(obj)
|
44
|
+
end
|
45
|
+
|
46
|
+
def deserialize(string)
|
47
|
+
string ? Oj.load(string) : []
|
48
|
+
end
|
49
|
+
|
50
|
+
def redis
|
51
|
+
@redis ||= Redis.current
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module FacileSearch
|
2
|
+
class MetaData
|
3
|
+
BASE_KEYS = %i(namespace text_field id_field tokenizer).freeze
|
4
|
+
|
5
|
+
attr_reader *BASE_KEYS
|
6
|
+
|
7
|
+
def initialize(namespace: , text_field: , id_field: nil, tokenizer: nil)
|
8
|
+
@namespace = namespace.to_s
|
9
|
+
@text_field = text_field.to_s
|
10
|
+
@id_field = id_field || "id"
|
11
|
+
@tokenizer = tokenizer || Tokenizer::NGram.new(2)
|
12
|
+
end
|
13
|
+
|
14
|
+
def lock_key
|
15
|
+
[namespace, "lock"].join("_")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: facile_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- i2bskn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis-objects
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oj
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Simple search with inverted index.
|
98
|
+
email:
|
99
|
+
- i2bskn@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- facile_search.gemspec
|
112
|
+
- lib/facile_search.rb
|
113
|
+
- lib/facile_search/inverted_index.rb
|
114
|
+
- lib/facile_search/meta_data.rb
|
115
|
+
- lib/facile_search/tokenizer/ngram.rb
|
116
|
+
- lib/facile_search/version.rb
|
117
|
+
homepage: https://github.com/i2bskn/facile_search
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.4.5
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Simple search with inverted index.
|
141
|
+
test_files: []
|