redi_search_rails 0.1.0 → 0.1.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 +4 -4
- data/README.md +22 -11
- data/lib/redi_search_rails/version.rb +1 -1
- data/lib/redi_search_rails.rb +42 -5
- data/redi_search_rails.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 382515dc3277d39c0c8389db1424fe6d73cda0ca
|
4
|
+
data.tar.gz: 86727dec2fe4a6e642314d0a805571dfac49efd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50dc588fb78895feefec03a17d88a080b37fab8e285519a954583401509d41b5ce5a4c30c00d387a4a68771f69a89f57db932d427d57deda660b27046268dd2c
|
7
|
+
data.tar.gz: ab851b7e3b939cc017c7ce0956ca6dfc3e075cf3e54e639a73cd8422d1ef5899d3861a3d9362127bb4482bec8158a312119b2e823eb227258a4a37a40f1be143
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ This gems simplifies integration with RediSearch module (http://redisearch.io/).
|
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
Install Redis and RediSearch following instructions on http://redisearch.io/Quick_Start/.
|
7
|
+
Install Redis and RediSearch following instructions on http://redisearch.io/Quick_Start/. Until modules are released with Redis 4 this requires compiling Redis from unstable branch from GitHub.
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
@@ -33,32 +33,43 @@ REDI_SEARCH = Redis.new(host: '127.0.0.1', port: '6379')
|
|
33
33
|
```ruby
|
34
34
|
class User < ApplicationRecord
|
35
35
|
include RediSearchRails
|
36
|
-
|
36
|
+
redi_search_schema name: 'TEXT', email: 'TEXT', age: 'NUMERIC'
|
37
37
|
end
|
38
|
-
# => create index
|
39
|
-
|
40
|
-
# => populate with records
|
41
|
-
|
38
|
+
# => to create index run in rails console
|
39
|
+
User.ft_create
|
40
|
+
# => populate index with records for all users
|
41
|
+
User.ft_add_all
|
42
|
+
# => or you can do it for specific record
|
43
|
+
User.ft_add(User.where(id: 1))
|
42
44
|
# => search
|
43
|
-
|
45
|
+
User.ft_search('keyword here')
|
46
|
+
# => output
|
47
|
+
[1, "gid://application_name/User/unique_id", ["name", "Bob", "age", "100"]]
|
44
48
|
```
|
45
49
|
|
50
|
+
As unique identifier (doc_id) for records in Redis it uses https://github.com/rails/globalid. You need to make sure you models support it if you are using ORM other than ActiveRecord.
|
51
|
+
|
46
52
|
## Development
|
47
53
|
|
48
54
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To understand the code look in `lib/redi_search_rails`.
|
49
55
|
|
50
56
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
51
57
|
|
58
|
+
## Testing
|
59
|
+
|
60
|
+
Testing this gem requires having local Redis with RediSearch module. This makes it very difficult to set it up with services like Codeship.
|
61
|
+
|
52
62
|
## TODOs
|
53
63
|
|
54
|
-
*
|
64
|
+
* ActiveModel callbacks to index records on saving and remove from Redis on delete
|
65
|
+
* Rake tasks to manage indexes
|
55
66
|
* Support additional RediSearch API calls (SUGGADD, SUGGET, ...)
|
56
|
-
* Support configuring SCORE
|
57
|
-
* Rake tasks
|
58
|
-
* ActiveModel callbacks to index records on saving
|
67
|
+
* Support configuring SCORE, WEIGHT and other options
|
59
68
|
* Support indexing fields from related models (index group name if user belongs to a group)
|
60
69
|
* Stopwords configuration
|
61
70
|
* Minimum keyword length to index
|
71
|
+
* Configurable method for doc_id, not just default to_global_id
|
72
|
+
* More test coverage and documentation
|
62
73
|
|
63
74
|
## Contributing
|
64
75
|
|
data/lib/redi_search_rails.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
require "redi_search_rails/version"
|
2
|
+
require "active_support/concern"
|
2
3
|
|
3
4
|
module RediSearchRails
|
4
5
|
extend ActiveSupport::Concern
|
5
6
|
|
7
|
+
included do
|
8
|
+
end
|
9
|
+
|
6
10
|
module ClassMethods
|
7
11
|
|
8
|
-
|
12
|
+
# will configure the RediSearch for the specific model
|
13
|
+
#
|
14
|
+
# @see https://github.com/dmitrypol/redi_search_rails
|
15
|
+
# @param schema [Hash] name: 'TEXT', age: 'NUMERIC'
|
16
|
+
def redi_search_schema(schema)
|
9
17
|
@@schema = schema.to_a.flatten
|
10
18
|
@@fields = schema.keys
|
11
19
|
@@model = self.name.constantize
|
@@ -13,8 +21,13 @@ module RediSearchRails
|
|
13
21
|
@@score = 1
|
14
22
|
end
|
15
23
|
|
16
|
-
|
17
|
-
|
24
|
+
# search the index for specific keyword(s)
|
25
|
+
#
|
26
|
+
# @param keyword [String] 'some keyword'
|
27
|
+
# @return [Array] [1, "gid://application_name/User/unique_id", ["name", "Bob", "age", "100"]]
|
28
|
+
# @raise [RuntimeError]
|
29
|
+
def ft_search keyword
|
30
|
+
results = REDI_SEARCH.call('FT.SEARCH', @@index_name, keyword,
|
18
31
|
#'LIMIT', 0, 1000,
|
19
32
|
#'NOCONTENT', #'VERBATIM', #'WITHSCORES', #'NOSTOPWORDS', #'WITHPAYLOADS',
|
20
33
|
)
|
@@ -24,6 +37,9 @@ module RediSearchRails
|
|
24
37
|
return e.message
|
25
38
|
end
|
26
39
|
|
40
|
+
# create index for specific model
|
41
|
+
#
|
42
|
+
# @return [String]
|
27
43
|
def ft_create
|
28
44
|
REDI_SEARCH.call('FT.CREATE', @@index_name,
|
29
45
|
#'NOFIELDS', 'NOSCOREIDX', 'NOOFFSETS',
|
@@ -35,6 +51,9 @@ module RediSearchRails
|
|
35
51
|
return e.message
|
36
52
|
end
|
37
53
|
|
54
|
+
# index all records in specific model
|
55
|
+
#
|
56
|
+
# @return [String]
|
38
57
|
def ft_add_all
|
39
58
|
@@model.all.each {|record| ft_add(record) }
|
40
59
|
rescue Exception => e
|
@@ -42,6 +61,10 @@ module RediSearchRails
|
|
42
61
|
return e.message
|
43
62
|
end
|
44
63
|
|
64
|
+
# index specific record
|
65
|
+
#
|
66
|
+
# @param record [Object] Object to index
|
67
|
+
# @return [String]
|
45
68
|
def ft_add record
|
46
69
|
fields = []
|
47
70
|
@@fields.each { |field| fields.push(field, record.send(field)) }
|
@@ -55,13 +78,21 @@ module RediSearchRails
|
|
55
78
|
return e.message
|
56
79
|
end
|
57
80
|
|
58
|
-
|
81
|
+
# delete specific document from index
|
82
|
+
#
|
83
|
+
# @param record [Object] Object to delete
|
84
|
+
# @return [String]
|
85
|
+
def ft_del record
|
86
|
+
doc_id = record.to_global_id
|
59
87
|
REDI_SEARCH.call('FT.DEL', @@index_name, doc_id)
|
60
88
|
rescue Exception => e
|
61
89
|
Rails.logger.error e
|
62
90
|
return e.message
|
63
91
|
end
|
64
92
|
|
93
|
+
# optimize specific index
|
94
|
+
#
|
95
|
+
# @return [String]
|
65
96
|
def ft_optimize
|
66
97
|
REDI_SEARCH.call('FT.OPTIMIZE', @@index_name)
|
67
98
|
rescue Exception => e
|
@@ -69,6 +100,9 @@ module RediSearchRails
|
|
69
100
|
return e.message
|
70
101
|
end
|
71
102
|
|
103
|
+
# drop specific index
|
104
|
+
#
|
105
|
+
# @return [String]
|
72
106
|
def ft_drop
|
73
107
|
REDI_SEARCH.call('FT.DROP', @@index_name)
|
74
108
|
rescue Exception => e
|
@@ -76,8 +110,11 @@ module RediSearchRails
|
|
76
110
|
return e.message
|
77
111
|
end
|
78
112
|
|
113
|
+
# get info about specific index
|
114
|
+
#
|
115
|
+
# @return [String]
|
79
116
|
def ft_info
|
80
|
-
|
117
|
+
REDI_SEARCH.call('FT.INFO', @@index_name)
|
81
118
|
rescue Exception => e
|
82
119
|
Rails.logger.error e
|
83
120
|
return e.message
|
data/redi_search_rails.gemspec
CHANGED
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.add_development_dependency "bundler", "~> 1.14"
|
34
34
|
spec.add_development_dependency "rake", "~> 10.0"
|
35
35
|
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
spec.add_development_dependency "activemodel", "~> 5.0.0"
|
36
37
|
# =>
|
37
38
|
spec.add_runtime_dependency "activesupport", "~> 5.0.0"
|
38
39
|
spec.add_runtime_dependency "redis", "~> 3.3"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redi_search_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Polyakovsky
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 5.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 5.0.0
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: activesupport
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|