typesense-rails 1.0.0.rc1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e981befd9505d72dc1611c7953461d541f86c2c7725bad89389cbc959a6985db
4
+ data.tar.gz: 04ff3d8c767bd6714180da4604bde3540324b1cc24f295910742472fe4c5ffb0
5
+ SHA512:
6
+ metadata.gz: 0e4fa549a44fb57064738447a2fd198b8832ec6f154467e0a381bc0c46edd8e1260f1f36f2ea38fbe7e6cd7bbde4e7b4a96c2d366c80def2349d52234e450eed
7
+ data.tar.gz: 97d3bc5ac70c9f622b620eede98e456fe25589975bc3858f1ce51430f45db4e2f0f8c4122e768e47e9361da76dfeea5ac89dcce2b5608cc95cea645d299087c1
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,42 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'json', '>= 1.5.1'
4
+ gem "typesense", "~> 0.13.0"
5
+
6
+
7
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
8
+ gem 'rubysl', '~> 2.0', :platform => :rbx
9
+ end
10
+
11
+ group :test do
12
+ rails_version = ENV["RAILS_VERSION"] || '6.1'
13
+ gem 'rails', "~> #{rails_version}"
14
+ gem 'active_model_serializers'
15
+ if Gem::Version.new(rails_version) >= Gem::Version.new('6.0')
16
+ gem 'sqlite3', '~> 1.6.0', :platform => [:rbx, :ruby]
17
+ else
18
+ gem 'sqlite3', '< 1.4.0', :platform => [:rbx, :ruby]
19
+ end
20
+ gem 'rspec', '~> 3.0'
21
+ gem 'jdbc-sqlite3', :platform => :jruby
22
+ gem 'activerecord-jdbc-adapter', :platform => :jruby
23
+ gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
24
+ gem 'redgreen'
25
+
26
+ sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
27
+ gem 'sequel', sequel_version
28
+ end
29
+
30
+ group :development do
31
+ gem 'rake', '>= 10.1.0'
32
+ gem 'rdoc'
33
+ gem "rubocop", "~> 1.19"
34
+ end
35
+
36
+ group :test, :development do
37
+ gem 'will_paginate', '>= 2.3.15'
38
+ gem 'kaminari', '< 1'
39
+ gem 'pagy'
40
+ end
41
+
42
+ gem "lefthook", "~> 0.7.6"
data/Gemfile.lock ADDED
@@ -0,0 +1,260 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actioncable (6.1.7.10)
5
+ actionpack (= 6.1.7.10)
6
+ activesupport (= 6.1.7.10)
7
+ nio4r (~> 2.0)
8
+ websocket-driver (>= 0.6.1)
9
+ actionmailbox (6.1.7.10)
10
+ actionpack (= 6.1.7.10)
11
+ activejob (= 6.1.7.10)
12
+ activerecord (= 6.1.7.10)
13
+ activestorage (= 6.1.7.10)
14
+ activesupport (= 6.1.7.10)
15
+ mail (>= 2.7.1)
16
+ actionmailer (6.1.7.10)
17
+ actionpack (= 6.1.7.10)
18
+ actionview (= 6.1.7.10)
19
+ activejob (= 6.1.7.10)
20
+ activesupport (= 6.1.7.10)
21
+ mail (~> 2.5, >= 2.5.4)
22
+ rails-dom-testing (~> 2.0)
23
+ actionpack (6.1.7.10)
24
+ actionview (= 6.1.7.10)
25
+ activesupport (= 6.1.7.10)
26
+ rack (~> 2.0, >= 2.0.9)
27
+ rack-test (>= 0.6.3)
28
+ rails-dom-testing (~> 2.0)
29
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
30
+ actiontext (6.1.7.10)
31
+ actionpack (= 6.1.7.10)
32
+ activerecord (= 6.1.7.10)
33
+ activestorage (= 6.1.7.10)
34
+ activesupport (= 6.1.7.10)
35
+ nokogiri (>= 1.8.5)
36
+ actionview (6.1.7.10)
37
+ activesupport (= 6.1.7.10)
38
+ builder (~> 3.1)
39
+ erubi (~> 1.4)
40
+ rails-dom-testing (~> 2.0)
41
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
42
+ active_model_serializers (0.10.15)
43
+ actionpack (>= 4.1)
44
+ activemodel (>= 4.1)
45
+ case_transform (>= 0.2)
46
+ jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
47
+ activejob (6.1.7.10)
48
+ activesupport (= 6.1.7.10)
49
+ globalid (>= 0.3.6)
50
+ activemodel (6.1.7.10)
51
+ activesupport (= 6.1.7.10)
52
+ activerecord (6.1.7.10)
53
+ activemodel (= 6.1.7.10)
54
+ activesupport (= 6.1.7.10)
55
+ activestorage (6.1.7.10)
56
+ actionpack (= 6.1.7.10)
57
+ activejob (= 6.1.7.10)
58
+ activerecord (= 6.1.7.10)
59
+ activesupport (= 6.1.7.10)
60
+ marcel (~> 1.0)
61
+ mini_mime (>= 1.1.0)
62
+ activesupport (6.1.7.10)
63
+ concurrent-ruby (~> 1.0, >= 1.0.2)
64
+ i18n (>= 1.6, < 2)
65
+ minitest (>= 5.1)
66
+ tzinfo (~> 2.0)
67
+ zeitwerk (~> 2.3)
68
+ ast (2.4.3)
69
+ base64 (0.2.0)
70
+ bigdecimal (3.1.9)
71
+ builder (3.3.0)
72
+ case_transform (0.2)
73
+ activesupport
74
+ cgi (0.4.2)
75
+ concurrent-ruby (1.3.5)
76
+ crass (1.0.6)
77
+ date (3.4.1)
78
+ diff-lcs (1.6.2)
79
+ erb (4.0.4)
80
+ cgi (>= 0.3.3)
81
+ erubi (1.13.1)
82
+ ethon (0.16.0)
83
+ ffi (>= 1.15.0)
84
+ ffi (1.17.2)
85
+ globalid (1.2.1)
86
+ activesupport (>= 6.1)
87
+ i18n (1.14.7)
88
+ concurrent-ruby (~> 1.0)
89
+ json (2.12.0)
90
+ jsonapi-renderer (0.2.2)
91
+ kaminari (0.17.0)
92
+ actionpack (>= 3.0.0)
93
+ activesupport (>= 3.0.0)
94
+ language_server-protocol (3.17.0.5)
95
+ lefthook (0.7.7)
96
+ lint_roller (1.1.0)
97
+ logger (1.7.0)
98
+ loofah (2.24.1)
99
+ crass (~> 1.0.2)
100
+ nokogiri (>= 1.12.0)
101
+ mail (2.8.1)
102
+ mini_mime (>= 0.1.1)
103
+ net-imap
104
+ net-pop
105
+ net-smtp
106
+ marcel (1.0.4)
107
+ method_source (1.1.0)
108
+ mini_mime (1.1.5)
109
+ mini_portile2 (2.8.9)
110
+ minitest (5.25.5)
111
+ net-imap (0.4.22)
112
+ date
113
+ net-protocol
114
+ net-pop (0.1.2)
115
+ net-protocol
116
+ net-protocol (0.2.2)
117
+ timeout
118
+ net-smtp (0.5.1)
119
+ net-protocol
120
+ nio4r (2.7.4)
121
+ nokogiri (1.15.7)
122
+ mini_portile2 (~> 2.8.2)
123
+ racc (~> 1.4)
124
+ oj (3.16.10)
125
+ bigdecimal (>= 3.0)
126
+ ostruct (>= 0.2)
127
+ ostruct (0.6.1)
128
+ pagy (6.5.0)
129
+ parallel (1.27.0)
130
+ parser (3.3.8.0)
131
+ ast (~> 2.4.1)
132
+ racc
133
+ prism (1.4.0)
134
+ psych (5.2.6)
135
+ date
136
+ stringio
137
+ racc (1.8.1)
138
+ rack (2.2.15)
139
+ rack-test (2.2.0)
140
+ rack (>= 1.3)
141
+ rails (6.1.7.10)
142
+ actioncable (= 6.1.7.10)
143
+ actionmailbox (= 6.1.7.10)
144
+ actionmailer (= 6.1.7.10)
145
+ actionpack (= 6.1.7.10)
146
+ actiontext (= 6.1.7.10)
147
+ actionview (= 6.1.7.10)
148
+ activejob (= 6.1.7.10)
149
+ activemodel (= 6.1.7.10)
150
+ activerecord (= 6.1.7.10)
151
+ activestorage (= 6.1.7.10)
152
+ activesupport (= 6.1.7.10)
153
+ bundler (>= 1.15.0)
154
+ railties (= 6.1.7.10)
155
+ sprockets-rails (>= 2.0.0)
156
+ rails-dom-testing (2.3.0)
157
+ activesupport (>= 5.0.0)
158
+ minitest
159
+ nokogiri (>= 1.6)
160
+ rails-html-sanitizer (1.6.2)
161
+ loofah (~> 2.21)
162
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
163
+ railties (6.1.7.10)
164
+ actionpack (= 6.1.7.10)
165
+ activesupport (= 6.1.7.10)
166
+ method_source
167
+ rake (>= 12.2)
168
+ thor (~> 1.0)
169
+ rainbow (3.1.1)
170
+ rake (13.2.1)
171
+ rdoc (6.14.0)
172
+ erb
173
+ psych (>= 4.0.0)
174
+ redgreen (1.2.2)
175
+ regexp_parser (2.10.0)
176
+ rspec (3.13.0)
177
+ rspec-core (~> 3.13.0)
178
+ rspec-expectations (~> 3.13.0)
179
+ rspec-mocks (~> 3.13.0)
180
+ rspec-core (3.13.3)
181
+ rspec-support (~> 3.13.0)
182
+ rspec-expectations (3.13.4)
183
+ diff-lcs (>= 1.2.0, < 2.0)
184
+ rspec-support (~> 3.13.0)
185
+ rspec-mocks (3.13.4)
186
+ diff-lcs (>= 1.2.0, < 2.0)
187
+ rspec-support (~> 3.13.0)
188
+ rspec-support (3.13.3)
189
+ rubocop (1.75.6)
190
+ json (~> 2.3)
191
+ language_server-protocol (~> 3.17.0.2)
192
+ lint_roller (~> 1.1.0)
193
+ parallel (~> 1.10)
194
+ parser (>= 3.3.0.2)
195
+ rainbow (>= 2.2.2, < 4.0)
196
+ regexp_parser (>= 2.9.3, < 3.0)
197
+ rubocop-ast (>= 1.44.0, < 2.0)
198
+ ruby-progressbar (~> 1.7)
199
+ unicode-display_width (>= 2.4.0, < 4.0)
200
+ rubocop-ast (1.44.1)
201
+ parser (>= 3.3.7.2)
202
+ prism (~> 1.4)
203
+ ruby-progressbar (1.13.0)
204
+ sequel (5.92.0)
205
+ bigdecimal
206
+ sprockets (4.2.2)
207
+ concurrent-ruby (~> 1.0)
208
+ logger
209
+ rack (>= 2.2.4, < 4)
210
+ sprockets-rails (3.5.2)
211
+ actionpack (>= 6.1)
212
+ activesupport (>= 6.1)
213
+ sprockets (>= 3.0.0)
214
+ sqlite3 (1.6.9)
215
+ mini_portile2 (~> 2.8.0)
216
+ stringio (3.1.7)
217
+ thor (1.3.2)
218
+ timeout (0.4.3)
219
+ typesense (0.13.0)
220
+ oj (~> 3.11)
221
+ typhoeus (~> 1.4)
222
+ typhoeus (1.4.1)
223
+ ethon (>= 0.9.0)
224
+ tzinfo (2.0.6)
225
+ concurrent-ruby (~> 1.0)
226
+ unicode-display_width (3.1.4)
227
+ unicode-emoji (~> 4.0, >= 4.0.4)
228
+ unicode-emoji (4.0.4)
229
+ websocket-driver (0.7.7)
230
+ base64
231
+ websocket-extensions (>= 0.1.0)
232
+ websocket-extensions (0.1.5)
233
+ will_paginate (4.0.1)
234
+ zeitwerk (2.6.18)
235
+
236
+ PLATFORMS
237
+ ruby
238
+
239
+ DEPENDENCIES
240
+ active_model_serializers
241
+ activerecord-jdbc-adapter
242
+ activerecord-jdbcsqlite3-adapter
243
+ jdbc-sqlite3
244
+ json (>= 1.5.1)
245
+ kaminari (< 1)
246
+ lefthook (~> 0.7.6)
247
+ pagy
248
+ rails (~> 6.1)
249
+ rake (>= 10.1.0)
250
+ rdoc
251
+ redgreen
252
+ rspec (~> 3.0)
253
+ rubocop (~> 1.19)
254
+ sequel (>= 4.0)
255
+ sqlite3 (~> 1.6.0)
256
+ typesense (~> 0.13.0)
257
+ will_paginate (>= 2.3.15)
258
+
259
+ BUNDLED WITH
260
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-Present Algolia
4
+ Copyright (c) 2021-Present Typesense
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,239 @@
1
+ # typesense-rails
2
+
3
+ A Ruby on Rails integration for Typesense search engine that provides seamless integration with ActiveRecord, Sequel and Mongoid models.
4
+
5
+ This gem is a fork of the [algolia-rails](https://github.com/algolia/algoliasearch-rails) gem, adapted to work with Typesense while maintaining similar functionality and API patterns.
6
+ The core integration patterns and much of the functionality were inspired by Algolia's excellent work.
7
+ Special thanks to the Algolia team for their original implementation, which provided a solid foundation for this Typesense integration.
8
+
9
+ ## Features
10
+
11
+ - Seamless integration with ActiveRecord, Sequel, and Mongoid models
12
+ - Automatic indexing of model data with callbacks
13
+ - Support for multiple pagination backends (Kaminari, WillPaginate, Pagy)
14
+ - Rich attribute customization and serialization
15
+ - Support for nested relationships and hierarchical data
16
+ - Background job processing via ActiveJob integration
17
+ - Conditional indexing based on model state
18
+ - Environment-specific indexing
19
+ - Support for faceted search and filtering
20
+ - Customizable schema with predefined fields
21
+ - Multi-way and one-way synonyms support
22
+ - Rake tasks for index management
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'typesense-rails', '~> 1.0.0.rc1'
30
+ ```
31
+
32
+ Then execute:
33
+
34
+ ```bash
35
+ $ bundle install
36
+ ```
37
+
38
+ ## Configuration
39
+
40
+ Initialize Typesense in your Rails application (e.g., in `config/initializers/typesense.rb`):
41
+
42
+ ```ruby
43
+ Typesense.configuration = {
44
+ nodes: [{
45
+ host: 'localhost', # For Typesense Cloud use xxx.a1.typesense.net
46
+ port: '8108', # For Typesense Cloud use 443
47
+ protocol: 'http' # For Typesense Cloud use https
48
+ }],
49
+ api_key: 'your-api-key',
50
+ connection_timeout_seconds: 2
51
+ }
52
+ ```
53
+
54
+ ## Usage
55
+
56
+ ### Basic Model Configuration
57
+
58
+ ```ruby
59
+ class Product < ApplicationRecord
60
+ include Typesense
61
+
62
+ typesense do
63
+ # Define attributes to be indexed
64
+ attributes :name, :description, :price
65
+
66
+ # Add dynamic attributes
67
+ attribute :full_name do
68
+ "#{first_name} #{last_name}"
69
+ end
70
+
71
+ # Define predefined fields with specific types
72
+ predefined_fields [
73
+ { 'name' => 'name', 'type' => 'string', 'facet' => true },
74
+ { 'name' => 'price', 'type' => 'float' }
75
+ ]
76
+
77
+ # Set default sorting
78
+ default_sorting_field 'price'
79
+
80
+ # Configure synonyms
81
+ multi_way_synonyms [
82
+ { "phone-synonym" => %w[galaxy samsung samsung_electronics] }
83
+ ]
84
+
85
+ one_way_synonyms [
86
+ { "smart-phone-synonym" => {
87
+ "root" => "smartphone",
88
+ "synonyms" => %w[nokia samsung motorola android]
89
+ }
90
+ }
91
+ ]
92
+
93
+ # Symbols to index
94
+ symbols_to_index ["-", "_"]
95
+
96
+ # Token separators
97
+ token_separator ["-", "_"]
98
+
99
+ # Enable nested fields
100
+ enable_nested_fields true
101
+ end
102
+ end
103
+ ```
104
+
105
+ ### Working with Relationships
106
+
107
+ ```ruby
108
+ class Profile < ApplicationRecord
109
+ include Typesense
110
+ belongs_to :user
111
+ has_many :specializations
112
+
113
+ typesense do
114
+ # Nest user data
115
+ attribute :user do
116
+ { name: user.name, email: user.email }
117
+ end
118
+
119
+ # Include related data
120
+ attribute :specializations do
121
+ specializations.select(&:public?).map do |s|
122
+ { title: s.title, category: s.category }
123
+ end
124
+ end
125
+ end
126
+ end
127
+ ```
128
+
129
+ ### Searching
130
+
131
+ ```ruby
132
+ # Basic search
133
+ results = Product.search('phone', 'name')
134
+
135
+ # Search with filters and sorting
136
+ results = Product.search('phone', 'name', {
137
+ filter_by: 'price:< 500',
138
+ sort_by: 'price:asc'
139
+ })
140
+
141
+ # Faceted search
142
+ results = Product.search('*', '', {
143
+ facet_by: 'category',
144
+ per_page: 20
145
+ })
146
+ ```
147
+
148
+ ### Indexing
149
+
150
+ ```ruby
151
+ # Reindex all records (with zero-downtime)
152
+ Product.reindex
153
+
154
+ # Regular reindexing (overwrites existing records)
155
+ Product.reindex!
156
+
157
+ # Index specific records
158
+ Product.where('updated_at > ?', 10.minutes.ago).reindex!
159
+
160
+ # Index a single record
161
+ product.index!
162
+
163
+ # Remove from index
164
+ product.remove_from_index!
165
+
166
+ # Conditional indexing
167
+ typesense if: :published?, unless: :draft? do
168
+ # indexing configuration
169
+ end
170
+ ```
171
+
172
+ ### Background Jobs
173
+
174
+ Process indexing operations asynchronously using ActiveJob:
175
+
176
+ ```ruby
177
+ class Product < ApplicationRecord
178
+ include Typesense
179
+
180
+ typesense enqueue: true do
181
+ attributes :name, :description, :price
182
+ end
183
+ end
184
+ ```
185
+
186
+ Or define custom job processing:
187
+
188
+ ```ruby
189
+ class Product < ApplicationRecord
190
+ include Typesense
191
+
192
+ typesense enqueue: :trigger_worker do
193
+ attributes :name, :description, :price
194
+ end
195
+
196
+ def self.trigger_worker(record, remove)
197
+ IndexingWorker.perform_async(record.id, remove)
198
+ end
199
+ end
200
+ ```
201
+
202
+ ## Pagination Support
203
+
204
+ The gem supports multiple pagination backends:
205
+
206
+ ```ruby
207
+ # Configure pagination backend
208
+ Typesense.configuration = {
209
+ # ... other configuration ...
210
+ pagination_backend: :kaminari # or :will_paginate or :pagy
211
+ }
212
+ ```
213
+
214
+ ## Testing
215
+
216
+ To run the test suite:
217
+
218
+ ```bash
219
+ bundle install
220
+ bundle exec rake
221
+ ```
222
+
223
+ ## Contributing
224
+
225
+ 1. Fork the repository
226
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
227
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
228
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
229
+ 5. Create a new Pull Request
230
+
231
+ Please make sure to update tests as appropriate and follow the existing coding style.
232
+
233
+ ### Releasing
234
+
235
+ 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).
236
+
237
+ ## License
238
+
239
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'bundler/gem_tasks'
4
+
5
+ require 'rdoc/task'
6
+ Rake::RDocTask.new do |rdoc|
7
+ version = File.exist?('VERSION') ? File.read('VERSION') : ''
8
+
9
+ rdoc.rdoc_dir = 'rdoc'
10
+ rdoc.title = "Typesense Rails #{version}"
11
+ rdoc.rdoc_files.include('README*')
12
+ rdoc.rdoc_files.include('lib/**/*.rb')
13
+ end
14
+
15
+ require 'rspec/core/rake_task'
16
+ RSpec::Core::RakeTask.new(:spec)
17
+
18
+ task default: :spec
@@ -0,0 +1,30 @@
1
+ module Typesense
2
+ module Config
3
+ def initiliaze
4
+ @client = nil
5
+ end
6
+
7
+ def configuration
8
+ @@configuration || raise(NotConfigured,
9
+ "Please configure Typesense. Set Typesense.configuration = {nodes: [{host: 'localhost', port: 8108, protocol: 'http'}], api_key: 'xyz'}")
10
+ end
11
+
12
+ def configuration=(configuration)
13
+ @@pagination_backend = configuration[:pagination_backend] if configuration.key?(:pagination_backend)
14
+ @@configuration = configuration
15
+ end
16
+
17
+ def pagination_backend
18
+ @@pagination_backend
19
+ end
20
+
21
+ def client
22
+ setup_client if @client.nil?
23
+ @client
24
+ end
25
+
26
+ def setup_client
27
+ @client = Typesense::Client.new(@@configuration)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module Typesense
2
+ class ImportJob < ::ActiveJob::Base
3
+ queue_as { Typesense.configuration[:queue_name] }
4
+
5
+ def self.perform(jsonl_object, collection_name, batch_size)
6
+ new.perform_later(jsonl_object, collection_name, batch_size)
7
+ end
8
+
9
+ def perform_later(jsonl_object, collection_name, batch_size)
10
+ # Initialize client with longer timeout for batch operations
11
+ client = Typesense::Client.new(Typesense.configuration.merge(
12
+ connection_timeout_seconds: 3600,
13
+ ))
14
+
15
+ client.collections[collection_name].documents.import(jsonl_object, {
16
+ action: "upsert",
17
+ batch_size: batch_size,
18
+ })
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ unless defined? Kaminari
2
+ raise(Typesense::BadConfiguration, "Typesense: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
3
+ end
4
+
5
+ require 'kaminari/models/array_extension'
6
+
7
+ module Typesense
8
+ module Pagination
9
+ class Kaminari < ::Kaminari::PaginatableArray
10
+ def initialize(array, options)
11
+ super(array, **options)
12
+ end
13
+
14
+ def limit(_num)
15
+ # noop
16
+ self
17
+ end
18
+
19
+ def offset(_num)
20
+ # noop
21
+ self
22
+ end
23
+
24
+ class << self
25
+ def create(results, total_hits, options = {})
26
+ offset = ((options[:page] - 1) * options[:per_page])
27
+ array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
28
+ if array.empty? && !results.empty?
29
+ # since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
30
+ # otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
31
+ results = ([nil] * offset) + results
32
+ array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
33
+ end
34
+ array
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end