redisearch-rails 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +162 -18
- data/lib/redisearch-rails/redisearchable/class_methods.rb +3 -1
- data/lib/redisearch-rails/redisearchable/instance_methods.rb +7 -2
- data/lib/redisearch-rails/version.rb +1 -1
- data/redisearch-rails.gemspec +1 -1
- metadata +6 -7
- data/LICENSE +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 389ea395f1fe1ef5130083e382f435fa4515d1ab3f9952f4bc660eed47838d40
|
4
|
+
data.tar.gz: cb56f462ab039f9b93eb7c9310cbd50a3a9678a1b2eb1d2c4205caec3066754c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f534dafaba92007233233420f17b7230f521df2d7a82b785b6cc2c35d03201d37c38ecbb4913aa90500aedc690cd9fd8840919261234c70626f9202f9dd83b1
|
7
|
+
data.tar.gz: 650c151013343ab35936a565b9ad657f54fc978ee64ed77ddec04fa04751d2b513318e8c25f4327756779305996526b768e2b6bc4f6fc717d16224b9607f5227
|
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
# Redisearch
|
1
|
+
# Redisearch-rails
|
2
2
|
|
3
|
-
|
3
|
+
Adds support for easily indexing and search ActiveRecord models using RediSearch module http://redisearch.io/
|
4
4
|
|
5
|
-
|
5
|
+
# Getting started
|
6
6
|
|
7
|
-
|
7
|
+
First you need a Redisearch Service running, please follow [Quick Start](https://oss.redislabs.com/redisearch/Quick_Start.html) documentation.
|
8
8
|
|
9
|
-
|
9
|
+
Redisearch-rails is compatible with Rails `~> 4.2` on Ruby `2.3` and later.
|
10
|
+
|
11
|
+
In your Gemfile, for the last officially released gem:
|
10
12
|
|
11
13
|
```ruby
|
12
14
|
gem 'redisearch-rails'
|
@@ -14,30 +16,172 @@ gem 'redisearch-rails'
|
|
14
16
|
|
15
17
|
And then execute:
|
16
18
|
|
17
|
-
|
19
|
+
```bash
|
20
|
+
$ bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
Once the gem installed you will need to add the configuration to cannect to redisearch.
|
18
24
|
|
19
|
-
|
25
|
+
Go to your initializers (`config/initializers`), add a new file and call it `redisearch.rb`
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
RediSearch.configure do |config|
|
29
|
+
config.redis_config = {
|
30
|
+
host: '127.0.0.1',
|
31
|
+
port: '6379',
|
32
|
+
db: 0, # this has to be 0
|
33
|
+
# password: 'some password' #(optional)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
```
|
20
37
|
|
21
|
-
$ gem install redisearch-rails
|
22
38
|
|
23
39
|
## Usage
|
24
40
|
|
25
|
-
|
41
|
+
To integrate Redisearch for Rails only you need to call `redisearch` method defining the Redisearch `schema` inside of your ActiveRecord Model.
|
26
42
|
|
27
|
-
|
43
|
+
```ruby
|
44
|
+
class User < ActiveRecord::Base
|
45
|
+
redisearch schema: {
|
46
|
+
first_name: { text: { phonetic: "dm:es" } },
|
47
|
+
last_name: { text: { phonetic: "dm:es" } },
|
48
|
+
email: :text,
|
49
|
+
age: :numeric
|
50
|
+
}
|
51
|
+
end
|
52
|
+
```
|
28
53
|
|
29
|
-
|
54
|
+
This will add the `reindex` and `rediseach` class methods.
|
30
55
|
|
31
|
-
|
56
|
+
Now you can index all the recods, using `User.reindex` and search using `redisearch` method with [RediSearch Query Syntax](https://oss.redislabs.com/redisearch/Query_Syntax.html).
|
32
57
|
|
33
|
-
|
58
|
+
```ruby
|
59
|
+
irb(main):004:0> User.redisearch('@first_name:(Jon|Jane) @last_name:"Doe"')
|
60
|
+
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IN (2, 1)
|
61
|
+
=> [#<User id: 1, email: "jon@test.com", first_name: "Jon", last_name: "Doe", created_at: "2020-1-06 19:21:36", updated_at: "2020-1-06 19:24:43", age: 15>, #<User id: 2, email: "Jane@other.com", first_name: "Jane", last_name: "Doe", created_at: "2020-1-06 22:19:00", updated_at: "2020-1-06 22:19:00", age: 20>]
|
62
|
+
```
|
34
63
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/pbeckmann/redisearch-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
64
|
|
37
|
-
##
|
65
|
+
## Indexing
|
66
|
+
Each Model with defined `redisearch` is a **Redisearch Index** representation, and an instantiated Object its a **Redisearch Document** where the ID attribute of the instance is the ID of the Document.
|
67
|
+
|
68
|
+
By default, `reindex` will use `all` scope for find, you can change overwriting the scope
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
class User < ActiveRecord::Base
|
72
|
+
scope :redisearch_import, -> { where(email: 'some@email.com') }
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
If you need to recreate the Index, you can use `recreate: true` option on `reindex`
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
irb(main):004:0> User.reindex(recreate: true)
|
80
|
+
=> nil
|
81
|
+
```
|
38
82
|
|
39
|
-
|
83
|
+
this will drop the **Index** with all the **Documents** and start to reindexing.
|
40
84
|
|
41
|
-
|
85
|
+
### Custom Attributes
|
86
|
+
You can use Custom attributes defining a method or attr_accessor.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
# == Schema Information
|
90
|
+
#
|
91
|
+
# Table name: users
|
92
|
+
# first_name :string(255)
|
93
|
+
# last_name :string(255)
|
94
|
+
# email :string(255) default(""), not null
|
95
|
+
# company_id :integer
|
96
|
+
#
|
97
|
+
class User < ActiveRecord::Base
|
98
|
+
redisearch schema: {
|
99
|
+
full_name: { text: { phonetic: "dm:es" } },
|
100
|
+
company_name: :text
|
101
|
+
}
|
102
|
+
|
103
|
+
belongs_to :company
|
104
|
+
|
105
|
+
scope :redisearch_import, -> { all.includes(:company) }
|
106
|
+
|
107
|
+
after_commit :redisearch_add, on: [ :create, :update ]
|
108
|
+
after_commit :redisearch_delete, on: :destroy
|
109
|
+
|
110
|
+
def company_name
|
111
|
+
company.name
|
112
|
+
end
|
113
|
+
|
114
|
+
def full_name
|
115
|
+
"#{first_name} #{last_name}"
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
def redisearch_add
|
121
|
+
redisearch_document.add(replace: true, partial: true)
|
122
|
+
end
|
123
|
+
|
124
|
+
def redisearch_delete
|
125
|
+
redisearch_document.del(dd: true)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
You can add a Serializer to the indexer like this
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
# == Schema Information
|
135
|
+
#
|
136
|
+
# Table name: company
|
137
|
+
# name :string(255)
|
138
|
+
#
|
139
|
+
class Company < ActiveRecord::Base
|
140
|
+
redisearch schema: {
|
141
|
+
name: { :text },
|
142
|
+
users_ids: :tag # Array
|
143
|
+
}, index_serializer: Company::RedisearchSerlializer
|
144
|
+
has_many :users
|
145
|
+
|
146
|
+
scope :redisearch_import, -> { includes(:users) }
|
147
|
+
end
|
148
|
+
|
149
|
+
class Company::RedisearchSerlializer
|
150
|
+
attr_reader :company
|
151
|
+
|
152
|
+
def initialize(company)
|
153
|
+
@company = company
|
154
|
+
end
|
155
|
+
|
156
|
+
def users_ids
|
157
|
+
company.users.ids #Array of ids
|
158
|
+
end
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
## Search
|
166
|
+
|
167
|
+
Simply use the `redisearch` method with a [RediSearch Query Syntax](https://oss.redislabs.com/redisearch/Query_Syntax.html).
|
168
|
+
|
169
|
+
This ask to Redisearch the Documents ids and then use ActiveRecord find method to brings the elements.
|
170
|
+
|
171
|
+
## TODOs
|
172
|
+
|
173
|
+
* ActiveModel callbacks to index records on saving and remove from Redis on delete
|
174
|
+
* More Configurations like batch size when reindexing
|
175
|
+
* Support GEO filters
|
176
|
+
* Stopwords configuration
|
177
|
+
* Configurable doc_id reference, for now use ID and find method to search
|
178
|
+
* Test coverage and better documentation
|
179
|
+
* Multiple redisearch indexes for the same model
|
180
|
+
|
181
|
+
## Contributing
|
182
|
+
|
183
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Ticketplus/redisearch-rails. This project is intended to be a safe, welcoming space for collaboration.
|
184
|
+
|
185
|
+
## License
|
42
186
|
|
43
|
-
|
187
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -2,10 +2,11 @@ module RediSearch
|
|
2
2
|
module RediSearchable
|
3
3
|
module ClassMethods
|
4
4
|
|
5
|
-
attr_reader :redisearch_index
|
5
|
+
attr_reader :redisearch_index, :redisearch_index_serializer
|
6
6
|
|
7
7
|
def redisearch(*args, schema:, **options)
|
8
8
|
prefix = options[:prefix]
|
9
|
+
@redisearch_index_serializer = options[:index_serializer]
|
9
10
|
|
10
11
|
index_name = [prefix, model_name.plural].compact.join("_")
|
11
12
|
@redisearch_index = RediSearch.client.generate_index(index_name, schema)
|
@@ -23,6 +24,7 @@ module RediSearch
|
|
23
24
|
def redisearch(query, **options)
|
24
25
|
result = redisearch_index.search(query, options.deep_merge(nocontent: true))
|
25
26
|
result.shift # remove the first element (count)
|
27
|
+
result.map! { |elem| elem.sub("#{redisearch_index.name}_", '')}
|
26
28
|
self.find(result)
|
27
29
|
end
|
28
30
|
|
@@ -10,13 +10,18 @@ module RediSearch
|
|
10
10
|
|
11
11
|
def generate_redisearch_document
|
12
12
|
index = self.class.redisearch_index
|
13
|
+
serializer = redisearch_serializer || self
|
13
14
|
fields = index.schema.fields.map(&:name)
|
14
|
-
fields_values = Hash[fields.flatten.map! { |field| [field, public_send(field)] }]
|
15
|
+
fields_values = Hash[fields.flatten.map! { |field| [field, serializer.public_send(field)] }]
|
15
16
|
index.generate_document(redisearch_document_id, fields_values)
|
16
17
|
end
|
17
18
|
|
18
19
|
def redisearch_document_id
|
19
|
-
|
20
|
+
[self.class.redisearch_index.name, self.id].join('_')
|
21
|
+
end
|
22
|
+
|
23
|
+
def redisearch_serializer
|
24
|
+
self.class.redisearch_index_serializer.new(self) if self.class.redisearch_index_serializer
|
20
25
|
end
|
21
26
|
|
22
27
|
end
|
data/redisearch-rails.gemspec
CHANGED
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_dependency 'redi_searcher', '~> 0.1', '>= 0.1.3'
|
30
30
|
|
31
31
|
spec.add_development_dependency "bundler", "~> 1.17"
|
32
|
-
spec.add_development_dependency "rake", "
|
32
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
33
33
|
spec.add_development_dependency "rspec", "~> 3.0"
|
34
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redisearch-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patricio Beckmann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02
|
11
|
+
date: 2020-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -76,16 +76,16 @@ dependencies:
|
|
76
76
|
name: rake
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- - "
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
|
-
version:
|
81
|
+
version: 12.3.3
|
82
82
|
type: :development
|
83
83
|
prerelease: false
|
84
84
|
version_requirements: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
88
|
+
version: 12.3.3
|
89
89
|
- !ruby/object:Gem::Dependency
|
90
90
|
name: rspec
|
91
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -112,7 +112,6 @@ files:
|
|
112
112
|
- ".travis.yml"
|
113
113
|
- CODE_OF_CONDUCT.md
|
114
114
|
- Gemfile
|
115
|
-
- LICENSE
|
116
115
|
- LICENSE.txt
|
117
116
|
- README.md
|
118
117
|
- Rakefile
|
data/LICENSE
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2020 Ticketplus.cl
|
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 all
|
13
|
-
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 THE
|
21
|
-
SOFTWARE.
|