Skip to main content

Elastic search & search kick incorporating dates fields and integers

General • Asked by John Munyi

HI Chris,

I implemented elastic search using search kick gem and all went well from search, autocomplete and suggestions when dealing with text.

Now i wanna perform a similar search on table that all columns are dates and integers is it possible .. i have tried tinkering with it but its yielding nothing, any ideas


You should just be able to pass over Date objects to handle that accordingly. Integers should also be handled the same way. Normally dates and times get converted to seconds since the epoch, so you may observe that change getting automatically converted in the logs when it queries. I don't think there should be anything special.


ok cool..... will check again what i am missing ... or should exact match be more efficient ?


What do you mean by exact match? If you've got some code examples, we can probably help out a bit better. :)


what i mean is if i am searching for a record dated .. "2016-03-04" and enter "2016-03-05" i still end up with the same results, you know how elastic search corrects the search for you ?

so i was wondering how i can lock the search to an exact match


Hmm, that's interesting. I just tried it and it's not doing that for me. There's a user with a birthday in the system as Jan 5, 1980 and it doesn't seem to find nearby results for me.

irb(main):009:0> User.search(where: {birthday: Date.new(1989, 5, 26)}).count
  User Search (2.4ms)  curl http://localhost:9200/users_development/_search?pretty -d '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":[{"term":{"birthday":"1980-01-6"}}]}}},"size":1000,"from":0,"fields":[]}'
=> 0

irb(main):010:0> User.search(where: {birthday: Date.new(1989, 5, 25)}).count
  User Search (2.9ms)  curl http://localhost:9200/users_development/_search?pretty -d '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":[{"term":{"birthday":"1980-01-5"}}]}}},"size":1000,"from":0,"fields":[]}'
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (2, 1)
=> 1

Hey Chris,

My syntax had an issue i wasn’t using where, thanks man all well now


HI Chris , I have hit another block while in my controller i am doing this

        @weekly_performance_reviews = current_admin.weekly_performance_reviews.search('*', where: {  week_start:                      

         Date.new(params[:query], '%Y/%m/%d') })

I am getting an error " no implicit conversion of String into Integer"

cheers man


I believe you can't pass a string in there like that to the new method. It only accepts integers http://ruby-doc.org/stdlib-2.3.0/libdoc/date/rdoc/Date.html#method-c-new


Hi Chris finally made it work with q = Date.strptime(params[:query], '%Y-%m-%d') unless params[:query].blank?

then just passing q to the where clause in the controller .
Thanks Man



Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.