public SearchResults Search(DocumentRoot root, string term) { var results = new SearchResults(); var indexPath = _settings.GetSearchIndexPath(); var version = Lucene.Net.Util.Version.LUCENE_30; using (var directory = FSDirectory.Open(new DirectoryInfo(indexPath))) using (var indexReader = IndexReader.Open(directory, true)) using (var indexSearch = new IndexSearcher(indexReader)) { var analyzer = new StandardAnalyzer(version); var queryParser = new MultiFieldQueryParser(version, new[] { "Title", "Body" }, analyzer); var query = queryParser.Parse(term); var resultDocs = indexSearch.Search(query, indexReader.MaxDoc); var hits = resultDocs.ScoreDocs; foreach (var hit in hits) { var doc = indexSearch.Doc(hit.Doc); results.Documents.Add(new SearchResult { Score = hit.Score, Snippet = doc.Get("Snippet"), Title = doc.Get("Title") }); } } return results; }
public IndexModel(SearchResults results, string term) { this.Count = results.Hits; this.Term = term; this.Documents = results.Documents.Select(r => new DocumentModel { Url = r.Title.ToLower(), Title = r.Title.FormatTitleForDisplay(), SnippetHtml = r.Snippet.Highlight(term.Split(" "[0])), Score = Math.Round(r.Score, 4) }) .ToList(); }