一个根据内容和用户的联合查询。。。现有一问题:当我传递两个参数时,怎么将两个参数中间加一个逗号,不太会用 join(",") 希望大牛指点,另外感觉代码需优化的地儿太多了。。。望指点!!!当我输入两个参数时 query_str 如下: "{query: {bool: {must: [{match: {content: params[:q]}} {match: {user_id: params[:user_id]}}] }},:highlight=>{:fields=>{:content=>{}}}}"
def index
@page_title = "评论列表"
if params[:q].present? || params[:user_id].present?
query_str = "{query: {bool: {must: ["
query_str += "{match: {content: params[:q]}}" if params[:q].present?
query_str += "{match: {user_id: params[:user_id]}" if params[:user_id].present?
query_str += "}] }},:highlight=>{:fields=>{:content=>{}}}}"
query = eval(query_str)
else
query = {query: { match_all: {} }, sort: {updated_at: {order: "desc"}}} #当没有查询条件时按照update排序
end
@comments = Comment.__elasticsearch__.search(query).paginate(page: params[:page] || 1, per_page: params[:per_page] || 20)
@records = @comments.records.load
end
Updated by @huacnlee 这代码就真的对不齐么,每次都要我来!