Giter VIP home page Giter VIP logo

meilisearch-rails's Introduction

Meilisearch-Rails

Meilisearch Rails

Test License Bors enabled

⚑ The Meilisearch integration for Ruby on Rails πŸ’Ž

Meilisearch Rails is the Meilisearch integration for Ruby on Rails developers.

Meilisearch is an open-source search engine. Learn more about Meilisearch.

Table of Contents

πŸ“– Documentation

The whole usage of this gem is detailed in this README.

To learn more about Meilisearch, check out our Documentation or our API References.

⚑ Supercharge your Meilisearch experience

Say goodbye to server deployment and manual updates with Meilisearch Cloud. No credit card required.

πŸ€– Compatibility with Meilisearch

This package guarantees compatibility with version v1.x of Meilisearch, but some features may not be present. Please check the issues for more info.

πŸ”§ Installation

This package requires Ruby version 2.7.0 or later and Rails 6.1 or later. It may work in older versions but it is not officially supported.

With gem in command line:

gem install meilisearch-rails

In your Gemfile with bundler:

source 'https://rubygems.org'

gem 'meilisearch-rails'

Run Meilisearch

There are many easy ways to download and run a Meilisearch instance.

For example, if you use Docker:

docker pull getmeili/meilisearch:latest # Fetch the latest version of Meilisearch image from Docker Hub
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest meilisearch --master-key=masterKey

NB: you can also download Meilisearch from Homebrew or APT.

πŸš€ Getting started

Configuration

Create a new file config/initializers/meilisearch.rb to setup your MEILISEARCH_HOST and MEILISEARCH_API_KEY

MeiliSearch::Rails.configuration = {
  meilisearch_url: ENV.fetch('MEILISEARCH_HOST', 'http://localhost:7700'),
  meilisearch_api_key: ENV.fetch('MEILISEARCH_API_KEY', 'YourMeilisearchAPIKey')
}

Or you can run a rake task to create the initializer file for you:

bin/rails meilisearch:install

The gem is compatible with ActiveRecord, Mongoid and Sequel.

⚠️ Note that even if you want to use all the default options, you must declare an empty meilisearch block in your model.

Add documents

The following code will create a Book index and add search capabilities to your Book model.

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch do
    attribute :title, :author # only the attributes 'title', and 'author' will be sent to Meilisearch
    # all attributes will be sent to Meilisearch if block is left empty
  end
end

Automatic indexing

As soon as you configure your model as mentioned above, meilisearch-rails will keep your database table data in sync with your Meilisearch instance using the ActiveRecord callbacks automatically.

Basic Backend Search

We strongly recommend the use of front-end search through our JavaScript API Client or Instant Meilisearch plugin

Search returns ORM-compliant objects reloaded from your database.

# Meilisearch is typo-tolerant:
hits = Book.search('harry pottre')
hits.each do |hit|
  puts hit.title
  puts hit.author
end

Extra Configuration

Requests made to Meilisearch may timeout and retry. To adapt the behavior to your needs, you can change the parameters during configuration:

MeiliSearch::Rails.configuration = {
  meilisearch_url: 'YourMeilisearchUrl',
  meilisearch_api_key: 'YourMeilisearchAPIKey',
  timeout: 2,
  max_retries: 1,
}

Compatibility

If your model already has methods that meilisearch-rails defines such as search and index, they will not be redefined. You can target the meilisearch-rails-defined methods by prefixing with ms_, e.g. Book.ms_search('harry potter').

βš™οΈ Settings

You can configure the index settings by adding them inside the meilisearch block as shown below:

class Book < ApplicationRecord
  include MeiliSearch::Rails

  meilisearch do
    searchable_attributes [:title, :author, :publisher, :description]
    filterable_attributes [:genre]
    sortable_attributes [:title]
    ranking_rules [
      'proximity',
      'typo',
      'words',
      'attribute',
      'sort',
      'exactness',
      'publication_year:desc'
    ]
    synonyms nyc: ['new york']

    # The following parameters are applied when calling the search() method:
    attributes_to_highlight ['*']
    attributes_to_crop [:description]
    crop_length 10
    faceting max_values_per_facet: 2000
    pagination max_total_hits: 1000
  end
end

Check the dedicated section of the documentation, for more information on the settings.

πŸ” Custom search

All the supported options are described in the search parameters section of the documentation.

Book.search('Harry', attributes_to_highlight: ['*'])

Then it's possible to retrieve the highlighted or cropped value by using the formatted method available in the object.

