Beispiel #1
0
        private void MakeSearch(SearchModel model)
        {
            string query = model.SearchText + "~";

            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);

            List<string> strings = new List<string>();            

            foreach (var checkBox in model.CheckBoxes)
            {
                if (checkBox.IsChecked)
                {
                    if (checkBox.SearchType == SearchType.Courses)
                    {
                        //make filtration here...
                        strings.Add("Name");
                        strings.Add("Content");
                    }
                    if (checkBox.SearchType == SearchType.Themes)
                    {
                        //make filtration here...
                        strings.Add("Theme");
                    }
                    if (checkBox.SearchType == SearchType.Users)
                    {
                        //make filtration here...
                        strings.Add("User");
                    }
                    if (checkBox.SearchType == SearchType.Curriculums)
                    {
                        //make filtration here...
                        strings.Add("Curriculum");
                    }
                    //make filtration here...
                }
            }

            MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                    Version.LUCENE_29,
                    strings.ToArray(),
                    //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<Curriculum> curriculums123 = _CurriculmService.GetCurriculums(_UserService.GetCurrentUser()).ToList();
            List<Course> courses123 = _CourseService.GetCourses(_UserService.GetCurrentUser()).ToList();
            List<ThemeDescription> themes123 = _CurriculmService.GetThemesAvailableForUser(_UserService.GetCurrentUser()).ToList();

            //List<Curriculum> themes123 = _CurriculmService.GetCurriculumsWithThemesOwnedByUser(_UserService.GetCurrentUser()).ToList();
            //foreach(Curriculum curr in curriculums123){
            //    themes123.InsertRange(themes123.Count - 1, _CurriculmService.GetThemesByCurriculumId(curr.Id));
            //}

            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("NodeID"));
                        node.Name = document.Get("Name");
                        node.CourseId = Convert.ToInt32(document.Get("NodeCourseID"));
                        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());
                        results.Add(result);
                        break;

                    case "course":

                        Course course = new Course();
                        course.Id = Convert.ToInt32(document.Get("CourseID"));
                        course.Name = document.Get("Name");
                        foreach (Course cour in courses123)
                        {
                            if (cour.Id == course.Id)
                            {
                                result = new CourseResult(course, _CourseService.GetCourse(course.Id).Updated.ToString(), _CourseService.GetCourse(course.Id).Owner);
                                results.Add(result);
                                break;
                            }
                        }
                        break;

                    case "curriculum":

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

                        string str = _CurriculmService.GetCurriculum(curriculum.Id).Owner;
                        foreach (Curriculum curr in curriculums123)
                        {
                            if (curr.Owner.Equals(curriculum.Owner))
                            {
                                result = new CurriculumResult(curriculum, _CurriculmService.GetCurriculum(curriculum.Id).Updated.ToString());
                                results.Add(result);
                                break;
                            }
                        }
                        break;

                    case "user":

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

                        result = new UserResult(user);
                        results.Add(result);
                        break;

                    case "group":

                        Group group = new Group();
                        group.Id = int.Parse(document.Get("GroupID"));
                        group.Name = document.Get("Group");
                        result = new GroupResult(group);
                        results.Add(result);
                        break;

                    case "theme":

                        Theme theme = new Theme();
                        theme.Id = Convert.ToInt32(document.Get("ThemeID"));
                        theme.Name = document.Get("Theme");
                        if (document.Get("CourseRef") == "null")
                        {
                            theme.CourseRef = null;
                        }
                        else
                        {
                            theme.CourseRef = Convert.ToInt32(document.Get("CourseRef"));
                        }

                        foreach (ThemeDescription themdesc in themes123)
                        {
                            if (themdesc.Theme.Id == theme.Id)
                            {
                                result = new ThemeResult(theme, _CourseService.GetCourse(theme.CourseRef.Value).Name);
                                results.Add(result);
                                break;
                            }
                        }
                        break;

                    default:
                        throw new Exception("Unknown result type");
                }  
            }
            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;


            model.SearchResult = results;
            
            model.Total = total;
            model.Score = Math.Abs(dataend.Millisecond - datastart.Millisecond);
        }
Beispiel #2
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);
        }