lucene 3.0 + 盘古分词 + 关键字高亮 + 分页的实现与demo
2015-01-09 11:44 图安 阅读(610) 评论(0) 编辑 收藏 举报Demo, 请见 www.guoxuetow.com/search
第一步创建索引: 很简单,从数据库里查出你想要索引的数据,并调用lucene的api进行索引就行了。
1 static void CreateIndex() 2 { 3 PanGu.Segment.Init(); 4 5 var service = new Lura.Gxt.Service.Sg_AuthorSummaryServiceTest(); 6 var writer = new IndexWriter(FSDirectory.Open(INDEX_DIR), analyzer, true, IndexWriter.MaxFieldLength.LIMITED); 7 8 var totalCount = service.GetAuthorCount(); 9 var totalPages = (int)Math.Ceiling(totalCount / (float)1000); 10 for (int i = 1; i <= totalPages; i++) 11 { 12 Stopwatch sw = new Stopwatch(); 13 sw.Start(); 14 15 var authorList = service.GetPagedAuthor(i); 16 foreach (var item in authorList) 17 { 18 Document doc = new Document(); 19 Field authorId = new Field("AuthorId", item.AuthorId.ToString(), Field.Store.YES, Field.Index.ANALYZED); 20 Field dynasty = new Field("Dynasty", SafeGetString(item.Dynasty), Field.Store.YES, Field.Index.ANALYZED); 21 Field authorName = new Field("AuthorName", SafeGetString(item.AuthorName), Field.Store.YES, Field.Index.ANALYZED); 22 Field authorSummary = new Field("AuthorSummary", SafeGetString(item.AuthorSummary), Field.Store.YES, Field.Index.ANALYZED); 23 Field authorSmallImage = new Field("AuthorSmallImage", SafeGetString(item.AuthorSmallImage), Field.Store.YES, Field.Index.NO); 24 Field authorLargeImage = new Field("AuthorLargeImage", SafeGetString(item.AuthorLargeImage), Field.Store.YES, Field.Index.NO); 25 Field statuses = new Field("Statuses", SafeGetString(item.Statuses), Field.Store.YES, Field.Index.NO); 26 27 doc.Add(authorId); 28 doc.Add(dynasty); 29 doc.Add(authorName); 30 doc.Add(authorSummary); 31 doc.Add(authorSmallImage); 32 doc.Add(authorLargeImage); 33 doc.Add(statuses); 34 35 writer.AddDocument(doc); 36 } 37 38 sw.Stop(); 39 Console.Write("建立" + authorList.Count() + "索引,花费: " + sw.Elapsed); 40 } 41 42 writer.Optimize(); 43 writer.Commit(); 44 writer.Dispose(); 45 46 }
第二步: 查询索引
1 public SearchPeopleView SearchAuthor(string keyword, int pagesize, int page) 2 { 3 var searchPeopleView = new SearchPeopleView(); 4 5 6 if (string.IsNullOrEmpty(keyword)) 7 return searchPeopleView; 8 9 IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(Author_INDEX_DIR), true); 10 QueryParser qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "AuthorName", analyzer); 11 Query query = qp.Parse(keyword); 12 13 14 var collector = TopScoreDocCollector.Create(page * pagesize, false); 15 16 searcher.Search(query, collector); 17 18 19 //开始数 20 int start = pagesize * (page - 1); 21 //结束数 22 int limit = start + pagesize; 23 24 ScoreDoc[] hits = collector.TopDocs(start, limit).ScoreDocs; 25 26 27 var authorList = new List<Author>(); 28 foreach (ScoreDoc sd in hits) 29 { 30 31 Document doc = searcher.Doc(sd.Doc); 32 var summaryall = doc.Get("AuthorSummary"); 33 var summary = summaryall.Length> 300 ? summaryall.Substring(0,300)+"......": summaryall; 34 35 var author = new Author() 36 { 37 AuthorId = Int64.Parse(doc.Get("AuthorId")), 38 Dynasty = doc.Get("Dynasty"), 39 AuthorName = doc.Get("AuthorName"), 40 AuthorSummary = SetHighlighter(keyword, summary), 41 AuthorSmallImage = doc.Get("AuthorSmallImage"), 42 AuthorLargeImage = doc.Get("AuthorLargeImage"), 43 Statuses = doc.Get("Statuses") 44 }; 45 46 authorList.Add(author); 47 } 48 searcher.Dispose(); 49 50 51 52 searchPeopleView.PageInfo.CurrentPage = page; 53 searchPeopleView.PageInfo.TotalCount = collector.TotalHits; 54 searchPeopleView.PageInfo.TotalPage = (int)Math.Ceiling(collector.TotalHits / (float)pagesize);// collector.TotalHits; 55 searchPeopleView.PeopleList = authorList; 56 57 58 return searchPeopleView; 59 }
第三步 ,分页展示
<div class="row"> <h3 id="people" class="page-header">人物 <small>共@(Model.PeopleView.PageInfo.TotalCount)条记录,当前第@(Model.PeopleView.PageInfo.CurrentPage)页</small></h3> <ul class="media-list"> @foreach (var p in Model.PeopleView.PeopleList) { <li class="media"> <div class="pull-left"> <a href="/people/@p.AuthorId"> <img width="50" alt="@Html.Raw(p.AuthorName)" src="@p.AuthorSmallImage" /> </a> </div> <div class="media-body"> <h4 class="media-heading"><span>[人物] @(p.Dynasty)▪</span><a href="/people/@p.AuthorId">@Html.Raw(@p.AuthorName)</a> </h4> <p>@Html.Raw(p.AuthorSummary)</p> <a href="/people/@p.AuthorId">www.guoxuetow.com/people/@p.AuthorId</a> </div> </li> } @if (Model.PeopleView.PeopleList.Count() == 0) { <li class="media"> <h4 class="media-heading">没有人物记录。 </h4> </li> } </ul> <ul id="author-pagination" class="pagination-sm"></ul> <script type="text/javascript"> var totlaapage = parseInt('@(Model.PeopleView.PageInfo.TotalPage)'); var apage = parseInt('@(Model.PeopleView.PageInfo.CurrentPage)'); $('#author-pagination').twbsPagination({ totalPages: totlaapage, startPage: apage, visiblePages: 20, href: '@Html.Raw(@authorPageUrl)', first: '首页', prev: '上一页', next: '下一页', last: '末页', onPageClick: function (event, page) { window.location.href = "@Html.Raw(@authorPageUrl1)" + page; // $('#page-content').text('Page ' + page); } }); </script> </div>
晚上回家再写.