Class: Meilisearch::Index

Inherits:
HTTPRequest show all
Defined in:
lib/meilisearch/index.rb

Constant Summary

Constants inherited from HTTPRequest

HTTPRequest::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from HTTPRequest

#headers, #options

Instance Method Summary collapse

Methods inherited from HTTPRequest

#http_delete, #http_get, #http_patch, #http_post, #http_put

Constructor Details

#initialize(index_uid, url, api_key = nil, primary_key = nil, options = {}) ⇒ Index

Returns a new instance of Index.



9
10
11
12
13
# File 'lib/meilisearch/index.rb', line 9

def initialize(index_uid, url, api_key = nil, primary_key = nil, options = {})
  @uid = index_uid
  @primary_key = primary_key
  super(url, api_key, options)
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



7
8
9
# File 'lib/meilisearch/index.rb', line 7

def created_at
  @created_at
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



7
8
9
# File 'lib/meilisearch/index.rb', line 7

def primary_key
  @primary_key
end

#uidObject (readonly)

Returns the value of attribute uid.



7
8
9
# File 'lib/meilisearch/index.rb', line 7

def uid
  @uid
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



7
8
9
# File 'lib/meilisearch/index.rb', line 7

def updated_at
  @updated_at
end

Instance Method Details

#add_documents(documents, primary_key = nil) ⇒ Object Also known as: replace_documents, add_or_replace_documents



89
90
91
92
93
94
# File 'lib/meilisearch/index.rb', line 89

def add_documents(documents, primary_key = nil)
  documents = [documents] if documents.is_a?(Hash)
  response = http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact

  Models::Task.new(response, task_endpoint)
end

#add_documents!(documents, primary_key = nil) ⇒ Object Also known as: replace_documents!, add_or_replace_documents!



98
99
100
101
102
103
104
105
# File 'lib/meilisearch/index.rb', line 98

def add_documents!(documents, primary_key = nil)
  Utils.soft_deprecate(
    'Index#add_documents!',
    'index.add_documents(...).await'
  )

  add_documents(documents, primary_key).await
end

#add_documents_csv(documents, primary_key = nil, delimiter = nil) ⇒ Object Also known as: replace_documents_csv, add_or_replace_documents_csv, add_or_update_documents_csv



127
128
129
130
131
132
133
134
135
136
# File 'lib/meilisearch/index.rb', line 127

def add_documents_csv(documents, primary_key = nil, delimiter = nil)
  options = { headers: { 'Content-Type' => 'text/csv' }, convert_body?: false }

  response = http_post "/indexes/#{@uid}/documents", documents, {
    primaryKey: primary_key,
    csvDelimiter: delimiter
  }.compact, options

  Models::Task.new(response, task_endpoint)
end

#add_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/meilisearch/index.rb', line 212

def add_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil)
  lines = documents.lines
  heading = lines.first
  lines.drop(1).each_slice(batch_size).map do |batch|
    add_documents_csv(heading + batch.join, primary_key, delimiter)
  end
end

#add_documents_in_batches(documents, batch_size = 1000, primary_key = nil) ⇒ Object



200
201
202
203
204
# File 'lib/meilisearch/index.rb', line 200

def add_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
  documents.each_slice(batch_size).map do |batch|
    add_documents(batch, primary_key)
  end
end

#add_documents_in_batches!(documents, batch_size = 1000, primary_key = nil) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/meilisearch/index.rb', line 220

def add_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
  Utils.soft_deprecate(
    'Index#add_documents_in_batches!',
    'index.add_documents_in_batches(...).each(&:await)'
  )

  add_documents_in_batches(documents, batch_size, primary_key).each(&:await)
end

#add_documents_json(documents, primary_key = nil) ⇒ Object Also known as: replace_documents_json, add_or_replace_documents_json



109
110
111
112
113
114
# File 'lib/meilisearch/index.rb', line 109

def add_documents_json(documents, primary_key = nil)
  options = { convert_body?: false }
  response = http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options

  Models::Task.new(response, task_endpoint)
end

#add_documents_ndjson(documents, primary_key = nil) ⇒ Object Also known as: replace_documents_ndjson, add_or_replace_documents_ndjson



118
119
120
121
122
123
# File 'lib/meilisearch/index.rb', line 118

def add_documents_ndjson(documents, primary_key = nil)
  options = { headers: { 'Content-Type' => 'application/x-ndjson' }, convert_body?: false }
  response = http_post "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options

  Models::Task.new(response, task_endpoint)
end

#add_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil) ⇒ Object



206
207
208
209
210
# File 'lib/meilisearch/index.rb', line 206

def add_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil)
  documents.lines.each_slice(batch_size).map do |batch|
    add_documents_ndjson(batch.join, primary_key)
  end
end

#deleteObject Also known as: delete_index



39
40
41
42
# File 'lib/meilisearch/index.rb', line 39

def delete
  response = http_delete indexes_path(id: @uid)
  Models::Task.new(response, task_endpoint)
end

#delete_all_documentsObject



312
313
314
315
# File 'lib/meilisearch/index.rb', line 312

def delete_all_documents
  response = http_delete "/indexes/#{@uid}/documents"
  Models::Task.new(response, task_endpoint)
end

#delete_all_documents!Object



317
318
319
320
321
322
323
324
# File 'lib/meilisearch/index.rb', line 317

def delete_all_documents!
  Utils.soft_deprecate(
    'Index#delete_all_documents!',
    'index.delete_all_documents(...).await'
  )

  delete_all_documents.await
end

#delete_document(document_id) ⇒ Object Also known as: delete_one_document



290
291
292
293
294
295
296
297
298
299
# File 'lib/meilisearch/index.rb', line 290

def delete_document(document_id)
  if document_id.nil? || document_id.to_s.empty?
    raise Meilisearch::InvalidDocumentId, 'document_id cannot be empty or nil'
  end

  encode_document = URI.encode_www_form_component(document_id)
  response = http_delete "/indexes/#{@uid}/documents/#{encode_document}"

  Models::Task.new(response, task_endpoint)
end

#delete_document!(document_id) ⇒ Object Also known as: delete_one_document!



302
303
304
305
306
307
308
309
# File 'lib/meilisearch/index.rb', line 302

def delete_document!(document_id)
  Utils.soft_deprecate(
    'Index#delete_document!',
    'index.delete_document(...).await'
  )

  delete_document(document_id).await
end

#delete_documents(options = {}) ⇒ Object Also known as: delete_multiple_documents

Public: Delete documents from an index

options: A Hash or an Array containing documents_ids or a hash with filter:.

filter: - A hash containing a filter that should match documents.
          Available ONLY with Meilisearch v1.2 and newer (optional)

Returns a Task object.



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/meilisearch/index.rb', line 263

def delete_documents(options = {})
  Utils.version_error_handler(__method__) do
    response = if options.is_a?(Hash) && options.key?(:filter)
                 http_post "/indexes/#{@uid}/documents/delete", options
               else
                 # backwards compatibility:
                 # expect to be a array or/number/string to send alongside as documents_ids.
                 options = [options] unless options.is_a?(Array)

                 http_post "/indexes/#{@uid}/documents/delete-batch", options
               end

    Models::Task.new(response, task_endpoint)
  end
end

#delete_documents!(documents_ids) ⇒ Object Also known as: delete_multiple_documents!



280
281
282
283
284
285
286
287
# File 'lib/meilisearch/index.rb', line 280

def delete_documents!(documents_ids)
  Utils.soft_deprecate(
    'Index#delete_documents!',
    'index.delete_documents(...).await'
  )

  delete_documents(documents_ids).await
end

#dictionaryObject

SETTINGS - DICTIONARY



613
614
615
# File 'lib/meilisearch/index.rb', line 613

def dictionary
  http_get("/indexes/#{@uid}/settings/dictionary")
end

#displayed_attributesObject Also known as: get_displayed_attributes

SETTINGS - DISPLAYED ATTRIBUTES



507
508
509
# File 'lib/meilisearch/index.rb', line 507

def displayed_attributes
  http_get "/indexes/#{@uid}/settings/displayed-attributes"
end

#distinct_attributeObject Also known as: get_distinct_attribute

SETTINGS - DISTINCT ATTRIBUTE



471
472
473
# File 'lib/meilisearch/index.rb', line 471

def distinct_attribute
  http_get "/indexes/#{@uid}/settings/distinct-attribute"
end

#document(document_id, fields: nil) ⇒ Object Also known as: get_document, get_one_document

DOCUMENTS



59
60
61
62
63
64
# File 'lib/meilisearch/index.rb', line 59

def document(document_id, fields: nil)
  encode_document = URI.encode_www_form_component(document_id)
  body = { fields: fields&.join(',') }.compact

  http_get("/indexes/#{@uid}/documents/#{encode_document}", body)
end

#documents(options = {}) ⇒ Object Also known as: get_documents

Public: Retrieve documents from a index.

options - The hash options used to refine the selection (default: {}):

:limit  - Number of documents to return (optional).
:offset - Number of documents to skip (optional).
:fields - Array of document attributes to show (optional).
:filter - Filter queries by an attribute's value.
          Available ONLY with Meilisearch v1.2 and newer (optional).

Returns the documents results object.



78
79
80
81
82
83
84
85
86
# File 'lib/meilisearch/index.rb', line 78

def documents(options = {})
  Utils.version_error_handler(__method__) do
    if options.key?(:filter)
      http_post "/indexes/#{@uid}/documents/fetch", Utils.filter(options, [:limit, :offset, :fields, :filter])
    else
      http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
    end
  end
end

#embeddersObject

SETTINGS - EMBEDDERS



755
756
757
# File 'lib/meilisearch/index.rb', line 755

def embedders
  http_get("/indexes/#{@uid}/settings/embedders")
end

#facet_search(name, query = '', **options) ⇒ Object

FACET SEARCH



352
353
354
355
356
357
# File 'lib/meilisearch/index.rb', line 352

def facet_search(name, query = '', **options)
  options.merge!(facet_name: name, facet_query: query)
  options = Utils.transform_attributes(options)

  http_post("/indexes/#{@uid}/facet-search", options)
end

#facet_search_settingObject

SETTINGS - FACET SEARCH



719
720
721
# File 'lib/meilisearch/index.rb', line 719

def facet_search_setting
  http_get("/indexes/#{@uid}/settings/facet-search")
end

#facetingObject Also known as: get_faceting



594
595
596
# File 'lib/meilisearch/index.rb', line 594

def faceting
  http_get("/indexes/#{@uid}/settings/faceting")
end

#fetch_infoObject



15
16
17
18
19
# File 'lib/meilisearch/index.rb', line 15

def fetch_info
  index_hash = http_get indexes_path(id: @uid)
  set_base_properties index_hash
  self
end

#fetch_primary_keyObject Also known as: get_primary_key



21
22
23
# File 'lib/meilisearch/index.rb', line 21

def fetch_primary_key
  fetch_info.primary_key
end

#fetch_raw_infoObject



26
27
28
29
30
# File 'lib/meilisearch/index.rb', line 26

def fetch_raw_info
  index_hash = http_get indexes_path(id: @uid)
  set_base_properties index_hash
  index_hash
end

#field_distributionObject



392
393
394
# File 'lib/meilisearch/index.rb', line 392

def field_distribution
  stats['fieldDistribution']
end

#filterable_attributesObject Also known as: get_filterable_attributes

SETTINGS - FILTERABLE ATTRIBUTES



525
526
527
# File 'lib/meilisearch/index.rb', line 525

def filterable_attributes
  http_get "/indexes/#{@uid}/settings/filterable-attributes"
end

#indexing?Boolean

Returns:

  • (Boolean)


388
389
390
# File 'lib/meilisearch/index.rb', line 388

def indexing?
  stats['isIndexing']
end

#localized_attributesObject

SETTINGS - LOCALIZED ATTRIBUTES



699
700
701
# File 'lib/meilisearch/index.rb', line 699

def localized_attributes
  http_get("/indexes/#{@uid}/settings/localized-attributes")
end

#non_separator_tokensObject

SETTINGS - NON SEPARATOR TOKENS



646
647
648
# File 'lib/meilisearch/index.rb', line 646

def non_separator_tokens
  http_get("/indexes/#{@uid}/settings/non-separator-tokens")
end

#number_of_documentsObject



384
385
386
# File 'lib/meilisearch/index.rb', line 384

def number_of_documents
  stats['numberOfDocuments']
end

#paginationObject Also known as: get_pagination

SETTINGS - PAGINATION



561
562
563
# File 'lib/meilisearch/index.rb', line 561

def pagination
  http_get("/indexes/#{@uid}/settings/pagination")
end

#prefix_searchObject

SETTINGS - PREFIX SEARCH



737
738
739
# File 'lib/meilisearch/index.rb', line 737

def prefix_search
  http_get("/indexes/#{@uid}/settings/prefix-search")
end

#proximity_precisionObject

SETTINGS - PROXIMITY PRECISION



663
664
665
# File 'lib/meilisearch/index.rb', line 663

def proximity_precision
  http_get("/indexes/#{@uid}/settings/proximity-precision")
end

#ranking_rulesObject Also known as: get_ranking_rules

SETTINGS - RANKING RULES



416
417
418
# File 'lib/meilisearch/index.rb', line 416

def ranking_rules
  http_get "/indexes/#{@uid}/settings/ranking-rules"
end

#reset_dictionaryObject



623
624
625
626
# File 'lib/meilisearch/index.rb', line 623

def reset_dictionary
  response = http_delete("/indexes/#{@uid}/settings/dictionary")
  Models::Task.new(response, task_endpoint)
end

#reset_displayed_attributesObject



518
519
520
521
# File 'lib/meilisearch/index.rb', line 518

def reset_displayed_attributes
  response = http_delete "/indexes/#{@uid}/settings/displayed-attributes"
  Models::Task.new(response, task_endpoint)
end

#reset_distinct_attributeObject



482
483
484
485
# File 'lib/meilisearch/index.rb', line 482

def reset_distinct_attribute
  response = http_delete "/indexes/#{@uid}/settings/distinct-attribute"
  Models::Task.new(response, task_endpoint)
end

#reset_embeddersObject



767
768
769
770
771
# File 'lib/meilisearch/index.rb', line 767

def reset_embedders
  response = http_delete("/indexes/#{@uid}/settings/embedders")

  Models::Task.new(response, task_endpoint)
end

#reset_facet_search_settingObject



729
730
731
732
733
# File 'lib/meilisearch/index.rb', line 729

def reset_facet_search_setting
  response = http_delete("/indexes/#{@uid}/settings/facet-search")

  Models::Task.new(response, task_endpoint)
end

#reset_facetingObject



606
607
608
609
# File 'lib/meilisearch/index.rb', line 606

def reset_faceting
  response = http_delete("/indexes/#{@uid}/settings/faceting")
  Models::Task.new(response, task_endpoint)
end

#reset_filterable_attributesObject



536
537
538
539
# File 'lib/meilisearch/index.rb', line 536

def reset_filterable_attributes
  response = http_delete "/indexes/#{@uid}/settings/filterable-attributes"
  Models::Task.new(response, task_endpoint)
end

#reset_localized_attributesObject



711
712
713
714
715
# File 'lib/meilisearch/index.rb', line 711

def reset_localized_attributes
  response = http_delete("/indexes/#{@uid}/settings/localized-attributes")

  Models::Task.new(response, task_endpoint)
end

#reset_non_separator_tokensObject



656
657
658
659
# File 'lib/meilisearch/index.rb', line 656

def reset_non_separator_tokens
  response = http_delete("/indexes/#{@uid}/settings/non-separator-tokens")
  Models::Task.new(response, task_endpoint)
end

#reset_paginationObject



572
573
574
575
# File 'lib/meilisearch/index.rb', line 572

def reset_pagination
  response = http_delete "/indexes/#{@uid}/settings/pagination"
  Models::Task.new(response, task_endpoint)
end

#reset_prefix_searchObject



747
748
749
750
751
# File 'lib/meilisearch/index.rb', line 747

def reset_prefix_search
  response = http_delete("/indexes/#{@uid}/settings/prefix-search")

  Models::Task.new(response, task_endpoint)
end

#reset_proximity_precisionObject



673
674
675
676
677
# File 'lib/meilisearch/index.rb', line 673

def reset_proximity_precision
  response = http_delete("/indexes/#{@uid}/settings/proximity-precision")

  Models::Task.new(response, task_endpoint)
end

#reset_ranking_rulesObject



427
428
429
430
# File 'lib/meilisearch/index.rb', line 427

def reset_ranking_rules
  response = http_delete "/indexes/#{@uid}/settings/ranking-rules"
  Models::Task.new(response, task_endpoint)
end

#reset_search_cutoff_msObject



691
692
693
694
695
# File 'lib/meilisearch/index.rb', line 691

def reset_search_cutoff_ms
  response = http_delete("/indexes/#{@uid}/settings/search-cutoff-ms")

  Models::Task.new(response, task_endpoint)
end

#reset_searchable_attributesObject



500
501
502
503
# File 'lib/meilisearch/index.rb', line 500

def reset_searchable_attributes
  response = http_delete "/indexes/#{@uid}/settings/searchable-attributes"
  Models::Task.new(response, task_endpoint)
end

#reset_separator_tokensObject



639
640
641
642
# File 'lib/meilisearch/index.rb', line 639

def reset_separator_tokens
  response = http_delete("/indexes/#{@uid}/settings/separator-tokens")
  Models::Task.new(response, task_endpoint)
end

#reset_settingsObject



409
410
411
412
# File 'lib/meilisearch/index.rb', line 409

def reset_settings
  response = http_delete "/indexes/#{@uid}/settings"
  Models::Task.new(response, task_endpoint)
end

#reset_sortable_attributesObject



554
555
556
557
# File 'lib/meilisearch/index.rb', line 554

def reset_sortable_attributes
  response = http_delete "/indexes/#{@uid}/settings/sortable-attributes"
  Models::Task.new(response, task_endpoint)
end

#reset_stop_wordsObject



464
465
466
467
# File 'lib/meilisearch/index.rb', line 464

def reset_stop_words
  response = http_delete "/indexes/#{@uid}/settings/stop-words"
  Models::Task.new(response, task_endpoint)
end

#reset_synonymsObject



445
446
447
448
# File 'lib/meilisearch/index.rb', line 445

def reset_synonyms
  response = http_delete "/indexes/#{@uid}/settings/synonyms"
  Models::Task.new(response, task_endpoint)
end

#reset_typo_toleranceObject



589
590
591
592
# File 'lib/meilisearch/index.rb', line 589

def reset_typo_tolerance
  response = http_delete("/indexes/#{@uid}/settings/typo-tolerance")
  Models::Task.new(response, task_endpoint)
end

#search(query, options = {}) ⇒ Object

options: A Hash

show_ranking_score - To see the ranking scores for returned documents
attributes_to_search_on - Customize attributes to search on at search time.


331
332
333
334
335
336
337
338
339
340
# File 'lib/meilisearch/index.rb', line 331

def search(query, options = {})
  attributes = { q: query.to_s }.merge(options.compact)

  parsed_options = Utils.transform_attributes(attributes)
  response = http_post "/indexes/#{@uid}/search", parsed_options

  response['nbHits'] ||= response['estimatedTotalHits'] unless response.key?('totalPages')

  response
end

#search_cutoff_msObject

SETTINGS - SEARCH CUTOFF MS



681
682
683
# File 'lib/meilisearch/index.rb', line 681

def search_cutoff_ms
  http_get("/indexes/#{@uid}/settings/search-cutoff-ms")
end

#search_similar_documents(document_id, **options) ⇒ Object

document_id: Identifier of the target document



343
344
345
346
347
348
# File 'lib/meilisearch/index.rb', line 343

def search_similar_documents(document_id, **options)
  options.merge!(id: document_id)
  options = Utils.transform_attributes(options)

  http_post("/indexes/#{@uid}/similar", options)
end

#searchable_attributesObject Also known as: get_searchable_attributes

SETTINGS - SEARCHABLE ATTRIBUTES



489
490
491
# File 'lib/meilisearch/index.rb', line 489

def searchable_attributes
  http_get "/indexes/#{@uid}/settings/searchable-attributes"
end

#separator_tokensObject

SETTINGS - SEPARATOR TOKENS



629
630
631
# File 'lib/meilisearch/index.rb', line 629

def separator_tokens
  http_get("/indexes/#{@uid}/settings/separator-tokens")
end

#settingsObject Also known as: get_settings

SETTINGS - GENERAL



398
399
400
# File 'lib/meilisearch/index.rb', line 398

def settings
  http_get "/indexes/#{@uid}/settings"
end

#sortable_attributesObject Also known as: get_sortable_attributes

SETTINGS - SORTABLE ATTRIBUTES



543
544
545
# File 'lib/meilisearch/index.rb', line 543

def sortable_attributes
  http_get "/indexes/#{@uid}/settings/sortable-attributes"
end

#statsObject

STATS



380
381
382
# File 'lib/meilisearch/index.rb', line 380

def stats
  http_get "/indexes/#{@uid}/stats"
end

#stop_wordsObject Also known as: get_stop_words

SETTINGS - STOP-WORDS



452
453
454
# File 'lib/meilisearch/index.rb', line 452

def stop_words
  http_get "/indexes/#{@uid}/settings/stop-words"
end

#synonymsObject Also known as: get_synonyms

SETTINGS - SYNONYMS



434
435
436
# File 'lib/meilisearch/index.rb', line 434

def synonyms
  http_get "/indexes/#{@uid}/settings/synonyms"
end

#task(task_uid) ⇒ Object



366
367
368
# File 'lib/meilisearch/index.rb', line 366

def task(task_uid)
  task_endpoint.index_task(task_uid)
end

#tasksObject



370
371
372
# File 'lib/meilisearch/index.rb', line 370

def tasks
  task_endpoint.index_tasks(@uid)
end

#typo_toleranceObject Also known as: get_typo_tolerance



577
578
579
# File 'lib/meilisearch/index.rb', line 577

def typo_tolerance
  http_get("/indexes/#{@uid}/settings/typo-tolerance")
end

#update(body) ⇒ Object Also known as: update_index



32
33
34
35
# File 'lib/meilisearch/index.rb', line 32

def update(body)
  response = http_patch indexes_path(id: @uid), Utils.transform_attributes(body)
  Models::Task.new(response, task_endpoint)
end

#update_dictionary(dictionary_attributes) ⇒ Object



617
618
619
620
621
# File 'lib/meilisearch/index.rb', line 617

def update_dictionary(dictionary_attributes)
  attributes = Utils.transform_attributes(dictionary_attributes)
  response = http_put("/indexes/#{@uid}/settings/dictionary", attributes)
  Models::Task.new(response, task_endpoint)
end

#update_displayed_attributes(displayed_attributes) ⇒ Object Also known as: displayed_attributes=



512
513
514
515
# File 'lib/meilisearch/index.rb', line 512

def update_displayed_attributes(displayed_attributes)
  response = http_put "/indexes/#{@uid}/settings/displayed-attributes", displayed_attributes
  Models::Task.new(response, task_endpoint)
end

#update_distinct_attribute(distinct_attribute) ⇒ Object Also known as: distinct_attribute=



476
477
478
479
# File 'lib/meilisearch/index.rb', line 476

def update_distinct_attribute(distinct_attribute)
  response = http_put "/indexes/#{@uid}/settings/distinct-attribute", distinct_attribute
  Models::Task.new(response, task_endpoint)
end

#update_documents(documents, primary_key = nil) ⇒ Object Also known as: add_or_update_documents



140
141
142
143
144
145
# File 'lib/meilisearch/index.rb', line 140

def update_documents(documents, primary_key = nil)
  documents = [documents] if documents.is_a?(Hash)
  response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact

  Models::Task.new(response, task_endpoint)
end

#update_documents!(documents, primary_key = nil) ⇒ Object Also known as: add_or_update_documents!



190
191
192
193
194
195
196
197
# File 'lib/meilisearch/index.rb', line 190

def update_documents!(documents, primary_key = nil)
  Utils.soft_deprecate(
    'Index#update_documents!',
    'index.update_documents(...).await'
  )

  update_documents(documents, primary_key).await
end

#update_documents_by_function(options) ⇒ Object

Update documents by function

options - A Hash containing the function string and related options

context:
filter:
function:


250
251
252
253
254
# File 'lib/meilisearch/index.rb', line 250

def update_documents_by_function(options)
  response = http_post "/indexes/#{@uid}/documents/edit", options

  Models::Task.new(response, task_endpoint)
end

#update_documents_csv(documents, primary_key = nil, delimiter = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/meilisearch/index.rb', line 164

def update_documents_csv(documents, primary_key = nil, delimiter = nil)
  options = { headers: { 'Content-Type' => 'text/csv' }, convert_body?: false }

  response = http_put "/indexes/#{@uid}/documents", documents, {
    primaryKey: primary_key,
    csvDelimiter: delimiter
  }.compact, options

  Models::Task.new(response, task_endpoint)
end

#update_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/meilisearch/index.rb', line 182

def update_documents_csv_in_batches(documents, batch_size = 1000, primary_key = nil, delimiter = nil)
  lines = documents.lines
  heading = lines.first
  lines.drop(1).each_slice(batch_size).map do |batch|
    update_documents_csv(heading + batch.join, primary_key, delimiter)
  end
end

#update_documents_in_batches(documents, batch_size = 1000, primary_key = nil) ⇒ Object



229
230
231
232
233
# File 'lib/meilisearch/index.rb', line 229

def update_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
  documents.each_slice(batch_size).map do |batch|
    update_documents(batch, primary_key)
  end
end

#update_documents_in_batches!(documents, batch_size = 1000, primary_key = nil) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/meilisearch/index.rb', line 235

def update_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
  Utils.soft_deprecate(
    'Index#update_documents_in_batches!',
    'index.update_documents_in_batches(...).each(&:await)'
  )

  update_documents_in_batches(documents, batch_size, primary_key).each(&:await)
end

#update_documents_json(documents, primary_key = nil) ⇒ Object Also known as: add_or_update_documents_json



148
149
150
151
152
153
# File 'lib/meilisearch/index.rb', line 148

def update_documents_json(documents, primary_key = nil)
  options = { convert_body?: false }
  response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options

  Models::Task.new(response, task_endpoint)
end

#update_documents_ndjson(documents, primary_key = nil) ⇒ Object Also known as: add_or_update_documents_ndjson



156
157
158
159
160
161
# File 'lib/meilisearch/index.rb', line 156

def update_documents_ndjson(documents, primary_key = nil)
  options = { headers: { 'Content-Type' => 'application/x-ndjson' }, convert_body?: false }
  response = http_put "/indexes/#{@uid}/documents", documents, { primaryKey: primary_key }.compact, options

  Models::Task.new(response, task_endpoint)
end

#update_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil) ⇒ Object



176
177
178
179
180
# File 'lib/meilisearch/index.rb', line 176

def update_documents_ndjson_in_batches(documents, batch_size = 1000, primary_key = nil)
  documents.lines.each_slice(batch_size).map do |batch|
    update_documents_ndjson(batch.join, primary_key)
  end
end

#update_embedders(new_embedders) ⇒ Object



759
760
761
762
763
764
765
# File 'lib/meilisearch/index.rb', line 759

def update_embedders(new_embedders)
  new_embedders = Utils.transform_attributes(new_embedders)

  response = http_patch("/indexes/#{@uid}/settings/embedders", new_embedders)

  Models::Task.new(response, task_endpoint)
end

#update_facet_search_setting(new_facet_search_setting) ⇒ Object



723
724
725
726
727
# File 'lib/meilisearch/index.rb', line 723

def update_facet_search_setting(new_facet_search_setting)
  response = http_put("/indexes/#{@uid}/settings/facet-search", new_facet_search_setting)

  Models::Task.new(response, task_endpoint)
end

#update_faceting(faceting_attributes) ⇒ Object Also known as: faceting=



599
600
601
602
603
# File 'lib/meilisearch/index.rb', line 599

def update_faceting(faceting_attributes)
  attributes = Utils.transform_attributes(faceting_attributes)
  response = http_patch("/indexes/#{@uid}/settings/faceting", attributes)
  Models::Task.new(response, task_endpoint)
end

#update_filterable_attributes(filterable_attributes) ⇒ Object Also known as: filterable_attributes=



530
531
532
533
# File 'lib/meilisearch/index.rb', line 530

def update_filterable_attributes(filterable_attributes)
  response = http_put "/indexes/#{@uid}/settings/filterable-attributes", filterable_attributes
  Models::Task.new(response, task_endpoint)
end

#update_localized_attributes(new_localized_attributes) ⇒ Object



703
704
705
706
707
708
709
# File 'lib/meilisearch/index.rb', line 703

def update_localized_attributes(new_localized_attributes)
  new_localized_attributes = Utils.transform_attributes(new_localized_attributes)

  response = http_put("/indexes/#{@uid}/settings/localized-attributes", new_localized_attributes)

  Models::Task.new(response, task_endpoint)
end

#update_non_separator_tokens(non_separator_tokens_attributes) ⇒ Object



650
651
652
653
654
# File 'lib/meilisearch/index.rb', line 650

def update_non_separator_tokens(non_separator_tokens_attributes)
  attributes = Utils.transform_attributes(non_separator_tokens_attributes)
  response = http_put("/indexes/#{@uid}/settings/non-separator-tokens", attributes)
  Models::Task.new(response, task_endpoint)
end

#update_pagination(pagination) ⇒ Object



566
567
568
569
# File 'lib/meilisearch/index.rb', line 566

def update_pagination(pagination)
  response = http_patch "/indexes/#{@uid}/settings/pagination", pagination
  Models::Task.new(response, task_endpoint)
