2015-04-19 55 views
2

我正在使用chewy耐嚼只搜索ID

我找不到使用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"]} 

有什麼不對?

回答

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 
0

瘋狂的猜測,但如何查詢以下內容?

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

**同**但是,工作:** OpsIndex :: Op.query匹配:{_id:「55263b48336f63004a000000 '}和 OpsIndex :: Op.filter(match_all:{}) **並且不工作:** OpsIndex :: Op.query(match:{name:'some text'})和OpsIndex :: Op.filter(範圍:{created_at:{gte:100}}) – user26171

+0

是否分析了字段「名稱」?然後你的值爲'一些文本'的查詢將不起作用。請參閱http://stackoverflow.com/questions/18598418/elastic-search-exact-match – Zouzias

+0

好的,我會檢查分析的選項名稱。但如何按範圍過濾? – user26171