Giter VIP home page Giter VIP logo

Comments (5)

coder2000 avatar coder2000 commented on May 19, 2024 1

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.

brunoocasali avatar brunoocasali commented on May 19, 2024

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

I get this log:
image

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:

image

Anyway, please give me some code and I can help you with :)

from meilisearch-rails.

coder2000 avatar coder2000 commented on May 19, 2024

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.

brunoocasali avatar brunoocasali commented on May 19, 2024

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.

brunoocasali avatar brunoocasali commented on May 19, 2024

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)

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.