簡體   English   中英

Rails中的Elasticsearch對結果進行排序

[英]Elasticsearch in Rails sort results

我在我的rails應用程序中添加了elasticsearch,它工作正常。 但是我想讓用戶能夠使用“ created_at”屬性和得分對搜索結果進行排序。 我添加了一個下拉菜單來執行此操作。 我的問題是我無法使其工作。 我嘗試了許多不同的想法,但到目前為止沒有任何嘗試。 如果有人可以向我展示正確的方式,我將不勝感激。

搜索控制器:

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:

    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(結果頁面的下拉菜單)

<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排序選項接收一個數組。 您可以嘗試以下方法:

@search_definition [:sort] = [{“ created_at” => {“ order” =>“ desc”}}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM