簡體   English   中英

RoR:Elasticsearch按映射字段排序

[英]RoR: Elasticsearch sort by mapped field

我有播放器,播放器有Profil的全名。 我在Player上使用elasticsearch。 我需要按全名進行默認排序。 我該如何設置? 我的Player類文件中的代碼:

   ......
  mapping do
    indexes :id, type: 'integer'
    indexes :fullname, boost: 10
    indexes :name
    indexes :surname
    indexes :position_name
    indexes :info
    indexes :club_id, type: 'integer'
    indexes :club_name
  end

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 20) do
      query do
        boolean do
          if params[:query].present?
            must { string params[:query], default_operator: "AND" } 
          else
            must {string '*'}
          end
          must { term :position_id, params[:position_id]} if params[:position_id].present?
          must { term :club_id, params[:club_id]} if params[:club_id].present?
        end
      end
      # filter :term, user_id: params[:user_id] if params[:user_id].present?
      # sort { by Item.column_names.include?(params[:sort]) ? params[:sort] : "created_at", %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc" } if params[:query].blank?

      sort { by Player.column_names.include?(params[:sort]) ? params[:sort] : :id ,%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"}
      facet "positions" do
        terms :position_id
      end
      facet "clubs" do
        terms :club_id
      end
      # raise to_curl
    end
  end

  def to_indexed_json
    to_json(methods: [:fullname, :name, :surname, :position_name, :club_name])
  end

  def fullname
        self.profil.fullname
  end
......

如果我將:id更改為:fullname,則會出現此錯誤:

500 : {"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[DBgvi6JiQAi0FTwhonq8Ag][players][0]: QueryPhaseExecutionException[[players][0]: query[ConstantScore(NotDeleted(cache(_type:player)))],from[0],size[20],sort[<custom:\"fullname\": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@33a626ac>!]: Query Failed [Failed to execute main query]]; nested: IOException[Can't sort on string types with more than one value per doc, or more than one token per field]; }{[DBgvi6JiQAi0FTwhonq8Ag][players][4]: QueryPhaseExecutionException[[players][4]: query[ConstantScore(NotDeleted(cache(_type:player)))],from[0],size[20],sort[<custom:\"fullname\": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@3274eb8a>!]: Query Failed [Failed to execute main query]]; nested: IOException[Can't sort on string types with more than one value per doc, or more than one token per field]; }]","status":500}

我知道了! 經過大量的研究,我在這里找到了這篇文章 第一個答案提到:

默認情況下,將對現場用戶進行分析並將其分解為一個或多個令牌。 如果將其分解為多個令牌,那么您就無法對其進行排序。 如果要對其進行排序,則需要將其i(在映射中)設置為未分析

根據您的情況,您需要做的是:

 mapping do
    indexes :id, type: 'integer'
    indexes :fullname, :index => 'not_analyzed'
    indexes :name
    indexes :surname
    indexes :position_name
    indexes :info
    indexes :club_id, type: 'integer'
    indexes :club_name
  end

它應該工作。

sort { by Player.column_names.include?(params[:sort]) ? params[:sort] : :fullname ,%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"}

將“ id”更改為“全名”應該可以。

暫無
暫無

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

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