private BaseQueryEntity <LayImUser> SearchUser(string keyword, int pageIndex = 1, int pageSize = 50) { var hasvalue = ValidateHelper.HasValue(keyword); var from = (pageIndex - 1) * pageSize; //全部的时候按照省份排序 string queryAll = "{\"query\":{\"match_all\":{}},\"from\":" + from + ",\"size\":" + pageSize + ",\"sort\":{\"province\":{\"order\":\"asc\"}}}"; //按照关键字搜索的时候,默认排序,会把最接近在在最上边 int im = hasvalue ? keyword.ToInt() : 0; //这里增加im是否为int类型判断,如果是int类型,那么可能是查询用户的IM号码,否则就是关键字查询 string term = im == 0 ? "{\"im\":0}" : "{\"im\":" + keyword + "}"; string queryWithKeyWord = "{\"query\":{\"filtered\":{\"filter\":{\"or\":[{\"term\":" + term + "},{\"query\":{\"match_phrase\":{\"nickname\":{\"query\":\"" + keyword + "\",\"slop\":0}}}}]}}},\"from\":" + from + ",\"size\":" + pageSize + ",\"sort\":{}}"; string queryFinal = hasvalue ? queryWithKeyWord : queryAll; var result = es.QueryBayConditions(queryFinal); return(result); }