stretchy-model 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1709205b5fe75817fe79c5047b6f4d3531257cf8a3118f84d8fa4b92adb8932
4
- data.tar.gz: 1add1b8c78f9c27362d31e0f1f8c141a429af4b6176b7d521e32e8a3e9230039
3
+ metadata.gz: a85f7bffcc11c54366d4cae7fec47b91341425e06b7f1c8a6269e0b23d4e649b
4
+ data.tar.gz: cd0b9314fa734aa7af7179f12ed00626a17bc869186f82088158bfca87c8d915
5
5
  SHA512:
6
- metadata.gz: cf690ed5affe0224370f50e5208a6fcd0a91e611460fecf3f9a024464b0c87fa9b2718932fefca49c4ad9d587c4cc956b3490738fd794176f35d8f180dc16998
7
- data.tar.gz: bc518187e999e3e989332612294ec8152bcc673fd5c025683e1170e8549491d8f8f3b578d4feb82a8fc4b564811f625eaae21fa5b754f0cd8fc7e36ae136c744
6
+ metadata.gz: e193c837695100177eac9888841b14243596dfc9694515696ab1a9934174287c821cf52b2ca165e3f3eda04de9ba16c7c7330cb187641d71229d322c06674977
7
+ data.tar.gz: 21ace7b64f85d505a05b61b89b5b85d9d2af34941b57672da45f70e6c8736e8b9451c59891fef7d0795f8b54076cffd88bfc141fcd4da1c1f8ffaddcf8276697
data/README.md CHANGED
@@ -118,6 +118,32 @@ If bundler is not being used to manage dependencies, install the gem by executin
118
118
 
119
119
  $ gem install stretchy-model
120
120
 
121
+ <details>
122
+ <summary>Rails Configuration</summary>
123
+
124
+
125
+
126
+ ```sh
127
+ rails credentials:edit
128
+ ```
129
+
130
+ #### Add elasticsearch credentials
131
+ ```yaml
132
+ elasticsearch:
133
+ url: localhost:9200
134
+ ```
135
+
136
+ #### Create an initializer
137
+ <p><sub><em>config/initializers/stretchy.rb</em></sub></p>
138
+
139
+ ```ruby {file=config/initializers/stretchy.rb}
140
+ Stretchy.configure do |config|
141
+ config.client = Elasticsearch::Client.new url: Rails.application.credentials.elasticsearch.url, log: true
142
+ end
143
+ ```
144
+ </details>
145
+
146
+
121
147
  ## Development
122
148
 