harry_book.formatted # => {"id"=>"1", "name"=>"<em>Harry</em> Potter", "description"=>…

πŸ‘‰ Don't forget that attributes_to_highlight, attributes_to_crop, and crop_length can be set up in the meilisearch block of your model.

πŸ” Sorted search

As an example of how to use the sort option, here is how you could achieve returning all books sorted by title in ascending order:

Book.search('*', sort: ['title:asc'])

πŸ‘‰ Don't forget to set up the sortable_attributes option in the meilisearch block of your model.

πŸ”πŸ” Multi search

Meilisearch supports searching multiple models at the same time (see πŸ” Custom search for search options):

multi_search_results = MeiliSearch::Rails.multi_search(
  Book => { q: 'Harry' },
  Manga => { q: 'Attack' }
)

You can iterate through the results with .each or .each_result:

<% multi_search_results.each do |record| %>
  <p><%= record.title %></p>
  <p><%= record.author %></p>
<% end %>

<p>Harry Potter and the Philosopher's Stone</p>
<p>J. K. Rowling</p>
<p>Harry Potter and the Chamber of Secrets</p>
<p>J. K. Rowling</p>
<p>Attack on Titan</p>
<p>Iseyama</p>
<% multi_search_results.each_result do |klass, results| %>
  <p><%= klass.name.pluralize %></p>

  <ul>
    <% results.each do |record| %>
      <li><%= record.title %></li>
    <% end %>
  </ul>
<% end %>


<p>Books</p>
<ul>
  <li>Harry Potter and the Philosopher's Stone</li>
  <li>Harry Potter and the Chamber of Secrets</li>
</ul>
<p>Mangas</p>
<ul>
  <li>Attack on Titan</li>
</ul>

See the official multi search documentation.

πŸͺ› Options

Meilisearch configuration & environment

Backend Pagination with kaminari or will_paginate

This gem supports:

Specify the :pagination_backend in the configuration file:

MeiliSearch::Rails.configuration = {
  meilisearch_url: 'YourMeilisearchUrl',
  meilisearch_api_key: 'YourMeilisearchAPIKey',
  pagination_backend: :kaminari # :will_paginate
}

Then, as soon as you use the search method, the returning results will be paginated:

# controller
@hits = Book.search('harry potter')

# views
<% @hits.each do |hit| %>
  <%= hit.title %>
  <%= hit.author %>
<% end %>

<%= paginate @hits %> # if using kaminari

<%= will_paginate @hits %> # if using will_paginate

The number of hits per page defaults to 20, you can customize it by adding the hits_per_page parameter to your search:

Book.search('harry potter', hits_per_page: 10)

Backend Pagination with pagy

This gem supports pagy to paginate your search results.

To use pagy with your meilisearch-rails you need to:

Add the pagy gem to your Gemfile. Create a new initializer pagy.rb with this:

# config/initializers/pagy.rb

require 'pagy/extras/meilisearch'

Then in your model you must extend Pagy::Meilisearch:

class Book < ApplicationRecord
  include MeiliSearch::Rails
  extend Pagy::Meilisearch

  meilisearch # ...
end

And in your controller and view:

# controllers/books_controller.rb
def search
  hits = Book.pagy_search(params[:query])
  @pagy, @hits = pagy_meilisearch(hits, items: 25)
end


# views/books/search.html.rb
<%== pagy_nav(@pagy) %>

⚠️ There is no need to set pagination_backend in the configuration block MeiliSearch::Rails.configuration for pagy.

Check ddnexus/pagy for more information.

Deactivate Meilisearch in certain moments

By default, HTTP connections to the Meilisearch URL are always active, but sometimes you want to disable the HTTP requests in a particular moment or environment.
you have multiple ways to achieve this.

By adding active: false in the configuration initializer:

MeiliSearch::Rails.configuration = {
  meilisearch_url: 'YourMeilisearchUrl',
  meilisearch_api_key: 'YourMeilisearchAPIKey',
  active: false
}

Or you can disable programmatically:

MeiliSearch::Rails.deactivate! # all the following HTTP calls will be dismissed.

# or you can pass a block to it:

MeiliSearch::Rails.deactivate! do
  # every Meilisearch call here will be dismissed, no error will be raised.
  # after the block, Meilisearch state will be active. 
end

You can also activate if you deactivated earlier:

MeiliSearch::Rails.activate!

⚠️ These calls are persistent, so prefer to use the method with the block. This way, you will not forget to activate it afterward.

Custom index_uid

By default, the index_uid will be the class name, e.g. Book. You can customize the index_uid by using the index_uid: option.

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch index_uid: 'MyCustomUID'
end

