订购弹性搜索结果,弹性搜索轨道宝石,Rails

编程入门 行业动态 更新时间:2024-10-25 10:24:40
本文介绍了订购弹性搜索结果,弹性搜索轨道宝石,Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

事实上,我需要通过连接(belongs_to)中的hstore记录对我的记录进行排序,因为我在项目中开始使用Elasticsearh,模型。

更多细节:

所以,我有一个我想要搜索的模型。这个模型与另一个模型有联系,这里是代码:

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,依赖::destroy has_one:property_object_feature_pack,依赖::destroy has_one:property_object_description,dependent::destroy has_one:property_object_seo_field,dependent ::destroy end

我想包含在我的搜索结果中的下一个字段:

模型PropertyObject:

  • :code:string

模型国家

  • :title_translations:hstore

模型区域

  • :title_translations:hstore

模特城

  • :title_translations:hstore

模型PropertyObjectDescription

  • :title_translations:hstore
  • :main_text_translations:h存储

模型PropertyObjectParameterPack

  • :price:hstore(example:{min => 10,max => 100})

工作我创造关注可搜索,并将其添加到我的模型PropertyObject。 这里的代码:

module Searchable extends ActiveSupport :: Concern 包含包括Elasticsearch ::模型包括Elasticsearch ::模型::回调 映射do 索引:property_object_parameter_pack,键入:'嵌套'做索引:price do 索引:最小,类型::整数结束结束结束 #自定义弹性搜索的JSON序列化 def as_indexed_json(options = {}) self.as_json( include:{ country:{only::title_translations}, region:{only:标题_translations}, city:{only::title_translations}, property_object_description:{only:[:title_translations,,main_text_translations]}, property_object_parameter_pack:{only:[:price, :rooms]} }) end end end

控制器部分,其中搜索正在调用

def search pagen = params [:page] || 1 @property_objects = PropertyObject.search(params [:q])。page(pagen).records end

所以现在搜索工作和一切似乎都很好。但是,我需要按最低价格排序搜索结果。

我尝试了在另一个订单中工作的订单方式 - 但没有运气。

据了解,我需要使用Elasticsearch排序,以获得结果已排序 - 但花费大量的时间尝试实现并失败。

你可以建议我什么?

更新 尝试此代码:

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

使用不同的变体

s.sort by

  • 由:price
  • 由:price.min
  • by:price [:min]
  • 由:property_object_parameter_pack.price.min
  • 由:property_object_parameter_pack.price [ :min]

没有运气订购。

解决方案

最后我决定了解Elasticsearch的工作原理,并开始阅读Elasticsearch:The Definitive Guide - 答案是成立。

首先我建议您阅读本指南并安装 Marvel 。之后,使用CURL变得更加清晰。事实上,我发现我的索引结构与Marvel,只是实现它来搜索elasticsearch-rails gem的查询。

我做的下一件事 - 我从hstore重建价格分隔整数列:如price_min和price_max。 简而言之,答案代码是:

sq = {query:{multi_match:{查询:布拉格,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)

事实上我肯定它会与hstore一起工作。因为我将翻译存储在hstore中,并且索引很好 - 只需要点右边的字段(在这个任务中,Marvel帮助自动完成)。

Im started to use Elasticsearh in my project, and have problem with result ordering.

In fact I need to sort my records by hstore record in connected (belongs_to) model.

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:

  • :code :string

Model Country

  • :title_translations :hstore

Model Region

  • :title_translations :hstore

Model City

  • :title_translations :hstore

Model PropertyObjectDescription

  • :title_translations :hstore
  • :main_text_translations :hstore

Model PropertyObjectParameterPack

  • :price :hstore (example: {min => 10, max=>100})

To make this work I had create concern Searchable and add it to my model 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.

What you can suggest me?

UPDATE Had try this code:

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

  • by :price
  • by :price.min
  • by :price[:min]
  • by :property_object_parameter_pack.price.min
  • by :property_object_parameter_pack.price[:min]

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.

First off all I recommend to read this guide and install Marvel . After this all becomes much more clearer then using CURL. In fact I discover my index structure with Marvel, and just implement it to search query of elasticsearch-rails gem.

Next thing that I did - I had rebuild price from hstore to separate integer columns : like price_min and 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. Because I store translations in hstore and it indexing fine - just need to point right field (in this task Marvel helps with autocomplete).

更多推荐

订购弹性搜索结果,弹性搜索轨道宝石,Rails

本文发布于:2023-11-30 00:35:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹性   搜索结果   轨道   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!