Ask A Question

Notifications

You’re not receiving notifications from this thread.

Format of created_at for date range using Searchkick

RJ McCollam asked in Rails

I am using searchkick to build out a searchform that has a couple different parts to it and searches across multiple models. I am specifically trying to get a date range setup so the user can search for results within a start and end date.

Here is how I am building my search query in my controller:

def index
        @page_title = "Search"

        query = params[:q] ? params[:q] : "*"
        args = {}
        args[:client] = params[:client] if params[:client].present?
        args[:created_at] = {}
        args[:created_at][:gte] = "2018-10-01"
        args[:created_at][:lte] = "2018-11-30"
        @results = Searchkick.search(query, where: args, order: {created_at: :desc}, aggs: { client: {} }, operator: "or")
    end

In passing in a hard coded value for a start and end date it does exactly what I want it to do.

When I attempt to pass these values in from the form in the same format I get a parse error.

I did go through and set the search_data up for each model in order to convert created_at to a time using created_at: created_at.to_time, but in indexing that data I ran into issues where my other search functions weren't working since my models don't all share the same data.

Am I missing the boat with my current implementation completely, or is there some formatting I can do in order to get this date range working?

Reply

Can you post the error? I'm pretty sure it tells you which portion of the params was invalid.

It's been a while since I've done this, but I remember having to specify "range" or something.

Reply

Not sure if the last comment came through or not. Not seeing it on my end, but post count for this thread went up.

Reply

It probably got flagged as spam cuz it was huge.

Important part of the error was this:

[400] {"error":{"root_cause":[{"type":"parse_exception","reason":"failed to parse date field []] with format [strict_date_optional_time||epoch_millis]"},

It looks like one of the date fields might not have been passed through.

Have you logged out your args var before it gets sent to Searchkick? I'm curious to see what that looks like. Usually you only want to include the field if it was present, and I don't see checks on that in your code for the date range fields. Maybe that is the issue.

Reply

In this particular instance to just test if it is working I am always making sure that those fields have values in them.

Reply

Okay yeah. :)

I wonder if you need to specify range before the attribute like it shows in the docs. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

What happens if you add debug: true to your query?

Product.search("soap", debug: true)
Reply

Also I found this when searching the error:

https://stackoverflow.com/questions/48749364/how-to-fix-the-error-failed-to-parse-date-field-in-elasticsearch

Which points out to me that you may be passing in data wrong. It appears to put the value it cannot parse inside the [] here...and that makes it seem like your value is just a ].

failed to parse date field []]

Have you double checked to make sure that args is printing out everything correctly? I still feel like that's the source of your problem.

Reply

When I get the error and output the args I get what I think I am suppose to be getting {:client=>"2", :created_at=>{:gte=>["2018-11-01"], :lte=>["2018-11-30"]}}

Reply

I think the issue might be in the format that the params are being sent through as.

If I set up simple variables for params like

client = params[:client]
start_date = params[:start_date]
end_date = params[:end_date]

then the output I get is

>>  client
=> "2"
>>  start_date
=> ["2018-11-01"]
>>  end_date
=> ["2018-11-30"]

So the dates are being wrapped as an array for some reason. Which is proably why that extra ] is what you were thinking could be the problem. Guess I need to fix that before I can go any further.

Reply

This whole thread goes to show my greeness in rails. The way that I was setting things up in my controller was fine. The issue was the way I had my date_field setup in my view. It was giving me the value in an array which was the cause of the error. Corrected my field setup to return just plain text and all is well with the world.

In all seriousness Chris you need to put a tip button on this site cause the attention you gave, especially considering the holiday, is more than appreciated. Yes I pay monthly for access, but this got me over a hump to meat a deadline. Thank you.

Reply

Ah ha! That was a weird one to track down! Glad you got it fixed.

What did you do to fix your date field? I imagine someone might find your solution helpful as well in case they stumble upon this.

A tip button is a fun idea. Paypal's got something kinda like that: https://www.paypal.me/gorails

Reply

What I had before that was causing it to be put in an array was

<%= date_field :start_date, nil, value: params[:start_date] %>

and what I changed it to get just a normal string was

<%= date_field nil, :start_date, value: params[:start_date] %>

Reply

Those plain fields that aren't using form like form.date_filed always trip me up too. Small argument differences can mess up your params really easily. Thanks for sharing! :D

Reply
Join the discussion
Create an account Log in

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

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

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