Comments (5)
Yeah, it isn't an issue with meilisearch because if I don't use pagy the in N+1 queries don't happen if I use includes
. Thanks for the help.
from meilisearch-rails.
Hello @coder2000 thanks for using Meilisearch!
I'll need more information regarding how you are using meilisearch-rails
, this will help me to understand your point, but I have some thoughts:
# using the code from gorails screencast https://github.com/gorails-screencasts/meilisearch-rails-example
class Restaurant < ApplicationRecord
has_many :reviews, dependent: :destroy
include MeiliSearch::Rails
meilisearch do
attribute :name
attribute :cuisine
attribute :description
attribute :reviews do
reviews.pluck(:body)
end
filterable_attributes [:cuisine]
sortable_attributes [:created_at, :updated_at]
end
end
And in the controller:
def index
@restaurants = Restaurant.search(params[:query])
end
But if I change the index method to
def index
@restaurants = Restaurant.includes(:reviews).search(params[:query])
end
I will get a log like this:
Anyway, please give me some code and I can help you with :)
from meilisearch-rails.
I understand that, I have
users = User.includes(:store, :point_account).pagy_search(params[:q], filter: filters(params, User::FILTER_FIELDS))
@pagy, @users = pagy_meilisearch(users, items: 30)
but the following is the log
web | Store Load (0.6ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.6ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 1], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | Store Load (1.0ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 31], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.6ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 10], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | Store Load (0.7ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 22], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.5ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 100], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | Store Load (0.8ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 55], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.8ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 101], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | Store Load (0.7ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 81], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.5ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 102], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | CACHE Store Load (0.0ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 55], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.5ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 103], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | Store Load (0.5ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 35], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.5ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 104], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | CACHE Store Load (0.0ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 31], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.5ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 105], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | CACHE Store Load (0.0ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 35], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
web | PointAccount Load (0.4ms) SELECT "point_accounts".* FROM "point_accounts" WHERE "point_accounts"."user_id" = $1 LIMIT $2 [["user_id", 106], ["LIMIT", 1]]
web | ↳ app/components/user/component.html.erb:16:in `call'
web | CACHE Store Load (0.0ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 LIMIT $2 [["id", 55], ["LIMIT", 1]]
web | ↳ app/components/user/badge.html.erb:11:in `block in call'
from meilisearch-rails.
Ok, so I dig into the pagy gem and I found some that could help you:
In order to setup the pagination you should call the pagy_search
or pagy_meilisearch
method right? Like this:
restaurants = Restaurant.pagy_search(params[:query])
@pagy, @restaurants = pagy_meilisearch(restaurants, items: 30)
But if you send your "includes" thing before the pagy_search
call you will lose it, because in the pagy core method the "MeilisearchExtra" has this self
.
def pagy_meilisearch(term = nil, **vars)
[self, term, vars]
end
Since the self
in this context is still the Restaurant
the way I found to workaround that is to add the array by my self:
@pagy, @restaurants = pagy_meilisearch([Restaurant.includes(:reviews), params[:query], {}], items: 30)
And it worked, but I don't know if this is the best usage.
I think you should look here https://ddnexus.github.io/pagy/how-to#custom-count-for-custom-scopes&gsc.tab=0 for more information 😃
from meilisearch-rails.
Based on this ddnexus/pagy#378 (comment) message, I tried and saw that it works for the reported case:
class Restaurant < ApplicationRecord
has_many :reviews, dependent: :destroy
include MeiliSearch::Rails
extend Pagy::Meilisearch
ActiveRecord_Relation.include Pagy::Meilisearch
meilisearch do
attribute :name
attribute :cuisine
attribute :description
attribute :reviews do
reviews.pluck(:body)
end
filterable_attributes [:cuisine]
sortable_attributes [:created_at, :updated_at]
end
end
def index
restaurants = Restaurant.includes(:reviews).pagy_search(params[:query])
@pagy, @restaurants = pagy_meilisearch(restaurants, items: 25)
end
from meilisearch-rails.
Related Issues (20)
- Race condition in async removal after destroy HOT 1
- Use index-swap when reindexing HOT 3
- Is there any way to re-index using a rake task ? HOT 1
- Return task object after ms_index! HOT 1
- Add support for distinct attribute HOT 2
- Add CodeCov
- Skip indexCreation and settingsUpdate whenever adding new record in production HOT 8
- Return all index items when using basic implementation, eager loaded associations and pagy_search method / limit is ignored HOT 5
- Multiple documents for the same ActiveRecord object HOT 1
- Initial index creation and indexing HOT 4
- Check if attributes have changed before index HOT 2
- Redundant Calls to `sleep` in the Test Suite
- Call to non-existant method Rails::Logger.warning (should be warn)
- Use factory_bot to create records in the test suite
- Indexes not cleaned up after test suite
- Refactor the test suite
- Refactor the code base
- Meilisearch is not indexing records in a Rails model HOT 4
- All CI tests with Rails 7 fail HOT 1
- Meilisearch Ruby now expects snake_cased attributes instead of camelCase. HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meilisearch-rails.