2012-09-10 74 views
0

對於輪胎(ElasticSearch包裝寶石),如何查詢和篩選具有某個屬性的nil/null值的索引記錄。舉例來說,如果我有這樣的關係輪胎(彈性搜索)無比較

class Article < ActiveRecord::Base 
    belongs_to :topic 
end 

我的文章索引,但我希望避免與topic_id =零拉回記錄。我試過這個代碼打擊,但沒有奏效。

class Article 
    belongs_to :topic 

    def search(q) 
    tire.search do 
     ... 
     filter :missing, :field => :topic_id, { :existence => false, :null_value => false } 
     ... 
     ### filter :range, :topic_id => { :gt => 0 } # ==> I tried this as well but didn't work 
     ... 
    end 
    end 
end 

回答

-3

@articles = Article.where( 'topic_id是不是?',無)

+0

對不起,但這是用於輪胎寶石DSL(ElasticSearch包裝)。不是ActiveRecord。 – RubyFanatic

2

我認爲你應該使用exists filter如果你想只過濾與現有topic_id價值的文件。

class Article 
    belongs_to :topic 

    def search(q) 
    tire.search do 
     ... 
     filter :exists, :field => :topic_id 
     ... 
    end 
    end 
end 

或者你可以使用query string_exists_:topic_id查詢。

1

請嘗試:

class Article 
    belongs_to :topic 

    def search(q) 
    tire.search do 
     ... 
     filter :not, :missing => {:field => :topic_id, { :null_value => true } } 
     ... 
    end 
    end 
end 

這應該工作。如果你想檢查該字段是否存在,則需要存在。我想你只需要null_value檢查。

+0

您不能將null_value和exists都設置爲false。至少有一個或兩個都應該設置爲true。 –

相關問題