[英]Ordering elasticsearch results, elasticsearch-rails gem, Rails
Im started to use Elasticsearh in my project, and have problem with result ordering. 我开始在我的项目中使用Elasticsearh,并对结果排序有疑问。
In fact I need to sort my records by hstore record in connected (belongs_to) model. 实际上,我需要按连接(belongs_to)模型中的hstore记录对记录进行排序。
More details: 更多细节:
So, I have a Model that I want to be searchable. 因此,我有一个要搜索的模型。 This model have connections with another models, here the code:
该模型与另一个模型有联系,此处为代码:
class PropertyObject < ActiveRecord::Base
belongs_to :country, :counter_cache => true
belongs_to :region, :counter_cache => true
belongs_to :city, :counter_cache => true
belongs_to :property_object_type, :counter_cache => true
belongs_to :property_object_state, :counter_cache => true
has_one :property_object_parameter_pack, dependent: :destroy
has_one :property_object_feature_pack, dependent: :destroy
has_one :property_object_description, dependent: :destroy
has_one :property_object_seo_field, dependent: :destroy
end
I want to include to my search results next fields: 我想在搜索结果中包括以下字段:
Model PropertyObject: 模型PropertyObject:
Model Country 示范国
Model Region 模型区域
Model City 模范城市
Model PropertyObjectDescription 模型PropertyObjectDescription
Model PropertyObjectParameterPack 模型PropertyObjectParameterPack
To make this work I had create concern Searchable and add it to my model PropertyObject. 为了完成这项工作,我创建了一个可搜索的关注对象并将其添加到我的模型PropertyObject中。 Here the code of it:
这里的代码:
module Searchable
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
mapping do
indexes :property_object_parameter_pack, type: 'nested' do
indexes :price do
indexes :min, type: :integer
end
end
end
# Customize the JSON serialization for Elasticsearch
def as_indexed_json(options={})
self.as_json(
include: {
country: {only: :title_translations},
region: {only: :title_translations},
city: {only: :title_translations},
property_object_description: {only: [:title_translations, :main_text_translations]},
property_object_parameter_pack: {only: [:price, :area, :rooms]}
})
end
end
end
Controller part where search is calling 搜索正在调用的控制器部分
def search
pagen = params[:page] || 1
@property_objects = PropertyObject.search(params[:q]).page(pagen).records
end
So now searching working and all seems good. 所以现在搜索工作,一切似乎都很好。 But I need sort results of search by min price .
但是我需要按最低价格对搜索结果进行排序。
I had try order method that works in my another orders - but no luck. 我尝试了可以在其他订单中使用的订购方法-但没有运气。
As I understand I need to use Elasticsearch sorting , to get result already sorted - but spend a lot of hours trying to implement this and fail. 据我了解,我需要使用Elasticsearch sorting来获得已经排序的结果-但是要花很多时间尝试实现这一点,但会失败。
What you can suggest me? 你能建议我什么?
UPDATE Had try this code: UPDATE尝试以下代码:
pagen = params[:page] || 1
query = params[:q]
params[:order] ||= 'asc'
property_objects = PropertyObject.search(query) do |s|
s.query do |q|
q.string query
end
s.sort { by :property_object_parameter_pack.price.min, params[:sort]}
end
@property_objects = property_objects.page(pagen).records
With different variants 具有不同的变体
s.sort by 排序
and no luck with ordering. 也没有运气。
In the end I decide to understand how Elasticsearch works, and start to read Elasticsearch: The Definitive Guide - where the answer was founded. 最后,我决定了解Elasticsearch的工作原理,并开始阅读Elasticsearch:权威指南-答案所在。
First off all I recommend to read this guide and install Marvel . 首先,我建议您阅读本指南并安装Marvel 。 After this all becomes much more clearer then using CURL.
之后,使用CURL会更加清晰。 In fact I discover my index structure with Marvel, and just implement it to search query of elasticsearch-rails gem.
实际上,我使用Marvel发现了我的索引结构,然后将其实现为对elasticsearch-rails gem进行查询。
Next thing that I did - I had rebuild price from hstore to separate integer columns : like price_min and price_max. 接下来要做的事情-我已经将价格从hstore重建为单独的整数列:例如price_min和price_max。 So in short the answer code is:
简而言之,答案代码为:
sq = {
"query": {
"multi_match": {
"query": "Prague",
"fields": [ "country.title_translations.en",
"region.title_translations.en",
"city.title_translations.en",
"property_object_description.main_text_translations.en",
"property_object_description.title_translations.en"
]
}
},
"track_scores": true,
"sort": {
"property_object_parameter_pack.price_min":
{ "order": "desc",
"missing" : "_last"
}
}} PropertyObject.search (sq)
In fact Im sure that it will work with hstore. 实际上,我确定它将与hstore一起使用。 Because I store translations in hstore and it indexing fine - just need to point right field (in this task Marvel helps with autocomplete).
因为我将翻译存储在hstore中并且可以很好地建立索引-只需指向正确的字段即可(在此任务中,Marvel有助于自动完成)。
Did you try this? 你有尝试过吗?
def search
options = { :page => (params[:page] || 1) }
@property_objects = PropertyObject.search, options do |f|
f.query { string params[:q] }
f.sort { by :price, 'desc' }
end
@property_objects.records
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.