简体   繁体   中英

Ordering elasticsearch results, elasticsearch-rails gem, Rails

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM