代码改变世界

T a s k 搜 索 ( L u c e n e )

2011-11-04 17:33  yuejianjun  阅读(192)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Comm;
using Entity;

namespace Service
{
   public class SearchTask
    {
        public static ReturnItem TaskSearchModuleSecond(QueryItemEntity queryItemEntity)
        {
            string[] indexPath = Profile.IndexModuleSecondPath();
            Task<ReturnItem> parentTask = new Task<ReturnItem>(() =>
            {
                IList<Task<ReturnItem>> taskArray = new List<Task<ReturnItem>>();

                for (int i = 0; i < indexPath.Length; i++)
                {
                    string path = indexPath[i];

                    Task<ReturnItem> childTask = new Task<ReturnItem>
                         (() =>
                         {
                             ReturnItem result = SearchService(queryItemEntity, path);
                             return result;
                         }, TaskCreationOptions.AttachedToParent);
                    taskArray.Add(childTask);
                }

                foreach (Task<ReturnItem> t in taskArray)
                    t.Start();

                ReturnItem listResult = new ReturnItem();

                foreach (Task<ReturnItem> t in taskArray)
                {
                    listResult.TotalHits += t.Result.TotalHits;
                    if (listResult.ReturnList == null)
                    {
                        listResult.ReturnList = t.Result.ReturnList;
                    }
                    else if (t.Result.ReturnList != null)
                    {
                        listResult.ReturnList.AddRange(t.Result.ReturnList); 
                    }
                } 
                return listResult;
            });
            parentTask.Start();
            parentTask.Wait();
            ReturnItem list = parentTask.Result;
            list.ReturnList.Sort((IndexEntity x, IndexEntity y) => y.Rank.CompareTo(x.Rank));
            return list;
        }
        public static ReturnItem SearchService(QueryItemEntity queryItemEntity, string path)
        {
          ReturnItem r=  SearchBase.MainSearch(queryItemEntity,false ,true ,path );
          return r;
        }
    }
}