只能通过 id 搜索

chewy searching only by id

我正在使用 chewy

我无法使用 op 的任何字段找到 'ops',id 除外。

型号:

class Op
  include Mongoid::Document
  ...
  state_machine :initial => :draft do
  ...
  end
  update_index 'ops#op', :self
end

索引:

class OpsIndex < Chewy::Index
  define_type Op
end

控制器:

def index
  OpsIndex.reset! # => true
  OpsIndex.purge  # => {\"acknowledged\"=>true}
  OpsIndex::Op.import # => true

  scope = OpsIndex::Op.query term: { _id: '55263b48336f63004a000000' }
  scope.total_count # => 1 nice!
  scope.to_a.inspect => #<OpsIndex::Op:0x00000006f5f310 @attributes={\"_id\"=>{\"$oid\"=>\"55263b48336f63004a000000\"}, \"state\"=>\"deactivated\" ...

  #But
  scope = OpsIndex::Op.query term: { state: 'deactivated' }
  scope.total_count # => 0
end

在development.log:

[1m[32mOpsIndex::Op Search (7.4ms)[0m {:body=>{:query=>{:term=>{:_id=>"55263b48336f63004a000000"}}}, :index=>["development_ops"], :type=>["op"]}
[1m[32mOpsIndex::Op Search (3.2ms)[0m {:body=>{:query=>{:term=>{:state=>"deactivated"}}}, :index=>["development_ops"], :type=>["op"]}

怎么了?

乱猜,但下面的查询怎么样?

scope = OpsIndex::Op.query match: { state: 'deactivated' }

已解决!错误定义excess字段_id.

索引: (chewy/ops_index.rb)

define_type Op do
    # field :_id #don't specify it!
    field :created_at, :updated_at, :postponed, type: 'integer', index: :not_analyzed
    field :state
    field :name
    field :description
  end