Ask A Question


You’re not receiving notifications from this thread.

Elastic search & search kick incorporating dates fields and integers

John Munyi asked in General

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> {birthday:, 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> {birthday:, 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 ='*', where: {  week_start:                      
[: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


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


Aweosme! :D

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.