Index UID according to the environment

You can suffix the index UID with the current Rails environment by setting it globally:

MeiliSearch::Rails.configuration = {
  meilisearch_url: 'YourMeilisearchUrl',
  meilisearch_api_key: 'YourMeilisearchAPIKey',
  per_environment: true
}

This way your index UID will look like this "Book_#{Rails.env}".

Index configuration

Custom attribute definition

You can add a custom attribute by using the add_attribute option or by using a block.

⚠️ When using custom attributes, the gem is not able to detect changes on them. Your record will be pushed to the API even if the custom attribute didn't change. To prevent this behavior, you can create a will_save_change_to_#{attr_name}? method.

class Author < ApplicationRecord
  include MeiliSearch::Rails

  meilisearch do
    attribute :first_name, :last_name
    attribute :full_name do
      "#{first_name} #{last_name}"
    end
    add_attribute :full_name_reversed
  end

  def full_name_reversed
    "#{last_name} #{first_name}"
  end

  def will_save_change_to_full_name?
    will_save_change_to_first_name? || will_save_change_to_last_name?
  end

  def will_save_change_to_full_name_reversed?
    will_save_change_to_first_name? || will_save_change_to_last_name?
  end
end

Custom primary key

By default, the primary key is based on your record's id. You can change this behavior by specifying the primary_key: option.

Note that the primary key must return a unique value otherwise your data could be overwritten.

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch primary_key: :isbn # isbn is a column in your table definition.
end

You can also set the primary_key as a method, this method will be evaluated in runtime, and its return will be used as the reference to the document when Meilisearch needs it.

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch primary_key: :my_custom_ms_id

  private

  def my_custom_ms_id
    "isbn_#{primary_key}" # ensure this return is unique, otherwise you'll lose data.
  end
end

Conditional indexing

You can control if a record must be indexed by using the if: or unless: options.
As soon as you use those constraints, add_documents and delete_documents calls will be performed in order to keep the index synced with the DB. To prevent this behavior, you can create a will_save_change_to_#{attr_name}? method.

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch if: :published?, unless: :premium?

  def published?
    # [...]
  end

  def premium?
    # [...]
  end

  def will_save_change_to_published?
    # return true only if you know that the 'published' state changed
  end
end
Target multiple indexes

You can index a record in several indexes using the add_index option:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  PUBLIC_INDEX_UID = 'Books'
  SECURED_INDEX_UID = 'PrivateBooks'

  # store all books in index 'SECURED_INDEX_UID'
  meilisearch index_uid: SECURED_INDEX_UID do
    searchable_attributes [:title, :author]

    # store all 'public' (released and not premium) books in index 'PUBLIC_INDEX_UID'
    add_index PUBLIC_INDEX_UID, if: :public? do
      searchable_attributes [:title, :author]
    end
  end

  private

  def public?
    released? && !premium?
  end
end

Share a single index

You may want to share an index between several models. You'll need to ensure you don't have any conflict with the primary_key of the models involved.

class Cat < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch index_uid: 'Animals', primary_key: :ms_id

  private

  def ms_id
    "cat_#{primary_key}" # ensure the cats & dogs primary_keys are not conflicting
  end
end

class Dog < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch index_uid: 'Animals', primary_key: :ms_id

  private

  def ms_id
    "dog_#{primary_key}" # ensure the cats & dogs primary_keys are not conflicting
  end
end

Queues & background jobs

You can configure the auto-indexing & auto-removal process to use a queue to perform those operations in background. ActiveJob queues are used by default but you can define your own queuing mechanism:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch enqueue: true # ActiveJob will be triggered using a `meilisearch` queue
end

πŸ€” If you are performing updates and deletions in the background, a record deletion can be committed to your database prior to the job actually executing. Thus if you were to load the record to remove it from the database then your ActiveRecord#find will fail with a RecordNotFound.

In this case you can bypass loading the record from ActiveRecord and just communicate with the index directly.

With ActiveJob:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch enqueue: :trigger_job do
    attribute :title, :author, :description
  end

  def self.trigger_job(record, remove)
    MyActiveJob.perform_later(record.id, remove)
  end
end

class MyActiveJob < ApplicationJob
  def perform(id, remove)
    if remove
      # The record has likely already been removed from your database so we cannot
      # use ActiveRecord#find to load it.
      # We access the underlying Meilisearch index object.
      Book.index.delete_document(id)
    else
      # The record should be present.
      Book.find(id).index!
    end
  end
end

