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 9226231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:46:48+00:00 2026-06-18T04:46:48+00:00

I am using rails and for search I am using Tire and elasticsearch. I

  • 0

I am using rails and for search I am using Tire and elasticsearch. I have a string type field which in some records have value and in some records is nil.

I’d like to sort and show last, all the records that have null value in this field. As I see in this issue https://github.com/elasticsearch/elasticsearch/issues/896 in the current version this can’t be possible through sort and elasticsearch.

Is there a workaround with rails? I am trying to do it using two searches and using filters like the following example:

    filter :not, :missing => { :field => :video_url } if params[:video].present?
    filter :missing, { :field => :video_url } if params[:video].blank?

But it didn’t work (I can’t understand why until now, I’ll continue debugging).

Another idea is to create two methods with the specific fields. Any other solution/idea?

Update 2/2/2013

I finally did it like the following:

    if video == "yes"
      filter :not, :missing => { :field => :video_url }
    elsif video == "no"
      filter :missing, { :field => :video_url }
    end

And I am passing the video parameter by my own. I am sorting and boosting the search but additionally I want all the objects that hasn’t got video_url field, to appear at the bottom no matter how relevant they are. Indeed I don’t need to sort by this field, just to show last the nil value fields.

So to solve this I am calling two times the search and with the addition of the code above, it works like a charm.

Just for completeness, my search method is the following:

  def self.search(params, video = nil)
tire.search do
  query do
    boolean do
      must { string params[:query], default_operator: "AND" } if params[:query].present?
      must { term :active, true }
    end
  end
    sort { by :update_ad => "desc" } unless params[:query].present?
    facet "categories" do
      terms :category_id
    end
    if video == "yes"
      filter :not, :missing => { :field => :video_url }
    elsif video == "no"
      filter :missing, { :field => :video_url }
    end
end

end

If you don’t pass the video param, it won’t apply any filter. In my mapping, I have set the boost, analyzers etc.

Thank you

  • 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-18T04:46:49+00:00Added an answer on June 18, 2026 at 4:46 am

    First, the Elasticsearch issue you’re linking to is still open and is only a feature suggestion.

    Second, just as a note, are you really sure you want to sort as opposed to boost the score of certain records?

    Third, if you indeed do want to sort on this field, the easiest way is to just index the field with some value which comes last ("ZZZ", weird Unicode chars, you get the picture). You probably don’t want to do this by default, so it’s a good idea to use the multi_field feature. Of course, you have to reindex your corpus to pick up the new settings.

    Lastly, it is possible to sort by a script (see documentation), but it has the usual and obvious performance impact.

    • 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
Hi I have been using the normal rails active record LIKE search in my
I'm a RoR beginner and am using Rails 3.2.3. I have a search form
I am using Tire and elasticsearch to provide search functionality on a MongoMapper model,
I have a few very specific questions about Rails + Tire + ElasticSearch. I
I have an app using Rails 3.0.6 which I run on two machines. I
I'm using Backbone with Rails (3.1). I have a search page. The first search
I am using tire-0.4.2 to interact with elasticsearch in my rails application (uses mongodb
I'm using Rails and I have a hash object. I want to search the
I am using Rails 3.1.0 and I would like to get a particular value

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.