Commit 87774303bad05c1636fd4658ee3fcd3339d9343f
1 parent
dda43093
Exists in
master
and in
1 other branch
Improved performant conversions example [skip ci]
Showing
1 changed file
with
16 additions
and
6 deletions
Show diff stats
README.md
@@ -1602,10 +1602,12 @@ add_column :products, :search_conversions, :jsonb | @@ -1602,10 +1602,12 @@ add_column :products, :search_conversions, :jsonb | ||
1602 | 1602 | ||
1603 | For MySQL, use `:json`, and for others, use `:text` with a [JSON serializer](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html). | 1603 | For MySQL, use `:json`, and for others, use `:text` with a [JSON serializer](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html). |
1604 | 1604 | ||
1605 | -Create a separate method so you can use partial reindexing. | 1605 | +Next, update your model, Create a separate method for conversion data so you can use [partial reindexing](#partial-reindexing). |
1606 | 1606 | ||
1607 | ```ruby | 1607 | ```ruby |
1608 | class Product < ApplicationRecord | 1608 | class Product < ApplicationRecord |
1609 | + searchkick conversions: [:conversions] | ||
1610 | + | ||
1609 | def search_data | 1611 | def search_data |
1610 | { | 1612 | { |
1611 | name: name | 1613 | name: name |
@@ -1620,11 +1622,13 @@ class Product < ApplicationRecord | @@ -1620,11 +1622,13 @@ class Product < ApplicationRecord | ||
1620 | end | 1622 | end |
1621 | ``` | 1623 | ``` |
1622 | 1624 | ||
1623 | -Create a job to update the column and reindex records with new conversions. | 1625 | +Deploy and reindex your data. For zero downtime deployment, temporarily set `conversions: false` in your search calls until the data is reindexed. |
1626 | + | ||
1627 | +To populate conversions, create a job to update the column and reindex records with new conversions. | ||
1624 | 1628 | ||
1625 | ```ruby | 1629 | ```ruby |
1626 | class ReindexConversionsJob < ApplicationJob | 1630 | class ReindexConversionsJob < ApplicationJob |
1627 | - def perform(class_name, since) | 1631 | + def perform(class_name, since: nil, reindex: true) |
1628 | # get records that have a recent conversion | 1632 | # get records that have a recent conversion |
1629 | recently_converted_ids = | 1633 | recently_converted_ids = |
1630 | Searchjoy::Conversion.where(convertable_type: class_name).where(created_at: since..) | 1634 | Searchjoy::Conversion.where(convertable_type: class_name).where(created_at: since..) |
@@ -1652,8 +1656,8 @@ class ReindexConversionsJob < ApplicationJob | @@ -1652,8 +1656,8 @@ class ReindexConversionsJob < ApplicationJob | ||
1652 | end | 1656 | end |
1653 | end | 1657 | end |
1654 | 1658 | ||
1655 | - # partial reindex | ||
1656 | - model.where(id: ids).reindex(:conversions_data) | 1659 | + # reindex conversions data |
1660 | + model.where(id: ids).reindex(:conversions_data) if reindex | ||
1657 | end | 1661 | end |
1658 | end | 1662 | end |
1659 | end | 1663 | end |
@@ -1662,7 +1666,13 @@ end | @@ -1662,7 +1666,13 @@ end | ||
1662 | Run the job with: | 1666 | Run the job with: |
1663 | 1667 | ||
1664 | ```ruby | 1668 | ```ruby |
1665 | -ReindexConversionsJob.perform_later("Product", 1.day.ago) | 1669 | +ReindexConversionsJob.perform_later("Product") |
1670 | +``` | ||
1671 | + | ||
1672 | +And set it up to run daily. | ||
1673 | + | ||
1674 | +```ruby | ||
1675 | +ReindexConversionsJob.perform_later("Product", since: 1.day.ago) | ||
1666 | ``` | 1676 | ``` |
1667 | 1677 | ||
1668 | ## Advanced | 1678 | ## Advanced |