Exemplo n.º 1
0
 public override void TearDown()
 {
     reader.Close();
     dir2.Close();
 }
Exemplo n.º 2
0
 public void Close()
 {
     Directory.Close(ID);
 }
Exemplo n.º 3
0
        public ActionResult Search(String query)
        {
            if (query == "")
            {
                return(RedirectToAction("Index"));
            }

            query = query + "~";

            DateTime      datastart = DateTime.Now;
            Directory     directory = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("~/Data/Index")));
            IndexSearcher searcher  = new IndexSearcher(directory, true);
            Analyzer      analyzer  = new StandardAnalyzer(Version.LUCENE_29);

            MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                Version.LUCENE_29,
                new String[] { "Name", "Content", "Curriculum", "User", "Group", "Theme" },
                analyzer
                );


            ScoreDoc[] scoreDocs = searcher.Search(queryParser.Parse(query), 100).scoreDocs;

            Hits hit   = searcher.Search(queryParser.Parse(query));
            int  total = hit.Length();



            List <ISearchResult> results = new List <ISearchResult>();
            Stopwatch            sw      = new Stopwatch();

            sw.Start();
            foreach (ScoreDoc doc in scoreDocs)
            {
                ISearchResult result;
                Document      document = searcher.Doc(doc.doc);
                String        type     = document.Get("Type").ToLower();

                switch (type)
                {
                case "node":
                    Node node = new Node();
                    node.Id       = Convert.ToInt32(document.Get("ID"));
                    node.Name     = document.Get("Name");
                    node.CourseId = Convert.ToInt32(document.Get("CourseID"));
                    node.IsFolder = Convert.ToBoolean(document.Get("isFolder"));

                    result = new NodeResult(node, _CourseService.GetCourse(node.CourseId).Name, document.Get("Content"), _CourseService.GetCourse(node.CourseId).Updated.ToString());

                    break;

                case "course":

                    Course course = new Course();
                    course.Id   = Convert.ToInt32(document.Get("ID"));
                    course.Name = document.Get("Name");

                    result = new CourseResult(course, _CourseService.GetCourse(course.Id).Updated.ToString(), _CourseService.GetCourse(course.Id).Owner);

                    break;

                case "curriculum":

                    Curriculum curriculum = new Curriculum();
                    curriculum.Id   = Convert.ToInt32(document.Get("ID"));
                    curriculum.Name = document.Get("Curriculum");

                    result = new CurriculumResult(curriculum, _CurriculmService.GetCurriculum(curriculum.Id).Updated.ToString());

                    break;

                case "user":

                    User user = new User();
                    user.Id   = Guid.Parse(document.Get("ID"));
                    user.Name = document.Get("User");
                    /*user.RoleId = Convert.ToInt32(document.Get("RoleId"));*/

                    result = new UserResult(user);

                    break;

                case "group":

                    Group group = new Group();
                    group.Id   = int.Parse(document.Get("ID"));
                    group.Name = document.Get("Group");

                    result = new GroupResult(group);

                    break;

                case "theme":

                    Theme theme = new Theme();
                    theme.Id        = Convert.ToInt32(document.Get("ID"));
                    theme.Name      = document.Get("Theme");
                    theme.CourseRef = Convert.ToInt32(document.Get("CourseRef"));

                    result = new ThemeResult(theme, _CourseService.GetCourse(theme.CourseRef.Value).Name);

                    break;

                default:
                    throw new Exception("Unknown result type");
                }

                results.Add(result);
            }
            sw.Stop();

            DateTime dataend = DateTime.Now;

            analyzer.Close();
            searcher.Close();
            directory.Close();

            ViewData["SearchString"] = query;
            ViewData["score"]        = Math.Abs(dataend.Millisecond - datastart.Millisecond); //sw.ElapsedMilliseconds.ToString();
            ViewData["total"]        = total;

            return(View(results));
        }