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 &#61; 1; i <&#61; totalPages; i&#43;&#43;)
11 {
12 Stopwatch sw &#61; new Stopwatch();
13 sw.Start();
14
15 var authorList &#61; service.GetPagedAuthor(i);
16 foreach (var item in authorList)
17 {
18 Document doc &#61; new Document();
19 Field authorId &#61; new Field("AuthorId", item.AuthorId.ToString(), Field.Store.YES, Field.Index.ANALYZED);
20 Field dynasty &#61; new Field("Dynasty", SafeGetString(item.Dynasty), Field.Store.YES, Field.Index.ANALYZED);
21 Field authorName &#61; new Field("AuthorName", SafeGetString(item.AuthorName), Field.Store.YES, Field.Index.ANALYZED);
22 Field authorSummary &#61; new Field("AuthorSummary", SafeGetString(item.AuthorSummary), Field.Store.YES, Field.Index.ANALYZED);
23 Field authorSmallImage &#61; new Field("AuthorSmallImage", SafeGetString(item.AuthorSmallImage), Field.Store.YES, Field.Index.NO);
24 Field authorLargeImage &#61; new Field("AuthorLargeImage", SafeGetString(item.AuthorLargeImage), Field.Store.YES, Field.Index.NO);
25 Field statuses &#61; 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("建立" &#43; authorList.Count() &#43; "索引,花费: " &#43; sw.Elapsed);
40 }
41
42 writer.Optimize();
43 writer.Commit();
44 writer.Dispose();
45
46 }
第二步&#xff1a; 查询索引
1 public SearchPeopleView SearchAuthor(string keyword, int pagesize, int page)
2 {
3 var searchPeopleView &#61; new SearchPeopleView();
4
5
6 if (string.IsNullOrEmpty(keyword))
7 return searchPeopleView;
8
9 IndexSearcher searcher &#61; new IndexSearcher(FSDirectory.Open(Author_INDEX_DIR), true);
10 QueryParser qp &#61; new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "AuthorName", analyzer);
11 Query query &#61; qp.Parse(keyword);
12
13
14 var collector &#61; TopScoreDocCollector.Create(page * pagesize, false);
15
16 searcher.Search(query, collector);
17
18
19 //开始数
20 int start &#61; pagesize * (page - 1);
21 //结束数
22 int limit &#61; start &#43; pagesize;
23
24 ScoreDoc[] hits &#61; collector.TopDocs(start, limit).ScoreDocs;
25
26
27 var authorList &#61; new List
28 foreach (ScoreDoc sd in hits)
29 {
30
31 Document doc &#61; searcher.Doc(sd.Doc);
32 var summaryall &#61; doc.Get("AuthorSummary");
33 var summary &#61; summaryall.Length> 300 ? summaryall.Substring(0,300)&#43;"......": summaryall;
34
35 var author &#61; new Author()
36 {
37 AuthorId &#61; Int64.Parse(doc.Get("AuthorId")),
38 Dynasty &#61; doc.Get("Dynasty"),
39 AuthorName &#61; doc.Get("AuthorName"),
40 AuthorSummary &#61; SetHighlighter(keyword, summary),
41 AuthorSmallImage &#61; doc.Get("AuthorSmallImage"),
42 AuthorLargeImage &#61; doc.Get("AuthorLargeImage"),
43 Statuses &#61; doc.Get("Statuses")
44 };
45
46 authorList.Add(author);
47 }
48 searcher.Dispose();
49
50
51
52 searchPeopleView.PageInfo.CurrentPage &#61; page;
53 searchPeopleView.PageInfo.TotalCount &#61; collector.TotalHits;
54 searchPeopleView.PageInfo.TotalPage &#61; (int)Math.Ceiling(collector.TotalHits / (float)pagesize);// collector.TotalHits;
55 searchPeopleView.PeopleList &#61; authorList;
56
57
58 return searchPeopleView;
59 }
第三步 &#xff0c;分页展示
<div class&#61;"row"><h3 id&#61;"people" class&#61;"page-header">人物 <small>共&#64;(Model.PeopleView.PageInfo.TotalCount)条记录,当前第&#64;(Model.PeopleView.PageInfo.CurrentPage)页small>h3><ul class&#61;"media-list">&#64;foreach (var p in Model.PeopleView.PeopleList){<li class&#61;"media"><div class&#61;"pull-left"><a href&#61;"/people/&#64;p.AuthorId"><img width&#61;"50" alt&#61;"&#64;Html.Raw(p.AuthorName)" src&#61;"&#64;p.AuthorSmallImage" />a>div><div class&#61;"media-body"><h4 class&#61;"media-heading"><span>[人物] &#64;(p.Dynasty)▪span><a href&#61;"/people/&#64;p.AuthorId">&#64;Html.Raw(&#64;p.AuthorName)a> h4><p>&#64;Html.Raw(p.AuthorSummary)p><a href&#61;"/people/&#64;p.AuthorId">www.guoxuetow.com/people/&#64;p.AuthorIda>div>li>}&#64;if (Model.PeopleView.PeopleList.Count() &#61;&#61; 0){<li class&#61;"media"><h4 class&#61;"media-heading">没有人物记录。h4>li>}ul><ul id&#61;"author-pagination" class&#61;"pagination-sm">ul><script type&#61;"text/Javascript">var totlaapage &#61; parseInt(&#39;&#64;(Model.PeopleView.PageInfo.TotalPage)&#39;);var apage &#61; parseInt(&#39;&#64;(Model.PeopleView.PageInfo.CurrentPage)&#39;);$(&#39;#author-pagination&#39;).twbsPagination({totalPages: totlaapage,startPage: apage,visiblePages: 20,href: &#39;&#64;Html.Raw(&#64;authorPageUrl)&#39;,first: &#39;首页&#39;,prev: &#39;上一页&#39;,next: &#39;下一页&#39;,last: &#39;末页&#39;,onPageClick: function (event, page) {window.location.href &#61; "&#64;Html.Raw(&#64;authorPageUrl1)" &#43; page;// $(&#39;#page-content&#39;).text(&#39;Page &#39; &#43; page);
}});script>div>
晚上回家再写.