searchkick on state_id筛选器不工作

wtzytmuj  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(240)

我正在尝试更新我的searchkick查询以根据状态id进行筛选,但失败了。我尝试使用ElasticSearch路由并将其添加到搜索字段中。

路由尝试

app/models/content.rb

class Content < ApplicationRecord
  include HashtaggableItem
  include PgSearch::Model

  searchkick word_start: %i[body title state state_slug user], callbacks: :async, routing: true

  acts_as_paranoid

  belongs_to :user
  delegate :full_name, to: :user, allow_nil: true
  belongs_to :shared_link, optional: true
  belongs_to :state, foreign_key: :state_id
  delegate :name, :abbr, :slug, to: :state, prefix: true, allow_nil: true
  has_many :upvotes, as: :upvotable, dependent: :destroy_async, counter_cache: true
  has_many :comments, as: :commentable, dependent: :destroy_async, counter_cache: true

  validates :body, presence: true

  enum content_type: { question: 0, discussion: 1, bill: 2 }
  delegate :display_name, :full_name, :title, to: :user, prefix: true

  def search_routing
    state_id
  end

  def search_data
    {
      upvotes_count: upvotes_count,
      body: body,
      title: title,
      state: state_name,
      state_slug: state_slug,
      user: user_full_name,
      created_at: created_at,
      updated_at: updated_at
    }
  end
end

重新索引错误

routing required for <index_name>

添加到搜索字段

app/models/contents.rby

class Content < ApplicationRecord
  include HashtaggableItem
  include PgSearch::Model

  searchkick word_start: %i[body title state state_slug user], callbacks: :async

  acts_as_paranoid

  belongs_to :user
  delegate :full_name, to: :user, allow_nil: true
  belongs_to :shared_link, optional: true
  belongs_to :state, foreign_key: :state_id
  delegate :name, :abbr, :slug, to: :state, prefix: true, allow_nil: true
  has_many :upvotes, as: :upvotable, dependent: :destroy_async, counter_cache: true
  has_many :comments, as: :commentable, dependent: :destroy_async, counter_cache: true

  validates :body, presence: true

  enum content_type: { question: 0, discussion: 1, bill: 2 }
  delegate :display_name, :full_name, :title, to: :user, prefix: true

  def search_data
    {
      upvotes_count: upvotes_count,
      body: body,
      title: title,
      state: state_name,
      state_slug: state_slug,
      user: user_full_name,
      created_at: created_at,
      updated_at: updated_at,
      state_id: state_id
    }
  end
end

查找匹配项

Searchkick.search('*',
                      models: search_models,
                      where: { updated_at: { gt: since } },
                      routing: filters[:state_id],
                      order: sorting,
                      boost_by_recency: { created_at: { scale: '7d', decay: 0.5 } },
                      page: params[:page], per_page: 10)

没有火柴

Searchkick.search('*',
                      models: search_models,
                      where: { updated_at: { gt: since } },
                      routing: filters[:state_id],
                      order: sorting,
                      boost_by_recency: { created_at: { scale: '7d', decay: 0.5 } },
                      page: params[:page], per_page: 10)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题