/// <summary> /// /// </summary> /// <param name="queryString">关键字</param> /// <param name="documentType">文档类型</param> /// <param name="start">开始时间</param> /// <param name="end">结束时间</param> public List <DocumentSearchResult> Query(string queryString, string documentType, string userId, string depId, int?start = null, int?takeSize = null) { _logger.InfoFormat("SearchService 查询条件:{0},{1}", queryString, documentType); if (string.IsNullOrWhiteSpace(queryString)) { return(new List <DocumentSearchResult>()); } var results = _searchProvider.Query <DocumentSearchResult>(queryString.Trim(), start, takeSize); if (!string.IsNullOrWhiteSpace(userId) && !string.IsNullOrWhiteSpace(depId)) { results = results.FindAll(f => f.CreateUserId == userId || (f.DepId == depId && f.Visible == (int)Visible.Dep) || f.Visible == (int)Visible.Public); } if (!string.IsNullOrWhiteSpace(documentType)) { results = results.FindAll(f => f.DocumentType == documentType); } foreach (var result in results) { result.Name = SplitContent.HightLightTitle(queryString, result.Name); result.Content = SplitContent.HightLight(queryString, result.Content); } return(results); }
public ActionResult Search(string searchText, string onlySearchMine, int?page) { int pageSize = PAGE_SIZE; int pageNumber = page ?? 1; ViewBag.CurrentFilter = searchText; ViewBag.IsOnlySearchMine = onlySearchMine != null && onlySearchMine.Equals("on", StringComparison.InvariantCultureIgnoreCase); if (!String.IsNullOrWhiteSpace(searchText)) { bool isOnlySearchMine = ViewBag.IsOnlySearchMine; var result = docMgr.Search(searchText, UserId, isOnlySearchMine); if (result != null && result.Count > 0) { foreach (var r in result) { r.title = SplitContent.HightLight(searchText, r.title); r.content = SplitContent.HightLight(searchText, r.content != null ? r.content.StripHTML() : ""); r.category = SplitContent.HightLight(searchText, r.category); } return(View(result.ToPagedList(pageNumber, pageSize))); } } return(View()); }
private void SearchFromIndexData(string searchkey) { string indexPath = Context.Server.MapPath("~/IndexData"); FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); //搜索条件 PhraseQuery query = new PhraseQuery(); //把用户输入的关键字进行分词 foreach (string word in SplitContent.SplitWords(searchkey)) { query.Add(new Term("TITLE", word)); } //query.Add(new Term("content", "C#"));//多个查询条件时 为且的关系 query.SetSlop(100); //指定关键词相隔最大距离 //TopScoreDocCollector盛放查询结果的容器 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); searcher.Search(query, null, collector); //根据query查询条件进行查询,查询结果放入collector容器 //TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果 ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs; //collector.GetTotalHits() //展示数据实体对象集合 for (int i = 0; i < docs.Length; i++) { int docID = docs[i].doc; //得到查询结果文档的ID(Lucene内部分配的ID) Document doc = searcher.Doc(docID); //根据文档ID来获得文档对象Document SUC_NEWS mod = new SUC_NEWS(); mod.TITLE = SplitContent.HightLight(searchkey, doc.Get("TITLE")); mod.TITLE = string.IsNullOrEmpty(mod.TITLE) ? doc.Get("TITLE") : mod.TITLE; //book.ContentDESCRPTION = doc.Get("content");//未使用高亮 //搜索关键字高亮显示 使用盘古提供高亮插件 mod.CONTENT = SplitContent.HightLight(searchkey, doc.Get("CONTENT")); mod.CONTENT = string.IsNullOrEmpty(mod.CONTENT) ? doc.Get("CONTENT") : mod.CONTENT; mod.CONTENT = mod.CONTENT.Replace("<b>", ""); mod.ID = Convert.ToInt32(doc.Get("ID")); mod.pandaWebUrl = doc.Get("URL"); modResult.Add(mod); } }
public ActionResult SearchByCategory(string category, bool byOwner, int?page) { ViewBag.CurrentFilter = category; if (!String.IsNullOrWhiteSpace(category)) { int pageSize = PAGE_SIZE; int pageNumber = page ?? 1; var result = docMgr.SearchByCategory(category, byOwner ? UserId : ""); foreach (var r in result) { r.title = r.title; r.content = r.content.StripHTML().GetShortDesc(); r.category = SplitContent.HightLight(category, r.category); } return(View("Search", result.ToPagedList(pageNumber, pageSize))); } return(View("Search")); }
public ActionResult SearchBook(string searchText) { ViewBag.CurrentFilter = searchText; if (!String.IsNullOrWhiteSpace(searchText)) { var result = docMgr.SearchBook(searchText, UserId); if (result != null && result.Count > 0) { foreach (var r in result) { r.name = SplitContent.HightLight(searchText, r.name); r.description = SplitContent.HightLight(searchText, r.description.StripHTML()); r.category = SplitContent.HightLight(searchText, r.category); } return(View(result)); } } return(View()); }