对elasticsearch结果进行排序,elasticsearch-railsgem,Rails

Ordering elasticsearch results, elasticsearch-rails gem, Rails

我开始在我的项目中使用 Elasticsearch,结果排序有问题。

事实上,我需要在连接 (belongs_to) 模型中按 hstore 记录对我的记录进行排序。

更多详情:

所以,我有一个模型,我希望它可以被搜索到。此型号与其他型号有关联,此处代码:

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

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

模型属性对象:

模型国家

模型区域

模型城市

模型 PropertyObjectDescription

模型属性对象参数包

为了完成这项工作,我创建了关注点 Searchable 并将其添加到我的模型 PropertyObject 中。 这是它的代码:

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

调用搜索的控制器部分

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

end

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

我尝试过在我的另一个订单中使用的订单方法 - 但没有成功。

据我所知,我需要使用 Elasticsearch sorting 来获得已经排序的结果——但是我花了很多时间来尝试实现它并失败了。

你能给我什么建议?

更新 试过这个代码:

 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 来自

而且没有成功订购。

你试过了吗?

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

最终我决定了解 Elasticsearch 的工作原理,并开始阅读 Elasticsearch: The Definitive Guide - where the answer was founded.

首先,我建议阅读本指南并安装 Marvel .在此之后,一切都变得比使用 CURL 更清晰。事实上,我是用 Marvel 发现我的索引结构,并将其实现到 elasticsearch-rails gem.

的搜索查询中

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

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)

事实上,我确信它可以与 hstore 一起使用。因为我将翻译存储在 hstore 中并且它的索引很好 - 只需要指向正确的字段(在这个任务中 Marvel 帮助自动完成)。