2012-08-07 91 views
2

我有播放器和播放器具有Profil全名。我在Player上使用elasticsearch。我需要通过全名进行默认排序。我该如何设置它?从播放器类文件我的代码:RoR:弹性搜索按映射字段排序

...... 
    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来:在排序全名我有这样的错误:

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\": o[email protected]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\": o[email protected]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} 
+0

对此有何更新?我在与名 – 2012-08-20 22:26:39

+0

排序相同的问题不好意思,不,我使用jQuery的排序,但它不是好的解决办法 – quatermain 2012-08-20 22:33:31

回答

7

我明白了!愚蠢的研究后,我发现this article here。第一个答案中提到:

The field user by default gets analyzed and broken down into one or more tokens. If it gets broken down into more than one token, then you can't really sort on it. If you want to sort on it, you either need to set it i(in the mappings) as not analyzed

在你的情况,你需要做的是:

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 

,它应该工作。

+1

工作完美 – quatermain 2012-08-21 11:25:00

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

更改 '身份证' 到 '全名' 应该工作。

+0

没有,错误,我添加到我的帖子 – quatermain 2012-08-07 21:24:15