Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-12-29 19:26:11 UTC using RuboCop version 1.75.8.
# on 2026-02-11 08:35:21 UTC using RuboCop version 1.81.7.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -11,16 +11,21 @@
Metrics/AbcSize:
Max: 24

# Offense count: 4
# Offense count: 3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 541
Max: 128

# Offense count: 4
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 18

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 264

# Offense count: 3
# Configuration parameters: Max, CountKeywordArgs.
Metrics/ParameterLists:
Expand All @@ -31,15 +36,9 @@ Naming/AccessorMethodName:
Exclude:
- 'lib/meilisearch/index.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
Style/IfUnlessModifier:
Exclude:
- 'lib/meilisearch/index.rb'

# Offense count: 21
# Offense count: 23
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
# URISchemes: http, https
Layout/LineLength:
Max: 160
1,354 changes: 16 additions & 1,338 deletions lib/meilisearch/index.rb

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions lib/meilisearch/index/compact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Meilisearch
class Index
# Runs database compaction to optimize disk usage.
module Compact
# Run database compaction for this index.
#
# @note Meilisearch must temporarily duplicate the database during compaction.
# You need at least twice the current size of your database in free disk space.
#
# @see https://www.meilisearch.com/docs/reference/api/compact Meilisearch API Reference
# @return [Models::Task] The index compaction async task.
def compact
response = http_post "/indexes/#{@uid}/compact"
Models::Task.new(response, task_endpoint)
end
end

include Compact
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there! I loved your PR altough I have one question regarding the class reopening during these module definitions. I think I have never seen this being used like this. Is there any benefit to it over regular mixin usage?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback! Let me explain the reasoning behind this decision.

First of all, we need to ensure that Meilisearch::Index inherits from Meilisearch::HTTPRequest, which means lib/meilisearch/index.rb must be loaded before the submodules since they reopen class Index.

That's why we can't place require 'meilisearch/index/compact' at the top of lib/meilisearch/index.rb: the modules must be required after the class Index < HTTPRequest definition.

We could place include Compact right after the require statements, but I prefer to put it inside the Compact module definition to make it self-contained and keep Meilisearch::Index cleaner.

You might also wonder: why not use plain mixins like Meilisearch::IndexCompact and require/include them the usual way? That's certainly possible, and I considered it.

However, I find it cleaner to define Meilisearch::Index::Compact as a nested module and place it under the lib/meilisearch/index folder - it better reflects the logical hierarchy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brunoocasali so what do you think?

end
end
487 changes: 487 additions & 0 deletions lib/meilisearch/index/documents.rb

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions lib/meilisearch/index/facet_search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Meilisearch
class Index
# Searches for facet values matching a query.
# @see https://www.meilisearch.com/docs/reference/api/facet_search Meilisearch API Reference
module FacetSearch
# Search for facet values.
#
# client.index('books').facet_search('genres', 'fiction', filter: 'rating > 3')
# # {
# # "facetHits": [
# # {
# # "value": "fiction",
# # "count": 7
# # }
# # ],
# # "facetQuery": "fiction",
# # "processingTimeMs": 0
# # }
#
# @param name [String] Facet name to search values on.
# @param query [String] Search query for a given facet value.
# @param options [Hash{Symbol => Object}] Additional options, see API Reference.
# @return [Hash{String => Object}] Facet search result.
#
# @see https://www.meilisearch.com/docs/reference/api/facet_search#perform-a-facet-search Meilisearch API Reference
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
end

include FacetSearch
end
end
48 changes: 48 additions & 0 deletions lib/meilisearch/index/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Meilisearch
class Index
# Performs keyword and similarity-based search queries on an index.
# @see https://www.meilisearch.com/docs/reference/api/search Meilisearch API Reference
module Search
# Run a search on this index.
#
# Check Meilisearch API Reference for all options.
#
# @param query [String] The query string for the search.
# @param options [Hash{Symbol => Object}] Search options.
#
# @return [Hash{String => Object}] Search results
# @see https://www.meilisearch.com/docs/reference/api/search#search-in-an-index-with-post Meilisearch API Reference
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

# Run a search for semantically similar documents.
#
# An embedder must be configured and specified.
# Check Meilisearch API Reference for all options.
#
# @param document_id [String, Integer] The base document for comparisons.
# @param options [Hash{Symbol => Object}] Search options. Including a mandatory :embedder option.
#
# @return [Hash{String => Object}] Search results
# @see https://www.meilisearch.com/docs/reference/api/similar#get-similar-documents-with-post Meilisearch API Reference
def search_similar_documents(document_id, **options)
options.merge!(id: document_id)
options = Utils.transform_attributes(options)

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

include Search
end
end
Loading