Reputation: 168
I have add elasticsearch in my rails app and it works fine. But i want to give the user the ability to sort the search results with the attribute "created_at" and also with the score. I have add a drop down menu to do this. My problem is that i cannot make it work. I have tried a lot of different thinks but nothing so far. If anyone can show me the right way i will appreciate it.
Search controller:
class SearchController < ApplicationController
def search
options = { sort: params[:s] }
@products = Product.search(params[:q], options).paginate(page: params[:page], per_page: 5).records
end
end
Product.rb :
require "elasticsearch/model"
class Product < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :category
def self.search(query, options={})
@search_definition = {query: {}}
unless query.blank?
@search_definition[:query] = {
bool: {
should: [
{ multi_match: {
query: query,
fuzziness: 2,
fields: ['name^2', 'description','category.name', 'price'],
prefix_length: 2,
operator: 'and'
}}]}
}
else
@search_definition[:query] = { match_all: {} }
@search_definition[:sort] = { created_at: { order: 'desc'} }
end
if options[:sort]
@search_definition[:sort] = { options[:sort] => 'desc' }
end
__elasticsearch__.search @search_definition
end
settings analysis: {
analyzer: {
my_index_analyzer: {
type: "custom",
tokenizer: "standard",
filter: ["standard", "lowercase", "translation"]
},
my_search_analyzer: {
type: "custom",
tokenizer: "standard",
filter: ["standard", "lowercase"]
}
},
filter: {
translation: {
type: "nGram",
min_gram: 2,
max_gram: 20
}
}
}
mapping do
indexes :name, type: 'string', index_analyzer: 'my_index_analyzer', search_analyzer: 'my_search_analyzer'
indexes :description, type: 'string', index_analyzer: 'my_index_analyzer', search_analyzer: 'my_search_analyzer'
indexes :created_at, type: 'date'
indexes :category do
indexes :name, type: 'string', index_analyzer: 'my_index_analyzer', search_analyzer: 'my_search_analyzer'
end
end
def as_indexed_json(options={})
as_json(
only: [:name, :description, :price],
include: { category: { only: :name } }
)
end
end
Product.import
search.html.erb (the drop down menu at results page)
<div class="row">
<div class="col-xs-6 col-xs-offset-1">
<div class="btn-group pull-right">
<button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">
<% sort = case
when params[:s] then params[:s]
when params[:q].blank? then 'created_at'
else 'relevancy'
end
%>
sorted by <%= sort.humanize.downcase %> <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><%= link_to "Sort by created at", search_path(params.except(:controller, :action).merge(s: 'created_at')), class: 'btn-xs' %></li>
<li><%= link_to "Sort by relevancy", search_path(params.except(:controller, :action).merge(s: nil)), class: 'btn-xs' %></li>
</ul>
</div>
</div>
</div>
Upvotes: 4
Views: 1803
Reputation: 56
Elasticsearch sort option receives an array. You may try this:
@search_definition[:sort] = [{"created_at" => { "order"=> "desc"}}]
Upvotes: 1