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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 382515dc3277d39c0c8389db1424fe6d73cda0ca
4
- data.tar.gz: 86727dec2fe4a6e642314d0a805571dfac49efd7
3
+ metadata.gz: 53fd6d815f84aaacba7a129c7e5bd574124b1937
4
+ data.tar.gz: 6e00184db536350ce5e3d30e9c25f182b7fb8ea1
5
5
  SHA512:
6
- metadata.gz: 50dc588fb78895feefec03a17d88a080b37fab8e285519a954583401509d41b5ce5a4c30c00d387a4a68771f69a89f57db932d427d57deda660b27046268dd2c
7
- data.tar.gz: ab851b7e3b939cc017c7ce0956ca6dfc3e075cf3e54e639a73cd8422d1ef5899d3861a3d9362127bb4482bec8158a312119b2e823eb227258a4a37a40f1be143
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
- [1, "gid://application_name/User/unique_id", ["name", "Bob", "age", "100"]]
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.
@@ -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
- @@schema = schema.to_a.flatten
18
- @@fields = schema.keys
19
- @@model = self.name.constantize
20
- @@index_name = @@model.to_s
21
- @@score = 1
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] [1, "gid://application_name/User/unique_id", ["name", "Bob", "age", "100"]]
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', @@index_name, keyword,
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
- return results
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', @@index_name,
55
+ REDI_SEARCH.call('FT.CREATE', @index_name,
45
56
  #'NOFIELDS', 'NOSCOREIDX', 'NOOFFSETS',
46
- 'SCHEMA', @@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
- @@model.all.each {|record| ft_add(record) }
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
- @@fields.each { |field| fields.push(field, record.send(field)) }
71
- REDI_SEARCH.call('FT.ADD', @@index_name, record.to_global_id, @@score,
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', @@index_name, doc_id)
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', @@index_name)
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', @@index_name)
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', @@index_name)
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
 
@@ -1,3 +1,3 @@
1
1
  module RediSearchRails
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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", "~> 5.0.0"
36
+ spec.add_development_dependency "activemodel", ">= 4.2.8"
37
+ spec.add_development_dependency "awesome_print"
37
38
  # =>
38
- spec.add_runtime_dependency "activesupport", "~> 5.0.0"
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.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-08 00:00:00.000000000 Z
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: 5.0.0
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: 5.0.0
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: 5.0.0
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: 5.0.0
96
+ version: 4.2.8
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: redis
85
99
  requirement: !ruby/object:Gem::Requirement