耐嚼只能通过id搜索

时间:2015-04-19 10:53:00

标签: ruby-on-rails elasticsearch mongoid

我正在使用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"]}

怎么了?

2 个答案:

答案 0 :(得分:0)

狂野猜测,但下面的查询怎么样?

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

答案 1 :(得分:0)

<强>解决!定义中的错误多余字段_id。

索引:(耐嚼/ 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