GithubHelp home page GithubHelp logo

meilisearch / meilisearch-rails Goto Github PK

View Code? Open in Web Editor NEW
274.0 6.0 47.0 1.19 MB

Meilisearch integration for Ruby on Rails

Home Page: https://www.meilisearch.com

License: MIT License

Ruby 90.89% JavaScript 1.95% SCSS 1.01% HTML 5.85% Dockerfile 0.30%
rails ruby-on-rails ruby meilisearch

meilisearch-rails's People

Contributors

0xmostafa avatar adopi avatar alallema avatar anderson avatar bb avatar bors[bot] avatar brunoocasali avatar carofg avatar cjilbert504 avatar curquiza avatar danirod avatar dependabot[bot] avatar ellnix avatar excid3 avatar hosamaly avatar jason-hobbs avatar jasonbarnabe avatar jitingcn avatar jmarsh24 avatar kobaltz avatar meili-bors[bot] avatar meili-bot avatar nykma avatar rolandasb avatar sabljak avatar sunny avatar uvera avatar vaibhavwakde52 avatar yagince avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

meilisearch-rails's Issues

Something happened with versions I think

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?

Add each_with_hit and map_with_hits methods?

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!

Is it possible to deactivate Meilisearch on certain conditions?

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)
}

Error when enabling paging with Ruby3 / kaminari

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:

  • OS: Linux
  • MeiliSearch version: 0.20.0
  • meilisearch-rails version: 0.2.1
  • Rails version: 6.1.4

Add meilisearch backend version check

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

Add rubocop (linter):

  • in the Gemfile (dev group)
  • in the CI
  • how to launch it in the CONTRIBUTING

Allow tests randomization

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
  • Then try locally to run x times one particular file (start with the smaller ones).
  • Spot the order-dependency problems, some hints:
    • Look for data going down over the tests (creating in one test and using in another)
    • before/after hooks introducing behaviors, creating mocks etc...
    • Fix the problem
    • Remove the RSpec randomization config
    • Make a new PR for each test file (this will help us to approve the PR even faster)

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!

Ability to change the index uid during run time?

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?

Possible sanitization change leads to a unexpected behavior on integration_spec

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.

Add sortable attributes option

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.

Update .code-samples.meilisearch.yaml

⚠️ This issue is generated, it means the examples and the namings do not necessarily correspond to the language of this repository.
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:

  • Add new file .code-samples.meilisearch.yml
  • Add landing_getting_started_1 code samples

Search for a term on multiple models/Indexes

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])

Options `primary_key` vs `id`

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

Remove useless code from Algolia
ex: all about replica, tag...

Should be done once all the tests are added. See #17

Improve pagination documentation

  • Specify it's important to add the page to the search query. Example: Book.search('', page: (params['page'] || 1))
  • Specify that hitsPerPage only works when using the pagination_backend option

hitsPerPage param does not work

Following 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.

N+1 queries on results

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.

Clarify pagy pagination usage in documents

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.

Add playground and more tests

  • Add a playground in the /playground folder. This playground is a Rails application (with views) to check manually the plugin works
  • for each failure you find out with the playground, you must create an automated test

Default configuration

As 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.

Search within custom index (with pagy)

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

Unable to install the gem in any Rails app

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,

  1. Rails 5.2.3 / Ruby 2.6.3
  2. Rails 7.1.0.alpha / Ruby 2.7.3 (This is a completely new Rails app with no dependencies other than core Rails.)

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):

  • OS: macOS Monterey, 12.2 Beta
  • Meilisearch version: meilisearch (0.18.0)
  • meilisearch-rails version: meilisearch-rails (0.4.1)

upgrade rspec 3

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):

  • OS: Arch Linux
  • MeiliSearch version: v.0.24.0
  • meilisearch-rails version: 0.3.0
  • Ruby version: 2.7.2
  • Rake version: 13.0.6

superclass mismatch for class Task

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):

  • OS: ruby:2.6.9-alpine3.15
  • MeiliSearch version: v0.25.2
  • meilisearch-rails version: v0.4.0
  • Rails version: 6.0.4.3

Make tests run with default config

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.

[Question] - Getting occasional Net::ReadTimeout with #<TCPSocket:(closed) errors

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.

Custom Attributes does not returns on search

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?

Add tests to ensure rails app will load Meilisearch correctly

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.

Total hits incorrect when using pagination backend

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):

  • meilisearch-rails version: 0.5.1

LoadError (cannot load such file -- meilisearch/ms_job)

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.