Ask A Question

Notifications

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

Reply

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.

Reply

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

Reply

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

Reply

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

Reply

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
Reply

Hey Chris,

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

Reply

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

Reply

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

Reply

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

Reply

Aweosme! :D

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.