ruby on rails - Unable to filter out results based on "missing" field in elasticsearch -


i using tire-0.4.2 interact elasticsearch in rails application (uses mongodb database , mongoid interacting mongdb). have post model has spam document embedded in it.

post.rb

include mongoid::document include mongoid::timestamps   .. embeds 1 :spam, as: :spammable ... 

spam.rb

include mongoid::document include mongoid::timestamps embedded_in :spammable, polymorphic: true field :needs_approval, type: boolean, default: false field :is_spam, type: time  has_and_belongs_to_many :request_spam_by, :class_name => "user"  field :request_spam, type: boolean, default: false 

i want posts have no spam document: here tire query

post.tire.search(:load => :true, page: self.page, per_page: post::per_page) |pf|     pf.query{ |query| query.string self.search_text } unless search_text.blank?     pf.filter(:missing, :field => 'spam')     pf.filter(:term, :college_id => self.college.id)     pf.filter(:term, :user_id => self.user.id)     pf.filter(:missing, :field => 'spam' )     pf.filter(:terms, :user_type => self.user_type) unless self.user_type.blank?     pf.filter(:range, :created_at => {:gte => self.from_time}) unless self.from_time.blank?     pf.filter(:range, :created_at => {:lte => self.to_time}) unless self.to_time.blank?     pf.sort{|s| s.by :updated_at, self.sort_order} end 

generated elasticsearch query:

curl -x "http://localhost:9200/development_posts/post/_search?     from=0&load=true&page=1&per_page=10&size=10&pretty=true" -d '{"sort":[{"updated_at":"desc"}],"filter":{"and":[{"missing":{"field":"spam"}},{"term":{"college_id":"4fb424a5addf32296f00013a"}},{"missing":{"field":"spam"}},{"range":{"created_at":{"gte":"2012-06-05t00:00:00+05:30"}}},{"range":{"created_at":{"lte":"2012-06-05t23:59:59+05:30"}}}]},"size":10,"from":0}' 

the results of query gives me documents in spam exists though searching documents have spam document missing. don't know mistake doing. can point me in right direction?

you can't use missing on object level actual fields indexed.

if there field present in spam object set missing filter on "spam.always_there" isn't quite nice should work

 pf.filter(:or, [     {:missing => { :field => 'spam.needs_approval'}},     {:term => {'spam.needs_approval' => false}}]) 

should select documents field either false or missing (if remember correctly default null , missing same thing watch out there)


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -