Click here to Skip to main content
15,886,578 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.4K   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!
@model MvcLuceneSampleApp.ViewModels.IndexViewModel
		
<fieldset class="content_left" style="width: 25%;">
	<legend>All records in database (@Html.ActionLink("Create Index [+]", "CreateIndex"))</legend> 
	<table class="grid">
		<tr>
			<th>@Html.LabelFor(m => Model.SampleData.Id)</th>
			<th>@Html.LabelFor(m => Model.SampleData.Name)</th>
			<th>@Html.LabelFor(m => Model.SampleData.Description)</th>
		</tr>
	@foreach (var record in Model.AllSampleData) {
		<tr>
			<td>@record.Id</td>
			<td>@record.Name</td>
			<td>@record.Description</td>
		</tr>
	}		
	</table>	
</fieldset>
		
<fieldset class="content_left" style="width: 30%;">
	<legend>
		All records in search index
		(@Html.ActionLink("Optimize", "OptimizeIndex"))
		(@Html.ActionLink("Clear [X]", "ClearIndex"))
	</legend> 
	@if (Model.AllSearchIndexData.Any()) {
		<table class="grid">
			<tr>
				<th>@Html.LabelFor(m => Model.SampleData.Id)</th>
				<th>@Html.LabelFor(m => Model.SampleData.Name)</th>
				<th>@Html.LabelFor(m => Model.SampleData.Description)</th>
				<th></th>
			</tr>
		@foreach (var record in Model.AllSearchIndexData) {
			<tr>
				<td>@record.Id</td>
				<td>@record.Name</td>
				<td>@record.Description</td>
				<td>@Html.ActionLink("Delete", "ClearIndexRecord", new { record.Id })</td>
			</tr>
		}		
		</table>
	} else {
		<text><br/>Index is empty...<br/></text>
	}
</fieldset>
		
<fieldset class="content_left" style="width: 30%;">
	<legend>Messages</legend>
	<span style="color: green;">@TempData["Result"]</span>
	<span style="color: red;">@TempData["ResultFail"]</span>
</fieldset>
<div class="clear"></div>

<fieldset style="width:45%;" class="add_record content_left">
	<legend>Add/Update search index record (use Id of existing one):</legend>
	@using (Html.BeginForm("AddToIndex", "Home")) {
		<p>
			@Html.LabelFor(m => m.SampleData.Id)<br/>
			@Html.EditorFor(m => m.SampleData.Id)
		</p>
		<p>
			@Html.LabelFor(m => m.SampleData.Name)<br/>
			@Html.EditorFor(m => m.SampleData.Name)
		</p>
		<p>
			@Html.LabelFor(m => m.SampleData.Description)<br/>
			@Html.EditorFor(m => m.SampleData.Description)
		</p>
		<input type="submit" value="Add/Update Record" />
	}
</fieldset>

<fieldset style="width:45%;" class="content_left">
	<legend>Tip:</legend>
	<div style="margin: 16px 30px 0 0; " class="content_left">
		<strong>Note:</strong><br/>
		if you don't like the ordering of Lucene results,
		you can always reorder them via <strong>LINQ</strong>:
	</div>
	<div class="content_left">
		<pre style="width: 200px;" class="cs">*.OrderBy(x => x.SMTH)</pre>
	</div>
	<div class="clear"></div>
</fieldset>		
<div class="clear"></div>

<fieldset style="width:45%;" class="content_left">
	<legend>Search (custom, useful for most basic scenarios)</legend>
	<p>Try these searches: <em>"1 3 5", "City", "Russia India", "bel mos ind"</em></p>

	@using (Html.BeginForm("Search", "Home")) {
		@Html.TextBoxFor(m => m.SearchTerm, new { style="width:50%;" })
		@Html.DropDownListFor(m => m.SearchField, Model.SearchFieldList)

		<input type="submit" value="Search" />
	}
</fieldset>

<fieldset style="width:45%;" class="content_left">
	<legend>Search (Lucene default, for query testing)</legend>
	<p>Try these search: <em>"bel* mos* ind*"</em></p>

	@using (Html.BeginForm("SearchDefault", "Home")) {
		@Html.TextBoxFor(m => m.SearchTerm, new { style="width:50%;" })
		@Html.DropDownListFor(m => m.SearchField, Model.SearchFieldList)

		<input type="submit" value="Search" />
	}
</fieldset>
<div class="clear"></div>
		
@if (Model.SampleSearchResults.Any()) {
	<fieldset>
		<legend>Search Results</legend>
		<table class="grid">
			<tr>
				<th>@Html.LabelFor(m => Model.SampleData.Id)</th>
				<th>@Html.LabelFor(m => Model.SampleData.Name)</th>
				<th>@Html.LabelFor(m => Model.SampleData.Description)</th>
			</tr>
		@foreach (var record in Model.SampleSearchResults) {
			<tr>
				<td>@record.Id</td>
				<td>@record.Name</td>
				<td>@record.Description</td>
			</tr>
		}		
		</table>
	</fieldset>
}

<script type="text/javascript">
	$(document).ready(function () {
		$('#SearchTerm').focus();
		$('.grid tr:even').css("background", "silver");

		// snippet code highlighter
		//$("pre.html").snippet("html", { style: "acid", clipboard: "../../Scripts/ZeroClipboard.swf", showNum: false });
		$("pre.cs").snippet("csharp", { style: "acid", clipboard: "../../Scripts/ZeroClipboard.swf", showNum: false });
		//$("pre.css").snippet("css", { style: "peachpuff", clipboard: "../../Scripts/ZeroClipboard.swf", showNum: false });
	});
</script>

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