With Sidekiq:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch enqueue: :trigger_sidekiq_job do
    attribute :title, :author, :description
  end

  def self.trigger_sidekiq_job(record, remove)
    MySidekiqJob.perform_async(record.id, remove)
  end
end

class MySidekiqJob
  def perform(id, remove)
    if remove
      # The record has likely already been removed from your database so we cannot
      # use ActiveRecord#find to load it.
      # We access the underlying Meilisearch index object.
      Book.index.delete_document(id)
    else
      # The record should be present.
      Book.find(id).index!
    end
  end
end

With DelayedJob:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch enqueue: :trigger_delayed_job do
    attribute :title, :author, :description
  end

  def self.trigger_delayed_job(record, remove)
    if remove
      record.delay.remove_from_index!
    else
      record.delay.index!
    end
  end
end

Relations

Extend a change to a related record.

With ActiveRecord, you'll need to use touch and after_touch.

class Author < ActiveRecord::Base
  include MeiliSearch::Rails

  has_many :books
  # If your association uses belongs_to
  # - use `touch: true`
  # - do not define an `after_save` hook
  after_save { books.each(&:touch) }
end

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  belongs_to :author
  after_touch :index!

  meilisearch do
    attribute :title, :description, :publisher
    attribute :author do
      author.name
    end
  end
end

With Sequel, you can use the touch plugin to propagate changes.

# app/models/author.rb
class Author < Sequel::Model
  include MeiliSearch::Rails

  one_to_many :books

  plugin :timestamps
  # Can't use the associations since it won't trigger the after_save
  plugin :touch

  # Define the associations that need to be touched here
  # Less performant, but allows for the after_save hook to be triggered
  def touch_associations
    apps.map(&:touch)
  end

  def touch
    super
    touch_associations
  end
end

# app/models/book.rb
class Book < Sequel::Model
  include MeiliSearch::Rails

  many_to_one :author
  after_touch :index!

  plugin :timestamps
  plugin :touch

  meilisearch do
    attribute :title, :description, :publisher
    attribute :author do
      author.name
    end
  end
end

Sanitize attributes

You can strip all HTML tags from your attributes with the sanitize option.

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch sanitize: true
end

UTF-8 encoding

You can force the UTF-8 encoding of all your attributes using the force_utf8_encoding option.

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch force_utf8_encoding: true
end

Eager loading

You can eager load associations using meilisearch_import scope.

class Author < ActiveRecord::Base
  include MeiliSearch::Rails

  has_many :books

  scope :meilisearch_import, -> { includes(:books) }
end

Manual operations

Indexing & deletion

You can manually index a record by using the index! instance method and remove it by using the remove_from_index! instance method.

book = Book.create!(title: 'The Little Prince', author: 'Antoine de Saint-ExupΓ©ry')
book.index!
book.remove_from_index!
book.destroy!

To reindex all your records, use the reindex! class method:

Book.reindex!

# You can also index a subset of your records
Book.where('updated_at > ?', 10.minutes.ago).reindex!

To delete all your records, use the clear_index! class method:

Book.clear_index!

Access the underlying index object

To access the index object and use the Ruby SDK methods for an index, call the index class method:

index = Book.index
# index.get_settings, index.number_of_documents

Development & testing

Exceptions

You can disable exceptions that could be raised while trying to reach Meilisearch's API by using the raise_on_failure option:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  # Only raise exceptions in development environment.
  meilisearch raise_on_failure: Rails.env.development?
end

Testing

Synchronous testing

You can force indexing and removing to be synchronous by setting the following option:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch synchronous: true
end

🚨 This is only recommended for testing purposes, the gem will call the wait_for_task method that will stop your code execution until the asynchronous task has been processed by MeilSearch.

Disable auto-indexing & auto-removal

You can disable auto-indexing and auto-removing setting the following options:

class Book < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch auto_index: false, auto_remove: false
end

You can temporarily disable auto-indexing using the without_auto_index scope:

Book.without_auto_index do
  # Inside this block, auto indexing task will not run.
  1.upto(10000) { Book.create! attributes }
end

βš™οΈ Development workflow & contributing

Any new contribution is more than welcome in this project!

If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!

πŸ‘ Credits

The provided features and the code base is inspired by algoliasearch-rails.


Meilisearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.

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

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

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

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.

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.

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)

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!

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.

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

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

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.

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

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

Remove useless code

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

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

Add rubocop

Add rubocop (linter):

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

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?

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.

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

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.

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

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?

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.

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?

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.

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

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

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!

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

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.

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

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.