redi_search_rails 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/redi_search_rails.rb +36 -25
- data/lib/redi_search_rails/version.rb +1 -1
- data/redi_search_rails.gemspec +3 -2
- metadata +24 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53fd6d815f84aaacba7a129c7e5bd574124b1937
|
4
|
+
data.tar.gz: 6e00184db536350ce5e3d30e9c25f182b7fb8ea1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 914e17adff80e11950a6b63af883ef90b9e1bd97f2884e07b6ceea8d3d7f177d46e5411dcee5c879100d2a353943b5b506fab71dc4b411d362400060f2e6130b
|
7
|
+
data.tar.gz: 730fb0ee494f8c79c7d5ffd6f28edc59b720737f5ae1d8855c8e522487ccb1816e24011030e1250a03055f496709965ffeadae642d3db355081abb3b9f638a0c
|
data/README.md
CHANGED
@@ -44,7 +44,7 @@ User.ft_add(User.where(id: 1))
|
|
44
44
|
# => search
|
45
45
|
User.ft_search('keyword here')
|
46
46
|
# => output
|
47
|
-
[
|
47
|
+
[{"id": "gid://application_name/User/unique_id", "name": "Bob", "age": "100"}, {...}]
|
48
48
|
```
|
49
49
|
|
50
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.
|
data/lib/redi_search_rails.rb
CHANGED
@@ -14,26 +14,37 @@ module RediSearchRails
|
|
14
14
|
# @see https://github.com/dmitrypol/redi_search_rails
|
15
15
|
# @param schema [Hash] name: 'TEXT', age: 'NUMERIC'
|
16
16
|
def redi_search_schema(schema)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
@schema = schema.to_a.flatten
|
18
|
+
@fields = schema.keys
|
19
|
+
@model = self.name.constantize
|
20
|
+
@index_name = @model.to_s
|
21
|
+
@score = 1
|
22
22
|
end
|
23
23
|
|
24
24
|
# search the index for specific keyword(s)
|
25
25
|
#
|
26
26
|
# @param keyword [String] 'some keyword'
|
27
|
-
# @return [Array] [
|
27
|
+
# @return [Array] [{"id": "gid://application_name/User/unique_id", "name": "Bob", "age": "100"}, ...]
|
28
28
|
# @raise [RuntimeError]
|
29
29
|
def ft_search keyword
|
30
|
-
results = REDI_SEARCH.call('FT.SEARCH',
|
30
|
+
results = REDI_SEARCH.call('FT.SEARCH', @index_name, keyword,
|
31
31
|
#'LIMIT', 0, 1000,
|
32
32
|
#'NOCONTENT', #'VERBATIM', #'WITHSCORES', #'NOSTOPWORDS', #'WITHPAYLOADS',
|
33
33
|
)
|
34
|
-
|
34
|
+
# => [1, "gid://application_name/User/unique_id", ["name", "Bob", "age", "100"]]
|
35
|
+
# => transform into array of hashes
|
36
|
+
output = []
|
37
|
+
results.shift # => remove count
|
38
|
+
results.each_slice(2) do |result|
|
39
|
+
attributes = {}
|
40
|
+
result[1].each_slice(2) do |attribute|
|
41
|
+
attributes[attribute[0]] = attribute[1]
|
42
|
+
end
|
43
|
+
output << {id: result[0]}.merge(attributes)
|
44
|
+
end
|
45
|
+
return output
|
35
46
|
rescue Exception => e
|
36
|
-
Rails.logger.error e
|
47
|
+
Rails.logger.error e if defined? Rails
|
37
48
|
return e.message
|
38
49
|
end
|
39
50
|
|
@@ -41,13 +52,13 @@ module RediSearchRails
|
|
41
52
|
#
|
42
53
|
# @return [String]
|
43
54
|
def ft_create
|
44
|
-
REDI_SEARCH.call('FT.CREATE',
|
55
|
+
REDI_SEARCH.call('FT.CREATE', @index_name,
|
45
56
|
#'NOFIELDS', 'NOSCOREIDX', 'NOOFFSETS',
|
46
|
-
'SCHEMA',
|
57
|
+
'SCHEMA', @schema
|
47
58
|
)
|
48
59
|
ft_optimize
|
49
60
|
rescue Exception => e
|
50
|
-
Rails.logger.error e
|
61
|
+
Rails.logger.error e if defined? Rails
|
51
62
|
return e.message
|
52
63
|
end
|
53
64
|
|
@@ -55,9 +66,9 @@ module RediSearchRails
|
|
55
66
|
#
|
56
67
|
# @return [String]
|
57
68
|
def ft_add_all
|
58
|
-
|
69
|
+
@model.all.each {|record| ft_add(record) }
|
59
70
|
rescue Exception => e
|
60
|
-
Rails.logger.error e
|
71
|
+
Rails.logger.error e if defined? Rails
|
61
72
|
return e.message
|
62
73
|
end
|
63
74
|
|
@@ -67,14 +78,14 @@ module RediSearchRails
|
|
67
78
|
# @return [String]
|
68
79
|
def ft_add record
|
69
80
|
fields = []
|
70
|
-
|
71
|
-
REDI_SEARCH.call('FT.ADD',
|
81
|
+
@fields.each { |field| fields.push(field, record.send(field)) }
|
82
|
+
REDI_SEARCH.call('FT.ADD', @index_name, record.to_global_id.to_s, @score,
|
72
83
|
'REPLACE',
|
73
84
|
#'NOSAVE', 'PAYLOAD', record.name,
|
74
85
|
'FIELDS', fields
|
75
86
|
)
|
76
87
|
rescue Exception => e
|
77
|
-
Rails.logger.error e
|
88
|
+
Rails.logger.error e if defined? Rails
|
78
89
|
return e.message
|
79
90
|
end
|
80
91
|
|
@@ -84,9 +95,9 @@ module RediSearchRails
|
|
84
95
|
# @return [String]
|
85
96
|
def ft_del record
|
86
97
|
doc_id = record.to_global_id
|
87
|
-
REDI_SEARCH.call('FT.DEL',
|
98
|
+
REDI_SEARCH.call('FT.DEL', @index_name, doc_id)
|
88
99
|
rescue Exception => e
|
89
|
-
Rails.logger.error e
|
100
|
+
Rails.logger.error e if defined? Rails
|
90
101
|
return e.message
|
91
102
|
end
|
92
103
|
|
@@ -94,9 +105,9 @@ module RediSearchRails
|
|
94
105
|
#
|
95
106
|
# @return [String]
|
96
107
|
def ft_optimize
|
97
|
-
REDI_SEARCH.call('FT.OPTIMIZE',
|
108
|
+
REDI_SEARCH.call('FT.OPTIMIZE', @index_name)
|
98
109
|
rescue Exception => e
|
99
|
-
Rails.logger.error e
|
110
|
+
Rails.logger.error e if defined? Rails
|
100
111
|
return e.message
|
101
112
|
end
|
102
113
|
|
@@ -104,9 +115,9 @@ module RediSearchRails
|
|
104
115
|
#
|
105
116
|
# @return [String]
|
106
117
|
def ft_drop
|
107
|
-
REDI_SEARCH.call('FT.DROP',
|
118
|
+
REDI_SEARCH.call('FT.DROP', @index_name)
|
108
119
|
rescue Exception => e
|
109
|
-
Rails.logger.error e
|
120
|
+
Rails.logger.error e if defined? Rails
|
110
121
|
return e.message
|
111
122
|
end
|
112
123
|
|
@@ -114,9 +125,9 @@ module RediSearchRails
|
|
114
125
|
#
|
115
126
|
# @return [String]
|
116
127
|
def ft_info
|
117
|
-
REDI_SEARCH.call('FT.INFO',
|
128
|
+
REDI_SEARCH.call('FT.INFO', @index_name)
|
118
129
|
rescue Exception => e
|
119
|
-
Rails.logger.error e
|
130
|
+
Rails.logger.error e if defined? Rails
|
120
131
|
return e.message
|
121
132
|
end
|
122
133
|
|
data/redi_search_rails.gemspec
CHANGED
@@ -33,8 +33,9 @@ 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", "
|
36
|
+
spec.add_development_dependency "activemodel", ">= 4.2.8"
|
37
|
+
spec.add_development_dependency "awesome_print"
|
37
38
|
# =>
|
38
|
-
spec.add_runtime_dependency "activesupport", "
|
39
|
+
spec.add_runtime_dependency "activesupport", ">= 4.2.8"
|
39
40
|
spec.add_runtime_dependency "redis", "~> 3.3"
|
40
41
|
end
|
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.2
|
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-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,30 +56,44 @@ dependencies:
|
|
56
56
|
name: activemodel
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 4.2.8
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 4.2.8
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: awesome_print
|
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'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: activesupport
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- - "
|
87
|
+
- - ">="
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
89
|
+
version: 4.2.8
|
76
90
|
type: :runtime
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- - "
|
94
|
+
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
96
|
+
version: 4.2.8
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: redis
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|