2

I'm using chewy.

I can't find 'ops' using any fields of op, except id.

Model:

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

Index:

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

In 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"]}

What's wrong?

user26171
  • 21
  • 4

2 Answers2

0

Wild guess, but how about the following query?

scope = OpsIndex::Op.query match: { state: 'deactivated' }
Zouzias
  • 2,330
  • 1
  • 22
  • 32
  • **The same.** **But, work:** OpsIndex::Op.query match: { _id: '55263b48336f63004a000000' } and OpsIndex::Op.filter(match_all: {}) **And, don't work:** OpsIndex::Op.query(match: { name: 'some text' }) and OpsIndex::Op.filter(range: { created_at: { gte: 100 } }) – user26171 Apr 20 '15 at 04:30
  • Is the field "name" analyzed? Then your query with value 'some text' will not work. See also http://stackoverflow.com/questions/18598418/elastic-search-exact-match – Zouzias Apr 20 '15 at 09:35
  • All right, I'll check analyzed option for name. But how about filter by range? – user26171 Apr 20 '15 at 10:51
0

Solved! The fault in definition excess field _id.

Index: (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
user26171
  • 21
  • 4