README.md in meilisearch-rails-0.1.0 vs README.md in meilisearch-rails-0.2.0
- old
+ new
@@ -124,10 +124,12 @@
# all attributes will be sent to MeiliSearch if block is left empty
end
end
```
+⚠️ Note that even if you want to use all the default options, you must declare an empty `meilisearch` block in your model.
+
#### Basic Backend Search <!-- omit in toc -->
We **strongly recommend the use of front-end search** through our [JavaScript API Client](https://github.com/meilisearch/meilisearch-js/) or [Instant Meilisearch plugin](https://github.com/meilisearch/instant-meilisearch)
Search returns ORM-compliant objects reloaded from your database.
@@ -141,19 +143,22 @@
end
```
#### Backend Pagination <!-- omit in toc -->
-We support both [kaminari](https://github.com/amatsuda/kaminari) and [will_paginate](https://github.com/mislav/will_paginate).
+This gem supports:
+- [kaminari](https://github.com/amatsuda/kaminari)
+- [pagy](https://github.com/ddnexus/pagy)
+- [will_paginate](https://github.com/mislav/will_paginate).
Specify the `:pagination_backend` in the configuration file:
```ruby
MeiliSearch.configuration = {
- meilisearch_host: 'YourMeiliSearchHost',
- meilisearch_api_key: 'YourMeiliSearchAPIKey',
- pagination_backend: :kaminari #:will_paginate
+ meilisearch_host: 'YourMeiliSearchHost',
+ meilisearch_api_key: 'YourMeiliSearchAPIKey',
+ pagination_backend: :kaminari #:will_paginate
}
```
Then, as soon as you use the `search` method, the returning results will be paginated:
@@ -185,26 +190,27 @@
```ruby
class Book < ApplicationRecord
include MeiliSearch
meilisearch do
- searchableAttributes ['title', 'author', 'publisher', 'description']
- attributesForFaceting ['genre']
- rankingRules [
- "proximity",
- "typo",
- "words",
- "attribute",
- "wordsPosition",
- "exactness",
- "desc(publication_year)"
+ searchable_attributes [:title, :author, :publisher, :description]
+ attributes_for_faceting [:genre]
+ ranking_rules [
+ 'proximity',
+ 'typo',
+ 'words',
+ 'attribute',
+ 'wordsPosition',
+ 'exactness',
+ 'desc(publication_year)'
]
synonyms nyc: ['new york']
- // The following parameters are applied when calling the search() method:
- attributesToHighlight ['*']
- attributesToCrop ['description']
- cropLength 10
+
+ # The following parameters are applied when calling the search() method:
+ attributes_to_highlight ['*']
+ attributes_to_crop [:description]
+ crop_length 10
end
end
```
Check the dedicated section of the documentation, for more information on the [settings](https://docs.meilisearch.com/reference/features/settings.html).
@@ -212,26 +218,28 @@
## 🔍 Custom search
All the supported options are described in the [search parameters](https://docs.meilisearch.com/reference/features/search_parameters.html) section of the documentation.
```ruby
-Book.search('Harry', { filters: 'author = J. K. Rowling' })
+Book.search('Harry', filters: 'author = J. K. Rowling')
```
-👉 Don't forget that `attributesToHighlight`, `attributesToCrop`, and `cropLength` can be set up in the `meilisearch` block of your model.
+👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
+`crop_length` can be set up in the `meilisearch` block of your model.
## 🪛 Options
### MeiliSearch configuration & environment
#### 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.
+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.
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
- meilisearch :index_uid => 'MyCustomUID' do
+
+ meilisearch index_uid: 'MyCustomUID' do
end
end
```
#### Index UID according to the environment
@@ -239,10 +247,11 @@
You can suffix the index UID with the current Rails environment using the following option:
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
+
meilisearch per_environment: true do # The index UID will be "Book_#{Rails.env}"
end
end
```
@@ -280,31 +289,32 @@
end
```
#### Custom primary key
-By default, the `primary key` is based on your record's id. You can change this behavior specifying the `:primary_key` option.
+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 have a **unique value**.
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
- meilisearch :primary_key => 'ISBN' do
+
+ meilisearch primary_key: 'ISBN' do
end
end
```
#### Conditional indexing
-You can control if a record must be indexed by using the `:if` or `:unless` options.<br>
+You can control if a record must be indexed by using the `if:` or `unless:` options.<br>
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.
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
- meilisearch :if published?, :unless premium? do
+ meilisearch if: :published?, unless: :premium? do
end
def published?
# [...]
end
@@ -312,39 +322,38 @@
def premium?
# [...]
end
def will_save_change_to_published?
- # return true only if you know that the 'published' state changed
+ # 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:
```ruby
class Book < ActiveRecord::Base
-
include MeiliSearch
PUBLIC_INDEX_UID = 'Books'
SECURED_INDEX_UID = 'PrivateBooks'
# store all books in index 'SECURED_INDEX_UID'
meilisearch index_uid: SECURED_INDEX_UID do
- searchableAttributes [:title, :author]
+ 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
- searchableAttributes [:title, :author]
+ searchable_attributes [:title, :author]
end
end
private
def public?
- released && !premium
+ released? && !premium?
end
end
```
#### Share a single index
@@ -392,22 +401,35 @@
🤔 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**:
+
```ruby
+class Book < ActiveRecord::Base
+ include MeiliSearch
+
+ 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
+ # 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
- c = Book.find(id)
- c.index!
+ # The record should be present.
+ Book.find(id).index!
end
end
end
```
@@ -427,18 +449,17 @@
end
class MySidekiqWorker
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
- index = Book.index.delete_document(id)
+ # 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
- c = Contact.find(id)
- c.index!
+ # The record should be present.
+ Book.find(id).index!
end
end
end
```
@@ -464,11 +485,11 @@
#### Relations
Extend a change to a related record.
-**With Active Record**, you'll need to use `touch` and `after_touch`.
+**With ActiveRecord**, you'll need to use `touch` and `after_touch`.
```ruby
class Author < ActiveRecord::Base
include MeiliSearch
@@ -544,11 +565,11 @@
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
- meilisearch :sanitize => true do
+ meilisearch sanitize: true do
end
end
```
#### UTF-8 encoding
@@ -557,20 +578,20 @@
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
- meilisearch :force_utf8_encoding => true do
+ meilisearch force_utf8_encoding: true do
end
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
+You can manually index a record by using the `index!` instance method and remove it by using the `remove_from_index!` instance method.
```ruby
book = Book.create!(title: 'The Little Prince', author: 'Antoine de Saint-Exupéry')
book.index!
book.remove_from_index!
@@ -601,27 +622,28 @@
# 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:
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
- # only raise exceptions in development environment
- meilisearch :raise_on_failure => Rails.env.development? do
+ # Only raise exceptions in development environment.
+ meilisearch raise_on_failure: Rails.env.development? do
end
end
```
#### Testing
+
##### Synchronous testing
+
You can force indexing and removing to be synchronous by setting the following option:
```ruby
class Book < ActiveRecord::Base
include MeiliSearch
@@ -647,10 +669,11 @@
You can temporarily disable auto-indexing using the without_auto_index scope:
```ruby
Book.without_auto_index do
- 1.upto(10000) { Book.create! attributes } # inside this block, auto indexing task will not run.
+ # Inside this block, auto indexing task will not run.
+ 1.upto(10000) { Book.create! attributes }
end
```
## ⚙️ Development workflow & contributing