[英]Elasticsearch in Rails sort results
I have add elasticsearch in my rails app and it works fine. 我在我的rails应用程序中添加了elasticsearch,它工作正常。 But i want to give the user the ability to sort the search results with the attribute "created_at" and also with the score.
但是我想让用户能够使用“ created_at”属性和得分对搜索结果进行排序。 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 : 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) search.html.erb(结果页面的下拉菜单)
<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>
Elasticsearch sort option receives an array. Elasticsearch排序选项接收一个数组。 You may try this:
您可以尝试以下方法:
@search_definition[:sort] = [{"created_at" => { "order"=> "desc"}}] @search_definition [:sort] = [{“ created_at” => {“ order” =>“ desc”}}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.