예제 #1
0
        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;
        }
예제 #2
0
        public IEnumerable<string> ListDocumentNames(DocumentRoot root)
        {
            var path = Path.Combine(this.Settings.GetRepositoryPath(), root.ToPath());

            return Directory.GetFiles(path, "*" + DocumentExtension)
                .Select(Path.GetFileName)
                .Select(f => f.Substring(0, f.Length - 3)) // strip file extension
                .ToList();
        }
예제 #3
0
        public MarkdownDocument GetDocument(DocumentRoot root, string name)
        {
            var rootPath = GetAbsoluteDocumentRootPath(root);
            var docPath = Path.Combine(rootPath, name + DocumentExtension);

            if (File.Exists(docPath) == false)
            {
                throw new Exception(string.Format("No document with the name {0} could be found. [{1}]", name, rootPath));
            }

            return new MarkdownDocument(root, name, File.ReadAllText(docPath, Encoding.UTF8));
        }
예제 #4
0
		private void Index(DocumentRoot root)
		{
			var indexPath = _settings.GetSearchIndexPath();
			var documents = _browser.ListDocuments(root);

			var directory = FSDirectory.Open(new DirectoryInfo(indexPath));
			var analyzer = new StandardAnalyzer(Version.LUCENE_30);

			using (var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED))
			{
				foreach (var doc in documents)
				{
					var luceneDoc = new Document();

					luceneDoc.Add(new Field("Title", doc.Title, Field.Store.YES, Field.Index.ANALYZED));
					luceneDoc.Add(new Field("Snippet", doc.Markdown.Cut(500), Field.Store.YES, Field.Index.NO));
					luceneDoc.Add(new Field("Body", doc.Markdown, Field.Store.NO, Field.Index.ANALYZED));

					writer.AddDocument(luceneDoc);
				}

				writer.Optimize();
			}
		}
예제 #5
0
 public MarkdownDocument(DocumentRoot root, string title, string markdown)
 {
     this.Root = root;
     this.Title = title;
     this.Markdown = markdown;
 }
예제 #6
0
 public IEnumerable<MarkdownDocument> ListDocuments(DocumentRoot root)
 {
     return ListDocumentNames(root).Select(name => GetDocument(root, name));
 }
예제 #7
0
 private string GetAbsoluteDocumentRootPath(DocumentRoot root)
 {
     return Path.Combine(this.Settings.GetRepositoryPath(), root.ToPath());
 }