Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8066879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:07:40+00:00 2026-06-05T12:07:40+00:00

I am using tire-0.4.2 to interact with elasticsearch in my rails application (uses mongodb

  • 0

I am using tire-0.4.2 to interact with elasticsearch in my rails application (uses mongodb database and mongoid for interacting with mongdb). I have a post model which has a spam document embedded in it.

post.rb

include Mongoid::Document
include Mongoid::Timestamps 

..
embeds one :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 to get all the posts which have no spam document: Here is the tire query

Post.tire.search(:load => :true, page: self.page, per_page: Post::PER_PAGE) do |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 GET "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 the query gives me the documents in which spam exists even though I am searching only for documents which have spam document missing. I don’t know what is the mistake I am doing. Can anybody point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:07:41+00:00Added an answer on June 5, 2026 at 12:07 pm

    You can’t use the missing on an object level only actual fields that get indexed.

    If there is a field that is always present in a spam object then you could set the missing filter on “spam.always_there” which isn’t quite as nice but should work

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

    Should select documents where that field is either false or missing (if I remember correctly by default null and missing are the same thing so watch out there)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Elasticsearch with Rails 3 and tire gem. I have got facets to work
I have an application that is using both the carrierwave & tire gems. The
I'm using Tire for Elastic Search. In my application I have 2 models; Price
I have a few very specific questions about Rails + Tire + ElasticSearch. I
Do you have any suggestions how to perform authorization of elasticsearch results (using tire)
I have an existing Rails app that uses tire (0.4.0) to interface with an
I’m working on an application which has been designed using n-tire application architecture .The
I'm having trouble getting Tire working using ElasticSearch with the Bonsai addon on the
I am using Tire in ruby on rails to connect with ElastiSearch. When I
Using MVC2 I have an AJAX form which is posting to a bound model.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.