mongodb_meilisearch 1.0.1 → 1.1.0

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: 1d42d4314105bccabf3b1cc8dc49c1e7c578cc854d213f8fb73cde81aba26c03
4
- data.tar.gz: 0dc187a25352ae68526a389d8e0deed4ba9772ff94a3a6fb2bf001e67f8ccbcb
3
+ metadata.gz: f57c18174244b2885536a990a2d602ce833e949dcf205112c78f30567a6eee6a
4
+ data.tar.gz: bb5bdd6c8cb90a6e1334c4606063a56b71515510f0f45c3e1ae2d817b4497163
5
5
  SHA512:
6
- metadata.gz: 13f4ab53d0794010d49bcbdf325db757ceae92cbcce77c77c7dccc99fc4436312b9d786a79fe62609363a8f83372c025374f73cc967261cda231f9624b0e0672
7
- data.tar.gz: ef36cb85710a0bcbe4c47747686aa19d0a7d6748000e4f8ed73e1682a01ec7c4166cffc7e0984faeb7d3b25f9c64288943a828888bcb161d24d2d2268ddf9974
6
+ metadata.gz: cb3e4f69dad6365682470020e3acc864492c45d79de54fb65fbab6fb9eaacb0811d8af02414f4df324a9d0cf64278646e0eac3062e7aff1a9873aa2f264d0d64
7
+ data.tar.gz: 54aa4eea1cfe0b378e9ad93c719eb31a5c35c96c08ef842d975efb8391cf1b55b0ca53e9ca3d8daf98626a02c30ea817e9623c78de0b6ce14ade726473767304
data/.rubocop.yml CHANGED
@@ -1116,12 +1116,6 @@ Style/ComparableClamp:
1116
1116
  Style/ConcatArrayLiterals:
1117
1117
  Enabled: false
1118
1118
 
1119
- Style/ConditionalAssignment:
1120
- Enabled: true
1121
- EnforcedStyle: assign_to_condition
1122
- SingleLineConditionsOnly: true
1123
- IncludeTernaryExpressions: true
1124
-
1125
1119
  Style/ConstantVisibility:
1126
1120
  Enabled: false
1127
1121
 
@@ -1857,3 +1851,11 @@ Style/YodaExpression:
1857
1851
  Style/ZeroLengthPredicate:
1858
1852
  Enabled: false
1859
1853
 
1854
+ # this is horrible for readability
1855
+ # and maintainability
1856
+ Style/ConditionalAssignment:
1857
+ Enabled: false
1858
+
1859
+ # I'll nest how I wanna nest! :p
1860
+ RSpec/NestedGroups:
1861
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongodb_meilisearch (1.0.1)
4
+ mongodb_meilisearch (1.1.0)
5
5
  meilisearch
6
6
  mongoid (~> 7.0)
7
7
  rails
data/README.md CHANGED
@@ -226,7 +226,8 @@ Then call the following. If `FILTERABLE_ATTRIBUTE_NAMES` is defined it will use
226
226
  otherwise it will use whatever `.searchable_attributes` returns.
227
227
 
228
228
  ```ruby
229
- MyModel.set_filterable_attributes!
229
+ MyModel.set_filterable_attributes! # synchronous
230
+ MyModel.set_filterable_attributes # asynchronous
230
231
  ```
231
232
 
232
233
  This will cause Meilisearch to reindex all the records for that index. If you
@@ -235,14 +236,31 @@ on a background thread. Note that filtering is managed at the index level, not t
235
236
  record level. By setting filterable attributes you're giving Meilisearch
236
237
  guidance on what to do when indexing your data.
237
238
 
239
+ Note that you will encounter problems in a shared index if you try and
240
+ filter on a field that one of the contributing models doesn't have set
241
+ as a filterable field, or doesn't have at all.
238
242
 
243
+ ### Sortable Fields
244
+
245
+ Sortable fields work in essentially the same way as filterable fields.
246
+ By default it's the same as your `FILTERABLE_ATTRIBUTE_NAMES` which, in turn, defaults to your `SEARCHABLE_ATTRIBUTES` You can
247
+ override it by setting `SORTABLE_ATTRIBUTE_NAMES`.
248
+
249
+ Note that you will encounter problems in a shared index if you try and
250
+ sort on a field that one of the contributing models doesn't have set
251
+ as a sortable field, or doesn't have at all.
252
+
253
+ ```ruby
254
+ MyModel.set_sortable_attributes! # synchronous
255
+ MyModel.set_sortable_attributes # asynchronous
256
+ ```
239
257
 
240
258
  ### Indexing things
241
259
  **Important note**: By default anything you do that updates the search index (adding, removing, or changing) happens asynchronously.
242
260
 
243
261
  Sometimes, especially when debugging something on the console, you want to
244
- update the index _synchronously_. The convention used in this codebase is that
245
- the synchronous methods are the ones with the bang. Similar to how mutating
262
+ update the index _synchronously_. The convention used in this codebase - and in the meilisearch-ruby library we build on - is that
263
+ the synchronous methods are the ones with the bang. Similar to how mutating
246
264
  state is potentially dangerous and noted with a bang, using synchronous methods
247
265
  is potentially problematic for your users, and thus noted with a bang.
248
266
 
@@ -264,7 +282,9 @@ MyModel.reindex! # runs synchronously
264
282
  **Reindexing**
265
283
  Calling `MyModel.reindex!` deletes all the existing records from the current index,
266
284
  and then reindexes all the records for the current model. It's safe to run this
267
- even if there aren't any records.
285
+ even if there aren't any records. In addition to re-indexing your models,
286
+ it will update/set the "sortable" and "filterable" fields on the
287
+ relevant indexes.
268
288
 
269
289
  Note: reindexing behaves slightly differently than all the other methods.
270
290
  It runs semi-asynchronously by default. The Asynchronous form will first,
@@ -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.0.1"
8
+ VERSION = "1.1.0"
9
9
  end
@@ -378,28 +378,92 @@ module Search
378
378
  # @return [Array[Symbol]] an array of symbols corresponding to
379
379
  # filterable attribute names.
380
380
  def filterable_attributes
381
- attributes = []
382
- if constants.include?(:FILTERABLE_ATTRIBUTE_NAMES)
383
- # the union operator is to guarantee no-one tries to create
384
- # invalid filterable attributes
385
- attributes = const_get(:FILTERABLE_ATTRIBUTE_NAMES).map(&:to_sym) & searchable_attributes
386
- elsif !unfilterable?
387
- attributes = searchable_attributes
388
- end
389
- attributes << "object_class" unless attributes.include? "object_class"
390
- attributes
381
+ @_filterable_attributes ||= sort_or_filter_attributes(:filterable)
382
+ end
383
+
384
+ def sortable_attributes
385
+ @_sortable_attributes ||= sort_or_filter_attributes(:sortable)
386
+ end
387
+
388
+ # For more details on sortable attributes see the official
389
+ # Meilisearch docs
390
+ # https://www.meilisearch.com/docs/reference/api/settings#sortable-attributes
391
+ #
392
+ #
393
+ # @return [Array] - an array of attributes configured as sortable
394
+ # in the index.
395
+ def meilisearch_sortable_attributes
396
+ # Search::Client.instance.http_get(
397
+ search_index.http_get(
398
+ "/indexes/#{search_index}/settings/sortable-attributes"
399
+ )
400
+ end
401
+
402
+ def meilisearch_filterable_attributes
403
+ # Search::Client.instance.http_get(
404
+ search_index.http_get(
405
+ "/indexes/#{search_index}/settings/filterable-attributes"
406
+ )
391
407
  end
392
408
 
393
409
  # Updates the filterable attributes in the search index.
394
410
  # Note that this forces Meilisearch to rebuild your index,
395
411
  # which may take time. Best to run this in a background job
396
412
  # for large datasets.
397
- def set_filterable_attributes!(new_attributes = filterable_attributes)
413
+ def set_filterable_attributes(new_attributes = filterable_attributes)
398
414
  search_index.update_filterable_attributes(new_attributes)
399
415
  end
400
416
 
417
+ def set_filterable_attributes!(new_attributes = filterable_attributes)
418
+ # meilisearch-ruby doesn't provide a synchronous version of this
419
+ task = set_filterable_attributes(new_attributes)
420
+ search_index.wait_for_task(task["taskUid"])
421
+ end
422
+
423
+ # Updates the sortable attributes in the search index.
424
+ # Note that this forces Meilisearch to rebuild your index,
425
+ # which may take time. Best to run this in a background job
426
+ # for large datasets.
427
+ def set_sortable_attributes(new_attributes = sortable_attributes)
428
+ search_index.update_sortable_attributes(new_attributes)
429
+ end
430
+
431
+ def set_sortable_attributes!(new_attributes = sortable_attributes)
432
+ # meilisearch-ruby doesn't provide a synchronous version of this
433
+ task = set_sortable_attributes(new_attributes)
434
+ search_index.wait_for_task(task["taskUid"])
435
+ end
436
+
401
437
  private
402
438
 
439
+ # @param [Symbol] which - either :sortable or :filterable
440
+ def sort_or_filter_attributes(which)
441
+ constant_symbol = (which == :sortable) ? :SORTABLE_ATTRIBUTE_NAMES : :FILTERABLE_ATTRIBUTE_NAMES
442
+
443
+ if which == :filterable && unfilterable? && constants.include?(constant_symbol)
444
+ raise "You can't define FILTERABLE_ATTRIBUTE_NAMES & UNFILTERABLE_IN_SEARCH on #{self.class.name}"
445
+ end
446
+ # with that out of the way...
447
+
448
+ attributes = []
449
+ # sortable == defined or filterable
450
+ # filterable == defined or search
451
+ if constants.include?(constant_symbol)
452
+ # the union operator is to guarantee no-one tries to create
453
+ # invalid filterable attributes
454
+ attributes = const_get(constant_symbol).map(&:to_sym) & searchable_attributes
455
+ elsif which == :filterable && !unfilterable?
456
+ attributes = searchable_attributes
457
+ elsif which == :sortable
458
+ # yes this is recursive...
459
+ attributes = filterable_attributes
460
+ end
461
+ # we need object_class even if you're unfilterable, because
462
+ # we need to be able to filter by object_class in shared indexes.
463
+ attributes << :object_class unless attributes.include? :object_class
464
+ attributes
465
+ end
466
+
403
467
  def lookup_matches_by_class!(results, primary_key)
404
468
  # matches_by_class contains a hash of
405
469
  # "FooModel" => [<array of ids>]
@@ -528,7 +592,13 @@ module Search
528
592
  end
529
593
  add_documents(documents, async: async) if documents.size != 0
530
594
 
531
- set_filterable_attributes!
595
+ if async
596
+ set_filterable_attributes
597
+ set_sortable_attributes
598
+ else
599
+ set_filterable_attributes!
600
+ set_sortable_attributes!
601
+ end
532
602
  end
533
603
  end
534
604
  end
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.0.1
4
+ version: 1.1.0
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-02 00:00:00.000000000 Z
11
+ date: 2023-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails