Roadkill.Core.Services.SearchService.Add C# (CSharp) Method

Add() public method

Adds the specified page to the search index.
An error occured with the lucene.net IndexWriter while adding the page to the index.
public Add ( PageViewModel model ) : void
model Roadkill.Core.Mvc.ViewModels.PageViewModel The page to add.
return void
		public virtual void Add(PageViewModel model)
		{
			try
			{
				EnsureDirectoryExists();

				StandardAnalyzer analyzer = new StandardAnalyzer(LUCENEVERSION);
				using (IndexWriter writer = new IndexWriter(FSDirectory.Open(new DirectoryInfo(IndexPath)), analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED))
				{
					Document document = new Document();
					document.Add(new Field("id", model.Id.ToString(), Field.Store.YES, Field.Index.ANALYZED));
					document.Add(new Field("content", model.Content, Field.Store.YES, Field.Index.ANALYZED));
					document.Add(new Field("contentsummary", GetContentSummary(model), Field.Store.YES, Field.Index.NO));
					document.Add(new Field("title", model.Title, Field.Store.YES, Field.Index.ANALYZED));
					document.Add(new Field("tags", model.SpaceDelimitedTags(), Field.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("createdby", model.CreatedBy, Field.Store.YES, Field.Index.ANALYZED));
					document.Add(new Field("createdon", model.CreatedOn.ToShortDateString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
					document.Add(new Field("contentlength", model.Content.Length.ToString(), Field.Store.YES, Field.Index.NO));
                    document.Add(new Field("projectstart", model.ProjectStart.ToString("yyyyMMdd"), Field.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("projectend", model.ProjectEnd.ToString("yyyyMMdd"), Field.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("projectestimatedtime", model.ProjectEstimatedTime.ToString(), Field.Store.YES, Field.Index.NO));
                    document.Add(new Field("projectlanguage", model.ProjectLanguage, Field.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("projectstatus", model.ProjectStatus, Field.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("orgID", model.orgID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));


					writer.AddDocument(document);
					writer.Optimize();
				}
			}
			catch (Exception ex)
			{
				if (!ApplicationSettings.IgnoreSearchIndexErrors)
					throw new SearchException(ex, "An error occured while adding page '{0}' to the search index", model.Title);
			}
		}

Usage Example

Example #1
0
		public void Search_With_No_Field_Returns_Results()
		{
			// Arrange
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "title content", "tag1", "title content1");
			PageViewModel page2 = CreatePage(2, "admin", "title content", "tag1", "title content2");

			searchService.Add(page1);
			searchService.Add(page2);

			// Act
			List<SearchResultViewModel> results = searchService.Search("title content").ToList();

			// Assert
			Assert.That(results.Count, Is.EqualTo(2)); // Lucene will ignore the 1 and 2 in the content
		}
All Usage Examples Of Roadkill.Core.Services.SearchService::Add