/// <summary> /// 重建索引 /// </summary> public void RebuildIndex() { //pageSize参数决定了每次批量取多少条数据进行索引。要注意的是,如果是分布式搜索,客户端会将这部分数据通过WCF传递给服务器端,而WCF默认的最大传输数据量是65535B,pageSize较大时这个设置显然是不够用的,WCF会报400错误;系统现在将最大传输量放宽了,但仍要注意一次不要传输过多,如遇异常,可适当调小pageSize的值 int pageSize = 1000; int pageIndex = 1; long totalRecords = 0; bool isBeginning = true; bool isEndding = false; UserQuery query = new UserQuery(); long followedUserId = 0; Document doc = null; do { //分页获取粉丝列表 PagingDataSet <User> users = userService.GetUsers(query, pageSize, pageIndex); totalRecords = users.TotalRecords; IEnumerable <long> followerUserIds = users.Select(n => n.UserId); isEndding = (pageSize * pageIndex < totalRecords) ? false : true; //根据粉丝列表批量查询关注用户列表 IEnumerable <FollowEntity> followedUsers = followService.GetFollowedUsers(followerUserIds); //循环关注用户列表(已按照UserId排序),对每个UserId建立索引 List <Document> docs = new List <Document>(); foreach (FollowEntity followedUser in followedUsers) { //判断当前记录的被关注用户的ID是否与上一条的被关注用户的ID相同。 //如果相同,说明属于同一个用户的关注对象 if (followedUser.UserId == followedUserId) { doc.Add(new Field(FollowedUserIds, followedUser.FollowedUserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); } else { //fixed by jiangshl //对于下一个用户,需要重新构建Document对象 doc = new Document(); doc.Add(new Field(UserId, followedUser.UserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field(FollowedUserIds, followedUser.FollowedUserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); docs.Add(doc); followedUserId = followedUser.UserId; } } searchEngine.RebuildIndex(docs, isBeginning, isEndding); isBeginning = false; pageIndex++; }while (!isEndding); }