end

#update_prefix_search(new_prefix_search_setting) ⇒ Object



741
742
743
744
745
# File 'lib/meilisearch/index.rb', line 741

def update_prefix_search(new_prefix_search_setting)
  response = http_put("/indexes/#{@uid}/settings/prefix-search", new_prefix_search_setting)

  Models::Task.new(response, task_endpoint)
end

#update_proximity_precision(proximity_precision_attribute) ⇒ Object



667
668
669
670
671
# File 'lib/meilisearch/index.rb', line 667

def update_proximity_precision(proximity_precision_attribute)
  response = http_put("/indexes/#{@uid}/settings/proximity-precision", proximity_precision_attribute)

  Models::Task.new(response, task_endpoint)
end

#update_ranking_rules(ranking_rules) ⇒ Object Also known as: ranking_rules=



421
422
423
424
# File 'lib/meilisearch/index.rb', line 421

def update_ranking_rules(ranking_rules)
  response = http_put "/indexes/#{@uid}/settings/ranking-rules", ranking_rules
  Models::Task.new(response, task_endpoint)
end

#update_search_cutoff_ms(search_cutoff_ms_attribute) ⇒ Object



685
686
687
688
689
# File 'lib/meilisearch/index.rb', line 685

def update_search_cutoff_ms(search_cutoff_ms_attribute)
  response = http_put("/indexes/#{@uid}/settings/search-cutoff-ms", search_cutoff_ms_attribute)

  Models::Task.new(response, task_endpoint)
end

#update_searchable_attributes(searchable_attributes) ⇒ Object Also known as: searchable_attributes=



494
495
496
497
# File 'lib/meilisearch/index.rb', line 494

def update_searchable_attributes(searchable_attributes)
  response = http_put "/indexes/#{@uid}/settings/searchable-attributes", searchable_attributes
  Models::Task.new(response, task_endpoint)
end

#update_separator_tokens(separator_tokens_attributes) ⇒ Object



633
634
635
636
637
# File 'lib/meilisearch/index.rb', line 633

def update_separator_tokens(separator_tokens_attributes)
  attributes = Utils.transform_attributes(separator_tokens_attributes)
  response = http_put("/indexes/#{@uid}/settings/separator-tokens", attributes)
  Models::Task.new(response, task_endpoint)
end

#update_settings(settings) ⇒ Object Also known as: settings=



403
404
405
406
# File 'lib/meilisearch/index.rb', line 403

def update_settings(settings)
  response = http_patch "/indexes/#{@uid}/settings", Utils.transform_attributes(settings)
  Models::Task.new(response, task_endpoint)
end

#update_sortable_attributes(sortable_attributes) ⇒ Object Also known as: sortable_attributes=, pagination=



548
549
550
551
# File 'lib/meilisearch/index.rb', line 548

def update_sortable_attributes(sortable_attributes)
  response = http_put "/indexes/#{@uid}/settings/sortable-attributes", sortable_attributes
  Models::Task.new(response, task_endpoint)
end

#update_stop_words(stop_words) ⇒ Object Also known as: stop_words=



457
458
459
460
461
# File 'lib/meilisearch/index.rb', line 457

def update_stop_words(stop_words)
  body = stop_words.nil? || stop_words.is_a?(Array) ? stop_words : [stop_words]
  response = http_put "/indexes/#{@uid}/settings/stop-words", body
  Models::Task.new(response, task_endpoint)
end

#update_synonyms(synonyms) ⇒ Object Also known as: synonyms=



439
440
441
442
# File 'lib/meilisearch/index.rb', line 439

def update_synonyms(synonyms)
  response = http_put "/indexes/#{@uid}/settings/synonyms", synonyms
  Models::Task.new(response, task_endpoint)
end

#update_typo_tolerance(typo_tolerance_attributes) ⇒ Object Also known as: typo_tolerance=



582
583
584
585
586
# File 'lib/meilisearch/index.rb', line 582

def update_typo_tolerance(typo_tolerance_attributes)
  attributes = Utils.transform_attributes(typo_tolerance_attributes)
  response = http_patch("/indexes/#{@uid}/settings/typo-tolerance", attributes)
  Models::Task.new(response, task_endpoint)
end

#wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50) ⇒ Object



374
375
376
# File 'lib/meilisearch/index.rb', line 374

def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
  task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms)
end