0

タイヤの宝石を使用してソートされた検索を実行しようとしていますが、常に次のエラーが発生します。

解析の失敗 [ソートするための [職業] のマッピングが見つかりません]]

私のコードの何が問題になっていますか? 以下は私のユーザーモデルです

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Tire::Model::Search
  include Tire::Model::Callbacks

  belongs_to :occupation

  field :name, :type => String, :default => ""
  field :email, :type => String, :default => ""
  field :kind, :type => String, :default => ""

  def to_indexed_json
    { id: id,
      name: name,
      email: email,
      kind: kind,
      occupation: occupation.try(:name),
      created_at: created_at,
      updated_at: updated_at
    }.to_json
  end

  mapping do
    indexes :id, :type => 'integer'
    indexes :name, :type => 'String'
    indexes :email, :type => 'String'
    indexes :kind, :type => 'String'
    indexes :occupation, :type => 'String'
    indexes :created_at, :type => 'Date'
    indexes :updated_at, :type => 'Date'
  end

  def self.search q, params={}
    tire.search page: (params[:page] || 1)  do |search|
      search.query do |query|
        query.string q
      end
      search.sort do |sort|
        sort_column = params[:sort] || 'occupation'
        sort_direction = params[:direction] || 'asc'
        sort.by(sort_column.to_sym, sort_direction)
      end
    end
  end
end
4

1 に答える 1

0

これを試すことができますか?職業.name を to_indexed_json に送信していて、職業モデル全体を並べ替えようとしていると思います。

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Tire::Model::Search
  include Tire::Model::Callbacks

  belongs_to :occupation

  field :name, :type => String, :default => ""
  field :email, :type => String, :default => ""
  field :kind, :type => String, :default => ""

  def to_indexed_json
    { id: id,
      name: name,
      email: email,
      kind: kind,
      occupation: occupation.try(:name),
      created_at: created_at,
      updated_at: updated_at
    }.to_json
  end

  mapping do
    indexes :id, :type => 'integer'
    indexes :name, :type => 'String'
    indexes :email, :type => 'String'
    indexes :kind, :type => 'String'
    indexes :occupation, :type => 'object' do
        indexes :name, :type => 'String'
    end
    indexes :created_at, :type => 'Date'
    indexes :updated_at, :type => 'Date'
  end

  def self.search q, params={}
    tire.search page: (params[:page] || 1)  do |search|
      search.query do |query|
        query.string q
      end
      search.sort do |sort|
        sort_column = params[:sort] || 'occupation.name'
        sort_direction = params[:direction] || 'asc'
        sort.by(sort_column, sort_direction)
      end
    end
  end
end
于 2013-01-11T08:05:42.480 に答える