mongodb_meilisearch 1.1.0 → 1.1.1

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
  SHA256:
3
- metadata.gz: f57c18174244b2885536a990a2d602ce833e949dcf205112c78f30567a6eee6a
4
- data.tar.gz: bb5bdd6c8cb90a6e1334c4606063a56b71515510f0f45c3e1ae2d817b4497163
3
+ metadata.gz: c7873b1a21e75522aa4fdaa063231997319faae90ddb9a4774cd814bf18a4afc
4
+ data.tar.gz: b4f241003a1ff709c1247f6411cec39831459795d8e17c429a9b7b187ff790cd
5
5
  SHA512:
6
- metadata.gz: cb3e4f69dad6365682470020e3acc864492c45d79de54fb65fbab6fb9eaacb0811d8af02414f4df324a9d0cf64278646e0eac3062e7aff1a9873aa2f264d0d64
7
- data.tar.gz: 54aa4eea1cfe0b378e9ad93c719eb31a5c35c96c08ef842d975efb8391cf1b55b0ca53e9ca3d8daf98626a02c30ea817e9623c78de0b6ce14ade726473767304
6
+ metadata.gz: 8ad38b1b7f7c14baaee4c12491affeb62c9b9a700ec41d3bddcdf3c095288c084c9bf8f6c9feb28300b441e5fd94380179c9f334c7d2df078d48410221fbc05f
7
+ data.tar.gz: 5792131d06698d2af16194078e1d5a8bfbf37b9084502f25183d5f3c347d712d2eeac7e604edf0298800a03795e573336adbdc9a5b1d47929691f9288e974027
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongodb_meilisearch (1.1.0)
4
+ mongodb_meilisearch (1.1.1)
5
5
  meilisearch
6
6
  mongoid (~> 7.0)
7
7
  rails
data/README.md CHANGED
@@ -323,6 +323,16 @@ Be careful to not add documents that are already in the index.
323
323
  WARNING: if you think you should use this, you're probably
324
324
  mistaken.
325
325
 
326
+ #### Indexes
327
+ By default every model gets its own search index. This means that
328
+ `Foo.search("some text")` will only search `Foo` objects. To have a
329
+ search cross objects you'll need to use a "Shared Index" (see below).
330
+
331
+
332
+ The name of the index isn't important when not using shared indexes.
333
+ By default a model's index is the snake cased form of the class name.
334
+ For example, data for `MyWidget` models will be stored in the `my_widget` index.
335
+
326
336
  #### Shared indexes
327
337
  Imagine you have a `Note` and a `Comment` model, sharing an index so that
328
338
  you can perform a single search and have search results for both models
@@ -442,10 +452,14 @@ To invoke any of Meilisearch's custom search options (see [their documentation](
442
452
 
443
453
  `MyModel.search("search term", options: <my custom options>)`
444
454
 
445
- The Meilisearch-ruby gem should be able to convert keys from snake case to
455
+ Currently the Meilisearch-ruby gem can convert keys from snake case to
446
456
  camel case. For example `hits_per_page` will become `hitsPerPage`.
447
- Meilisearch ultimately wants camel case. Follow their documentation
448
- to see what's available and what type of options to pass it. Note that your
457
+ Meilisearch ultimately wants camel case (`camelCase`) parameter keys,
458
+ _but_ `meilisearch-ruby` wants snake case (`snake_case`).
459
+
460
+ Follow Meilisearch's documentation
461
+ to see what's available and what type of options to pass it, but convert
462
+ them to snake case first. Note that your
449
463
  options keys and values must all be simple JSON values.
450
464
 
451
465
  If for some reason that still isn't enough, you can work with the
@@ -5,5 +5,5 @@ module MongodbMeilisearch
5
5
  # @note This library will adhere to strict semantic versioning.
6
6
  # See https://semver.org/
7
7
  #
8
- VERSION = "1.1.0"
8
+ VERSION = "1.1.1"
9
9
  end
@@ -109,6 +109,7 @@ module Search
109
109
  # @param filtered_by_class [Boolean] - defaults to filtering results by the class
110
110
  # your searching from. Ex. Foo.search("something") it will
111
111
  # have Meilisearch filter on records where `object_class` == `"Foo"`
112
+ # This simplifies working with shared indexes.
112
113
  # @return [Hash[String, [Array[String | Document]] a hash with keys corresponding
113
114
  # to the classes of objects returned, and a value of an array of ids or
114
115
  # Mongoid::Document objects sorted by match strength. It will ALSO have a key named
@@ -135,13 +136,13 @@ module Search
135
136
 
136
137
  # TODO: break this if/else out into a separate function
137
138
  if ids_only
138
- options.merge!({attributesToRetrieve: [pk.to_s]})
139
+ options.merge!({attributes_to_retrieve: [pk.to_s]})
139
140
  else
140
141
  # Don't care what you add, but we need the primary key and object_class
141
- options[:attributesToRetrieve] = [] unless options.has_key?(:attributesToRetrieve)
142
+ options[:attributes_to_retrieve] = [] unless options.has_key?(:attributes_to_retrieve)
142
143
  [pk.to_s, "object_class"].each do |attr|
143
- unless options[:attributesToRetrieve].include?(attr)
144
- options[:attributesToRetrieve] << attr
144
+ unless options[:attributes_to_retrieve].include?(attr)
145
+ options[:attributes_to_retrieve] << attr
145
146
  end
146
147
  end
147
148
  end
@@ -260,6 +261,7 @@ module Search
260
261
  # - each document hash is presumed to have been created by way of
261
262
  # search_indexable_hash
262
263
  def add_documents(new_documents, async: true)
264
+ configure_attributes_and_index_if_needed!
263
265
  if async
264
266
  search_index.add_documents(new_documents, primary_search_key)
265
267
  else
@@ -393,17 +395,38 @@ module Search
393
395
  # @return [Array] - an array of attributes configured as sortable
394
396
  # in the index.
395
397
  def meilisearch_sortable_attributes
396
- # Search::Client.instance.http_get(
397
- search_index.http_get(
398
- "/indexes/#{search_index}/settings/sortable-attributes"
399
- )
398
+ @_meilisearch_sortable_attributes ||= search_index.get_sortable_attributes
400
399
  end
401
400
 
402
401
  def meilisearch_filterable_attributes
403
- # Search::Client.instance.http_get(
404
- search_index.http_get(
405
- "/indexes/#{search_index}/settings/filterable-attributes"
406
- )
402
+ @_meilisearch_filterable_attributes ||= search_index.get_filterable_attributes
403
+ end
404
+
405
+ def reset_cached_data!
406
+ @_meilisearch_filterable_attributes = nil
407
+ @_filterable_attributes = nil
408
+ @_meilisearch_sortable_attributes = nil
409
+ @_sortable_attributes = nil
410
+ end
411
+
412
+ # Guarantees that the filterable attributes have been configured
413
+ # This should be called before
414
+ def configure_attributes_and_index_if_needed!
415
+ return if unfilterable?
416
+ indexes_filterable_attributes = []
417
+
418
+ begin
419
+ indexes_filterable_attributes = meilisearch_filterable_attributes
420
+ rescue ::MeiliSearch::ApiError => e
421
+ # this is expected to happen the first time an instance
422
+ # of a new model is saved.
423
+ raise unless e.message.match?(/Index `\S+` not found\./)
424
+ Search::Client.instance.create_index(search_index_name)
425
+ end
426
+
427
+ return if indexes_filterable_attributes.include?("object_class")
428
+ set_filterable_attributes
429
+ set_sortable_attributes
407
430
  end
408
431
 
409
432
  # Updates the filterable attributes in the search index.
@@ -579,7 +602,7 @@ module Search
579
602
  def reindex_core(async: true)
580
603
  # no point in continuing if this fails...
581
604
  delete_all_documents!
582
-
605
+ reset_cached_data!
583
606
  # this conveniently lines up with the batch size of 100
584
607
  # that Mongoid gives us
585
608
  documents = []
data/lib/search/client.rb CHANGED
@@ -30,7 +30,7 @@ module Search
30
30
  if @client.respond_to? m.to_sym
31
31
  @client.send(m, *args, &block)
32
32
  else
33
- raise ArgumentError.new("Method `#{m}` doesn't exist.")
33
+ raise ArgumentError.new("Method `#{m}` doesn't exist in #{@client.inspect}.")
34
34
  end
35
35
  end
36
36
 
@@ -2,6 +2,7 @@ module Search
2
2
  module InstanceMethods
3
3
  # Adds this record to the search index asynchronously
4
4
  def add_to_search
5
+ self.class.configure_attributes_and_index_if_needed!
5
6
  search_index.add_documents(
6
7
  [search_indexable_hash],
7
8
  primary_search_key.to_s
@@ -10,6 +11,7 @@ module Search
10
11
 
11
12
  # Adds this record to the search index synchronously
12
13
  def add_to_search!
14
+ self.class.configure_attributes_and_index_if_needed!
13
15
  index = search_index
14
16
  documents = [search_indexable_hash]
15
17
  pk = primary_search_key.to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb_meilisearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - masukomi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-16 00:00:00.000000000 Z
11
+ date: 2023-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -187,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
187
  - !ruby/object:Gem::Version
188
188
  version: '0'
189
189
  requirements: []
190
- rubygems_version: 3.4.17
190
+ rubygems_version: 3.4.20
191
191
  signing_key:
192
192
  specification_version: 4
193
193
  summary: MeiliSearch integration for MongoDB