Click here to Skip to main content
15,886,787 members
Articles / Programming Languages / C#

Lucene.Net ultra fast search for MVC or WebForms site => made easy!

Rate me:
Please Sign up or sign in to vote.
4.92/5 (138 votes)
22 Aug 2013CPOL17 min read 655.5K   12.1K   331  
Step-by-step tutorial for any developer who wishes to get Lucene.Net search working with their web site or app really quickly!
using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
using MvcLuceneSampleApp.Search;
using MvcLuceneSampleApp.ViewModels;

namespace MvcLuceneSampleApp.Controllers {
	public class HomeController : Controller {
		private IEnumerable<SampleData> _searchResults;

		public ActionResult Index(string searchTerm, string searchField, string type) {
			// create default Lucene search index directory
			if (!Directory.Exists(LuceneSearch._luceneDir)) Directory.CreateDirectory(LuceneSearch._luceneDir);

			// perform Lucene search
			if (string.IsNullOrEmpty(type))
				_searchResults = string.IsNullOrEmpty(searchField)
				                 	? LuceneSearch.Search(searchTerm)
				                 	: LuceneSearch.Search(searchTerm, searchField);
			else if (type == "default")
				_searchResults = string.IsNullOrEmpty(searchField)
				                 	? LuceneSearch.SearchDefault(searchTerm)
				                 	: LuceneSearch.SearchDefault(searchTerm, searchField);
			
			// setup and return view model
			var search_field_list = new
				List<SelectListItem> {
				                     	new SelectListItem {Text = "(All Fields)", Value = ""},
				                     	new SelectListItem {Text = "Id", Value = "Id"},
				                     	new SelectListItem {Text = "Name", Value = "Name"},
				                     	new SelectListItem {Text = "Description", Value = "Description"}
				                     };
			return View(new IndexViewModel {
			                               	AllSampleData = SampleDataRepository.GetAll(),
			                               	AllSearchIndexData = LuceneSearch.GetAllIndexRecords(),
			                               	SampleData = new SampleData {Id = 9, Name = "El-Paso", Description = "City in Texas"},
			                               	SampleSearchResults = _searchResults,
			                               	SearchFieldList = search_field_list,
			                               });
		}

		public ActionResult Search(string searchTerm, string searchField) {
			return RedirectToAction("Index", new {searchTerm, searchField});
		}

		public ActionResult SearchDefault(string searchTerm, string searchField) {
			return RedirectToAction("Index", new {type = "default", searchTerm, searchField});
		}

		public ActionResult CreateIndex() {
			LuceneSearch.AddUpdateLuceneIndex(SampleDataRepository.GetAll());
			TempData["Result"] = "Search index was created successfully!";
			return RedirectToAction("Index");
		}

		[HttpPost]
		public ActionResult AddToIndex(SampleData sampleData) {
			LuceneSearch.AddUpdateLuceneIndex(sampleData);
			TempData["Result"] = "Record was added to search index successfully!";
			return RedirectToAction("Index");
		}

		public ActionResult ClearIndex() {
			if (LuceneSearch.ClearLuceneIndex())
				TempData["Result"] = "Search index was cleared successfully!";
			else
				TempData["ResultFail"] = "Index is locked and cannot be cleared, try again later or clear manually!";
			return RedirectToAction("Index");
		}

		public ActionResult ClearIndexRecord(int id) {
			LuceneSearch.ClearLuceneIndexRecord(id);
			TempData["Result"] = "Search index record was deleted successfully!";
			return RedirectToAction("Index");
		}

		public ActionResult OptimizeIndex() {
			LuceneSearch.Optimize();
			TempData["Result"] = "Search index was optimized successfully!";
			return RedirectToAction("Index");
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Coding is awesome!

Comments and Discussions