123
149
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -128,6 +154,10 @@ After checking out the repo, run `bin/setup` to install dependencies. You can al
128
154
  > Full documentation on [Elasticsearch Query DSL and Aggregation options](https://github.com/elastic/elasticsearch-rails/tree/main/elasticsearch-persistence)
129
155
 
130
156
  ## Testing
157
+ <details>
158
+ <summary>Elasticsearch</summary>
159
+
160
+
131
161
  ```
132
162
  docker-compose up elasticsearch
133
163
  ```
@@ -136,6 +166,21 @@ docker-compose up elasticsearch
136
166
  bundle exec rspec
137
167
  ```
138
168
 
169
+ </details>
170
+
171
+ <details>
172
+ <summary>Opensearch</summary>
173
+
174
+
175
+ ```
176
+ docker-compose up opensearch
177
+ ```
178
+
179
+ ```
180
+ ENV['BACKEND']=opensearch bundle rspec
181
+ ```
182
+ </details>
183
+
139
184
  ## Contributing
140
185
 
141
186
  Bug reports and pull requests are welcome on GitHub at https://github.com/theablefew/stretchy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/theablefew/stretchy/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,86 @@
1
+ module Stretchy
2
+ module OpenSearchCompatibility
3
+ extend ActiveSupport::Concern
4
+
5
+ # Patches the Elasticsearch::Persistence::Repository::Search module to remove the
6
+ # document type from the request for compatability with OpenSearch
7
+ def self.opensearch_patch!
8
+ patch = Module.new do
9
+ def search(query_or_definition, options={})
10
+ request = { index: index_name }
11
+
12
+ if query_or_definition.respond_to?(:to_hash)
13
+ request[:body] = query_or_definition.to_hash
14
+ elsif query_or_definition.is_a?(String)
15
+ request[:q] = query_or_definition
16
+ else
17
+ raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String" +
18
+ " -- #{query_or_definition.class} given."
19
+ end
20
+
21
+ Elasticsearch::Persistence::Repository::Response::Results.new(self, client.search(request.merge(options)))
22
+ end
23
+
24
+ def count(query_or_definition=nil, options={})
25
+ query_or_definition ||= { query: { match_all: {} } }
26
+ request = { index: index_name}
27
+
28
+ if query_or_definition.respond_to?(:to_hash)
29
+ request[:body] = query_or_definition.to_hash
30
+ elsif query_or_definition.is_a?(String)
31
+ request[:q] = query_or_definition
32
+ else
33
+ raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String" +
34
+ " -- #{query_or_definition.class} given."
35
+ end
36
+
37
+ client.count(request.merge(options))['count']
38
+ end
39
+ end
40
+
41
+ store = Module.new do
42
+ def save(document, options={})
43
+ serialized = serialize(document)
44
+ id = __get_id_from_document(serialized)
45
+ request = { index: index_name,
46
+ id: id,
47
+ body: serialized }
48
+ client.index(request.merge(options))
49
+ end
50
+
51
+
52
+ def update(document_or_id, options = {})
53
+ if document_or_id.is_a?(String) || document_or_id.is_a?(Integer)
54
+ id = document_or_id
55
+ body = options
56
+ else
57
+ document = serialize(document_or_id)
58
+ id = __extract_id_from_document(document)
59
+ if options[:script]
60
+ body = options
61
+ else
62
+ body = { doc: document }.merge(options)
63
+ end
64
+ end
65
+ client.update(index: index_name, id: id, body: body)
66
+ end
67
+
68
+ def delete(document_or_id, options = {})
69
+ if document_or_id.is_a?(String) || document_or_id.is_a?(Integer)
70
+ id = document_or_id
71
+ else
72
+ serialized = serialize(document_or_id)
73
+ id = __get_id_from_document(serialized)
74
+ end
75
+ client.delete({ index: index_name, id: id }.merge(options))
76
+ end
77
+ end
78
+
79
+
80
+ ::Elasticsearch::Persistence::Repository.send(:include, patch)
81
+ ::Elasticsearch::Persistence::Repository.send(:include, store)
82
+ end
83
+
84
+
85
+ end
86
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stretchy
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/stretchy.rb CHANGED
@@ -24,9 +24,25 @@ module Stretchy
24
24
  class Configuration
25
25
 
26
26
  attr_accessor :client
27
+ attr_accessor :opensearch
27
28
 
28
29
  def initialize
29
30
  @client = Elasticsearch::Client.new url: 'http://localhost:9200'
31
+ @opensearch = false
32
+ end
33
+
34
+ def client=(client)
35
+ @client = client
36
+ self.opensearch = true if @client.class.name =~ /OpenSearch/
37
+ end
38
+
39
+ def opensearch=(bool)
40
+ @opensearch = bool
41
+ OpenSearchCompatibility.opensearch_patch! if bool
42
+ end
43
+
44
+ def opensearch?
45
+ @opensearch
30
46
  end
31
47
 
32
48
  end
@@ -45,9 +61,10 @@ module Stretchy
45
61
  end
46
62
  end
47
63
 
48
-
49
64
  end
50
65
 
66
+
67
+
51
68
  loader = Zeitwerk::Loader.new
52
69
  loader.tag = File.basename(__FILE__, ".rb")
53
70
  loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stretchy-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spencer Markowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-05 00:00:00.000000000 Z
11
+ date: 2024-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: 0.9.36
167
+ - !ruby/object:Gem::Dependency
168
+ name: opensearch-ruby
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '3.0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '3.0'
167
181
  description: Provides a familiar ActiveRecord-like interface for working with Elasticsearch
168
182
  email:
169
183
  - spencer.markowski@theablefew.com
@@ -197,6 +211,7 @@ files:
197
211
  - lib/stretchy/model/callbacks.rb
198
212
  - lib/stretchy/model/serialization.rb
199
213
  - lib/stretchy/null_relation.rb
214
+ - lib/stretchy/open_search_compatibility.rb
200
215
  - lib/stretchy/persistence.rb
201
216
  - lib/stretchy/querying.rb
202
217
  - lib/stretchy/record.rb