Search code examples
ruby-on-railssortingsearchkick

Searchkick - dynamic ordering and sorting


I have implemented searchkick for elasticsearch, but would like to order and sort my results based on column names and direction stored in my variables.

Something like the following:

@suppliers = Supplier.search(
    query, 
    where: {
      :active => true
    }, 
    order: {
      "#{@column}": :"#{@direction}"
    },
    page: params[:page], 
    per_page:  @per_page, 
    misspellings: {distance: 2}, 
    fields: fields) 

But the part:

order: {
      "#{@column}": :"#{@direction}"
    },

does not work and throws error. Any idea how it should be written?

Thank you, Miroslav


Solution

  • So this is the solution:

    order: {
          "#{@sort}" => "#{@direction}"
        },