meilisearch / meilisearch-rails Goto Github PK
View Code? Open in Web Editor NEWMeilisearch integration for Ruby on Rails
Home Page: https://www.meilisearch.com
License: MIT License
Meilisearch integration for Ruby on Rails
Home Page: https://www.meilisearch.com
License: MIT License
Add a CI (as GitHub Actions) that runs the test for each PR.
Was using connected to Dockerized version of Meilisearch using follwing config
MeiliSearch.configuration = {
meilisearch_host: "http://#{Rails.application.credentials[:meilisearch][:host]}:#{Rails.application.credentials[:meilisearch][:port]}",
meilisearch_api_key: Rails.application.credentials[:meilisearch][:key],
pagination_backend: :kaminari,
timeout: 2,
max_retries: 1,
}
which translates to this
{:meilisearch_host=>"http://localhost:7700", :meilisearch_api_key=>"thisIsAMasterKey", :pagination_backend=>:kaminari, :timeout=>2, :max_retries=>1}
That was working and fine, and can still access the Mini dashboard with those credentials
But from yesterday afternoon, I started getting NoMethodError (undefined method `qualified_version' for MeiliSearch:Module) when running Order.reindex! or doing a search.
I updated to 0.4.1 and reimplemented the module in the Models with include MeiliSearch::Rails instead of include MeiliSearch and MeiliSearch::Rails.configuration in the configuration file, but now get uninitialized constant MeiliSearch::Rails (NameError) on start
Any ideas?
Should contain:
In more general, use the same template as https://github.com/meilisearch/meilisearch-laravel-scout
In .rubocop_todo.yml
we currently have some remain offenses, we need to remove them :)
The readme references using a worker instead of a job. This should be updated to prevent confusion.
Description
Hi folks, thank you so much for this wonderful gem!
I think it would be pretty useful to add some handy collection methods to the result.
Basic example
Some examples for pseudocode :
def each_with_hits(&block)
results.zip(raw_answer["hits"]).each(&block)
end
def map_with_hits(&block)
results.zip(raw_answer["hits"]).map(&block)
end
Would love to hear your thoughts!
Description
I'd like to be able to deactivate all requests to Meilisearch instance on a given condition.
For example to run tests, or to activate it on certain environments.
Basic example
I tried meilisearch if: ->(_) { false }
but it sends a DELETE request.
And if I don't configure the service, I get Please configure Meilisearch. Set MeiliSearch::Rails.configuration = {meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'} (MeiliSearch::Rails::NotConfigured)
.
It could be something like:
MeiliSearch::Rails.configuration = {
activate: true | false (default: true)
}
Add a contributing file, following this template:
https://github.com/meilisearch/integration-guides/blob/main/templates/CONTRIBUTING.md
The development workflow part should be filled according to this repo, of course
Description
After enabling pagination using {pagination_backend: :kaminari}
, any search will throw an error.
Expected behavior
MyModel.search()
should work
Current behavior
ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /home/nykma/.asdf/installs/ruby/3.0.1/lib/ruby/gems/3.0.0/gems/kaminari-core-1.2.1/lib/kaminari/models/array_extension.rb:18:in `initialize'
Environment:
Description
When I encountered a problem with meilisearch not being able to create model index after upgrading gems yesterday, after an hour of troubleshooting I found that this was caused by an incompatible version of meilisearch. I thought it would be a good idea to add a version check in the rails initializer.
Basic example
Some other libraries I have used have similar functionality, for example https://github.com/stimulusreflex/stimulus_reflex/blob/master/lib/stimulus_reflex/utils/sanity_checker.rb
Add rubocop (linter):
Our test suite should be able to run in a randomized environment, this will help us to improve the confidence in our tests and of course, it will help a lot in the maintenance of the test suite.
This is a good first issue, and we could take small steps along the way in order to enable the randomization, for example:
Locally enable rspec randomization:
# spec_helper.rb:31
c.order = :random
Some seeds to start:
bundle exec rspec --seed=49484
bundle exec rspec --seed=64618
bundle exec rspec --seed=65507
After this, we should have a good and maintainable test suite!
Is there a way of changing the index_uid
during runtime?
I'm creating a SaaS app and want to have one index per user:
meilisearch index_uid: "Story_#{ActsAsTenant.current_tenant&.name}_#{Rails.env}",
synchronous: Rails.env.test?,
enqueue: !Rails.env.test?,
unless: :completed_and_one_off? || :discarded? do
attribute :goal, :reason, :repeatable, :id
attribute :values do
values.map(&:name)
end
searchable_attributes %i[goal reason values]
end
This works fine in test and development environments, however in production the index_uid
is never re-evaluated and breaks the implementation. Is there a way around this, or should I be taking a different approach?
Description
According to @jitingcn we are facing an issue regarding a possible change in the rails sanitizer.
We need to dig into that to understand better why this changed and if it affects any other part of the integration.
We could use this time to refactor that particular spec in order to explicitly tell the reader which version of rails is responsible for what behavior, today we have flow control by using exceptions and this could be improved to be more clear π₯
Expected behavior
No exception should be raised.
Current behavior
An exception is being raised.
Description
In keeping with the common theme of the other MeiliSearch libraries, it would be nice to also have the sortableAttributes option within this library as well.
Basic example
meilisearch if: :published_at? do
attribute :category_list, :name, :description, :number
sortable_attributes [:number]
end
Other
Any other things you want to add.
A getting started = a quick way to use the gem using the basics options.
Also, if you are a maintainer, feel free to add any clarification and instruction about this issue.
Sorry if this is already partially/completely implemented, feel free to let me know about the state of this issue in the repo.
Related to meilisearch/integration-guides#185
Since there is no code-samples in this repository, you should add a new file .code-samples.meilisearch.yml
into the root of this directory, and add a new key containing the landing_getting_started_1
contents.
Check out the #185 issue (Add landing_getting_started_1
code samples section) for more information
TODO:
.code-samples.meilisearch.yml
landing_getting_started_1
code samplesIn the code and in the README, we use the old ruby syntax
{ :key => 'value' }
We should use instead:
{ key: 'value' }
Tell me if I'm wrong and if it's not possible @CaroFG!
Description
I have just migrated our application from searchkick to meilisearch however meilisearch doesn't have a way I can search for single term across multiple indexes or models like searchkick does.
Basic example
I want to to be able to search my term on at least one model
example:
Meilisearch.search(term, models: [...index_names])
From my POV these two options seem to do the same: inform MeiliSearch about the unique identifier.
If they indeed to the same, we should remove one to keep only the other one.
Remove useless code from Algolia
ex: all about replica, tag...
Should be done once all the tests are added. See #17
Book.search('', page: (params['page'] || 1))
pagination_backend
optionFollowing the docs I try to limit number of results
products = Product.search params[:query], hitsPerPage: 9
But I got and error
MeiliSearch::ApiError (400 Bad Request - Invalid JSON: unknown field hitsPerPage, expected one of q, offset, limit, attributesToRetrieve, attributesToCrop cropLength, attributesToHighlight, filters, matches, facetFilters, facetsDistribution at line 1 column 29. See https://docs.meilisearch.com/errors#bad_request.)
Changing param name to "limit" (as exception message explains) - solves the problem
products = Product.search params[:query], limit: 9
Moreover, with Rails style guide, camelcase names for params is not acceptable, undersored names should be used.
Should be done after the first release of this repo
I have results being returned that reference other models and would like to reduce the number of N+1 queries being executed. Usually I would do Model.includes(:reference)
but this doesn't work with meilisearch.
I noticed in the readme it mentions that the backend pagination backend now supports pagy.
But when I try this configuration it gave me an error.
MeiliSearch::Rails.configuration = {
meilisearch_host: 'YourMeilisearchHost',
meilisearch_api_key: 'YourMeilisearchAPIKey',
pagination_backend: :pagy
}
After further investigation, the pagy backend pagination is provided by the pagy library and is different from other backend pagination setups. I think this needs to be clarified in the documentation.
/playground
folder. This playground is a Rails application (with views) to check manually the plugin worksAs convention over configuration, I think this gem should have meilisearch_host default value "http://127.0.0.1:7700"
This eliminate the need to manually create initializer file only to define meilisearch_host option.
Since adding sortable_attributes as a option, the documentation should reflect this along with an example use case.
I am reading through the docs and it nicely describes how to create a custom index. So, now i have an index of all items and additional conditional index - in the same model.
I cannot not figure out how to limit my search (with or without pagy) to specific index.
Can some please explain how that is done, preferably with pagy.
Thx
Inside this file spec/integration_spec.rb
we saw some specs testing methods by using .send
they should be replaced.
Some references about why this practice should be avoided:
https://anthonysciamanna.com/2016/02/14/should-private-methods-be-tested.html#unit-tests-should-only-test-public-methods
Description
I added gem 'meilisearch-rails'
to by Gemfile and did a bundle install. I then go to rails console and just type in,
irb(main):007:0> MeiliSearch::Rails
Traceback (most recent call last):
2: from (irb):6
1: from (irb):7:in `rescue in irb_binding'
NameError (uninitialized constant MeiliSearch::Rails)
irb(main):008:0>
I tried this on,
Tried it even on basic IRB shell. Same error.
Expected behavior
The gem should load!
Current behavior
Doesn't load. When I do,
irb(main):001:0> require 'meilisearch-rails'
=> false
So it has been required but it can not find the module MeiliSearch::Rails
.
We (@heertheeswaran and I) downgraded to 0.3 as well, same bug. Please fix ASAP. We want to move from Algolia to Meili.
Environment (please complete the following information):
Parmeters such as hitsPerPage
should be tranformed into camel case => hits_per_page
to respect Rails convention
Description
I got an error when I ran rake
after bundle install
, After some searching (sof), the issue is that rake 11.0 removed the last_comment
method, which was used by rspec-core (< 3.4.4)
Possible solutions include restricting rake version
gem 'rake', '< 11.0'
or upgrading the rspec version, which I think is a good option for long-term consideration.
This issue only affects run rake locally and does not affect run rspec tests alone, or the normal use the gem.
Logs
$ rake
rake aborted!
NoMethodError: undefined method `last_comment' for #<Rake::Application:0x00007f1be23e2588>
/home/jiting/RubymineProjects/meilisearch-rails/Rakefile:16:in `new'
/home/jiting/RubymineProjects/meilisearch-rails/Rakefile:16:in `<top (required)>'
/home/jiting/.rbenv/versions/2.7.2/bin/bundle:23:in `load'
/home/jiting/.rbenv/versions/2.7.2/bin/bundle:23:in `<main>'
Environment (please complete the following information):
Add simplecov
gem to track code coverage.
Description
I'm trying to add meilisearch to an existing Rails app. I've followed your blog post but after installing an enabling meilisearch-rails I get the following error:
F, [2022-01-23T08:20:09.249851 #1] FATAL -- : [cc86965b-0e6c-4b50-a197-53b73cea5c65]
TypeError - superclass mismatch for class Task:
app/models/task.rb:33:in `<top (required)>'
app/views/layouts/_header.html.erb:57
app/views/layouts/application.html.erb:55
Expected behavior
No superclass mismatch :)
Any help appreciated how to solve this!
Current behavior
F, [2022-01-23T08:20:09.249851 #1] FATAL -- : [cc86965b-0e6c-4b50-a197-53b73cea5c65]
TypeError - superclass mismatch for class Task:
app/models/task.rb:33:in `<top (required)>'
app/views/layouts/_header.html.erb:57
app/views/layouts/application.html.erb:55
Environment (please complete the following information):
Make the tests run with this config by default
MEILISEARCH_HOST="http://127.0.0.1:7700"
MEILISEARCH_API_KEY="masterKey"
without passing it as env variables.
The users should still be able to pass other configs if wanted.
https://github.com/meilisearch/meilisearch-ruby/releases
At least v0.15.1
I am looking for any guidance around improving how we are querying on a model and its association. As it stands we occasionally see the error listed in the title and see a 500 error when it occurs. We believe that the reason for the timeout is the large amount of associated model attribute records that are indexed and being returned. For reference here is how the parent model is setup:
class ForumThread < ApplicationRecord
extend Pagy::Meilisearch
include MeiliSearch::Rails
meilisearch unless: :is_spam? do
attribute :title, :updated_at, :solved, :user_id
attribute :forum_posts do
forum_posts.clean.pluck(:body)
end
attribute :participants do
forum_posts.clean.pluck(:user_id)
end
filterable_attributes [:solved, :user_id, :forum_posts, :participants]
sortable_attributes [:updated_at]
end
As a temporary fix we have updated our MeiliSearch::Rails.configuration as follows:
MeiliSearch::Rails.configuration = {
meilisearch_host: 'http://127.0.0.1:7700',
meilisearch_api_key: Rails.application.secrets.meilisearch_api_key,
timeout: 3,
max_retries: 2
}
This seems to have mostly addressed the issue but I'm curious as to whether you had any thoughts on how we could improve the querying here. One thought we had was to set the :forum_posts
as a searched but not displayed attribute to see if that would help. The other thought we had is that instead of calling just forum_posts.clean.pluck(:body)
which gives an array back to search through, instead join the bodies into one large string. Not sure if that would have any impact or not though.
Thanks for any and all help on this one! It is much appreciated.
HI All,
I've try to use custom attributes on my model, but when a search, this field is not returned. The custom field is ok on meilisearch UI.
My Product Model
class Product < ApplicationRecord
include MeiliSearch::Rails
include MoneyRails::ActionViewExtension
belongs_to :brand, optional: true, touch: true
has_many :product_languages, dependent: :destroy
after_save { product_languages.each(&:touch) }
# MeiliSearch
meilisearch force_utf8_encoding: true do
attribute :id, :name, :short_description, :full_description
attribute :product_languages do
product_languages.pluck(:language, :name, :short_description, :full_description)
end
attribute :humanized_price do
humanized_money price_to_customer
end
attribute :photo do
Rails.application.routes.url_helpers.rails_representation_url(self.main_image.variant(resize: "600x600").processed, only_path: true) if self.main_image.attached?
end
attribute :brand do
brand.name
end
displayed_attributes [:id, :name, :short_description, :full_description, :product_languages, :humanized_price, :photo, :brand]
filterable_attributes [:id, :name, :language, :short_description, :full_description, :brand, :product_languages]
sortable_attributes [:id, :name, :humanized_price, :brand]
end
after_touch :index!
def will_save_change_to_humanized_price?
will_save_change_to_cost_price_cents? || will_save_change_to_price_cents? || will_save_change_to_recommended_retail_price_cents? || will_save_change_to_default_shipping_cost_cents? || will_save_change_to_vat_cents?
end
def will_save_change_to_photo?
will_save_change_to_main_image?
end
end
Searching on rails console:
[56] pry(main)> p = Product.search('33074510115')
[57] pry(main)> p[0].photo
NoMethodError: undefined method `photo' for #<Product:0x00007f0bdd0eb588>
from /home/app/.rvm/gems/ruby-2.6.0/gems/activemodel-5.2.6/lib/active_model/attribute_methods.rb:430:in `method_missing'
[58] pry(main)> p[0].humanized_price
NoMethodError: undefined method `humanized_price' for #<Product:0x00007f0bdd0eb588>
Did you mean? humanized_money
from /home/app/.rvm/gems/ruby-2.6.0/gems/activemodel-5.2.6/lib/active_model/attribute_methods.rb:430:in `method_missing'
[59] pry(main)> p[0].brand
Brand Load (0.7ms) SELECT `brands`.* FROM `brands` WHERE `brands`.`id` = 15848592665 LIMIT 1
=> #<Brand:0x00007f0bdd3ddea8
id: 15848592665,
name: "SEVEN CREATIONS",
url: "seven-creations",
brand_id: nil,
created_at: Sun, 13 Mar 2022 08:41:27 WET +00:00,
updated_at: Sun, 13 Mar 2022 08:41:55 WET +00:00>
[60] pry(main)>
The fields photo, humanized_price brand, are not returning as expected.
I'm doing something wrong or it's a bug?
We should have a way to automate testing the potentially breaking
new versions.
One idea to do that is to use the playground
rails application in this app to load Meilisearch and make some basic operations in order to guarantee the minimum operational status as possible, since we do not have type checking yet.
There are still some references to Algolia
In a GH action
Can be inspired of https://github.com/meilisearch/meilisearch-ruby/blob/main/.github/workflows/gempush.yml
Description
When using a pagination backend, the total hits is capped at 200. I tried this with will_paginate, but it should also happen with kaminari.
When a pagination backend is set, the limit parameter is set to 200 with no option to override in MeiliSearch::Rails::ClassMethods#ms_search.
Later in the method total hits is calculated by total_hits = json['hits'].length. This is capped at 200 due to the limit set earlier.
total_hits should instead be set to the value of nbHits.
Expected behavior
When more than 200 documents match the search, the total hits count should be match the value returned.
What you expected to happen.
Current behavior
Currently the total number of hits is capped at 200.
Environment (please complete the following information):
Screenshots or Logs
I, [2022-02-13T04:05:42.448366 #26] INFO -- : [2b5c50a3-fcba-4272-b087-bab9f11de456] method=PATCH path=/entries/296 format=turbo_stream controller=EntriesController action=update status=500 error='LoadError: cannot load such file -- meilisearch/ms_job' duration=11.81 view=0.00 db=3.29
F, [2022-02-13T04:05:42.448883 #26] FATAL -- : [2b5c50a3-fcba-4272-b087-bab9f11de456]
[2b5c50a3-fcba-4272-b087-bab9f11de456] LoadError (cannot load such file -- meilisearch/ms_job):
[2b5c50a3-fcba-4272-b087-bab9f11de456]
[2b5c50a3-fcba-4272-b087-bab9f11de456] app/controllers/entries_controller.rb:73:in block in update'
[2b5c50a3-fcba-4272-b087-bab9f11de456] app/controllers/entries_controller.rb:62:in update
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.