-
Notifications
You must be signed in to change notification settings - Fork 52
Refactor Meilisearch::Index class into focused modules
#677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c9c5f15
7bb64c3
ce2547c
26803ab
01ebc25
990dfd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 That's why we can't place We could place You might also wonder: why not use plain mixins like However, I find it cleaner to define
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brunoocasali so what do you think? |
||
| end | ||
| end | ||
Large diffs are not rendered by default.
| 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 |
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