ruby-on-rails - 耐嚼只按ID搜索

标签 ruby-on-rails elasticsearch mongoid

我正在使用chewy

除了id之外,我无法使用任何op字段来查找“ops”。

型号:

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

Controller :
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' }

关于ruby-on-rails - 耐嚼只按ID搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728858/

相关文章:

ruby-on-rails - 如何从 Mongoid 模型中删除属性,即不仅仅是取消它们的值

mongoid - 使用Mongoid查看MongoDB中的现有索引

ruby-on-rails - 如何将 worker 分为工作延迟+ heroku的工作池?

ruby-on-rails - Rails - 常量哈希?

java - "V is not a functional interface"错误

elasticsearch - 如何在嵌入式ElasticSearch中记录所有查询?

node.js - 索引新文档并在同一查询中获取索引文档

ruby-on-rails - RubyMine:在项目中找不到 Rails 服务器启动器

ruby-on-rails - 随机 0 被插入到 Rails 中的编码数组查询参数中

ruby - 使用elasticsearch过滤带有空格的标签