[assembly: WebActivator.PostApplicationStartMethod(typeof(Blog.Web.App_Start.LuceneSearchConfig), "InitializeSearch")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(Blog.Web.App_Start. LuceneSearchConfig),"FinalizeSearch")] namespace Blog.Web.App_Start { using Blog.Domain.Service; using Lucene.Net.Analysis; using Lucene.Net.Analysis.Standard; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.QueryParsers; using Lucene.Net.Search; using Lucene.Net.Store; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; public class LuceneSearchConfig { public static Directory directory; public static Analyzer analyzer; public static IndexWriter writer; public static void InitializeSearch() { string directoryPath = AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\LuceneIndexes"; directory = FSDirectory.Open(directoryPath); analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); Task.Factory.StartNew(() => CreateIndex()); } private static void CreateIndex() { IBlogService blogService = new BlogService(repository); var pages = blogService.GetAllPages(); Document doc; string pageUrl = string.Empty; string pagePath = string.Empty; foreach (var page in pages) { doc = new Document(); pageUrl = string.Concat("~/Blog/", page.PageViewName); pagePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, @"\Views\MyBlog\", page.PageViewName, ".cshtml"); doc.Add(new Field("postUrl", pageUrl, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("postTitle", page.PageTitle, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("postBody", pagePath, Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); doc = null; } writer.Optimize(); writer.Commit(); writer.Dispose(); } public static void FinalizeSearch() { directory.Dispose(); } } }
string directoryPath = AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\LuceneIndexes"; directory = FSDirectory.Open(directoryPath); analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
doc = new Document(); pageUrl = string.Concat("~/Blog/", page.PageViewName); pagePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory,@"\Views\MyBlog\", page.PageViewName, ".cshtml"); doc.Add(new Field("postUrl", pageUrl, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("postTitle", page.PageTitle, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("postBody", pagePath, Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc);
writer.Optimize(); writer.Commit(); writer.Dispose();
public ActionResult Search(FormCollection formCollection) { List <SearchResult> searchResults = new List <SearchResult>(); var query = formCollection["search"]; string indexDirectory = Server.MapPath("~/App_Data/LuceneIndexes"); var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(indexDirectory)); var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "postBody", analyzer); Query searchQuery = parser.Parse(query); TopDocs hits = searcher.Search(searchQuery, 200); int results = hits.TotalHits; SearchResult searchResult = null; for (int i = 0; i < results; i++) { Document doc = searcher.Doc(hits.ScoreDocs[i].Doc); searchResult = new SearchResult(); searchResult.PostUrl = doc.Get("postUrl"); searchResult.PostTitle = doc.Get("postTitle"); searchResult.PostIntro = doc.Get("postIntro"); searchResults.Add(searchResult); searchResult = null; } return View(searchResults); } public class SearchResult { public string PostUrl { get; set; } public string PostTitle { get; set; } public string PostIntro { get; set; } }
Share this page on
5
People Like(s) This Page
Permalink
comments powered by Disqus