Click here to Skip to main content
15,884,298 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 654.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

<div class="content_left">
  <span style="color: green;">@TempData["Result"]</span>
  <span style="color: red;">@TempData["ResultFail"]</span>
</div>
<div class="clear"></div>

<div class="col_12">		
  <fieldset>
	  <legend>Database records (@Html.ActionLink("Create Search Index From Database [+]", "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>	
    
    @if (ViewBag.Limit > 0) {
      <div class="margin_top10">And <b>@ViewBag.Limit</b> more records... (<a href="\?limit=0">See All</a>)</div>
    }
  </fieldset>
</div>

<div class="col_12">
  <fieldset>
	  <legend>Search (custom, useful for most basic scenarios)</legend>
    <div class="content_left margin_top5">
	    <p>Try these searches: <em>"1 3 5", "City", "Russia India", "bel mos ind"</em></p>
    </div>
    @using (Html.BeginForm("Search", "Home")) {
      <div class="content_left margin_top13 margin_left30">
        @Html.CheckBoxFor(m => m.SearchDefault)
        @Html.LabelFor(m => m.SearchDefault, "Use default Lucene query")
      </div>
	    <div class="content_left margin_right20">
		    @Html.TextBoxFor(m => m.SearchTerm, new { @class="big", style="width:650px;", autocomplete="off" })
      </div>
      <div class="content_left margin_top15 margin_right30">
		    @Html.DropDownListFor(m => m.SearchField, Model.SearchFieldList.Select(x => new SelectListItem { Text = x.Text, Value = x.Value }),
          new { style="width:150px;" })
      </div>
      <div class="content_right margin_top8">
		    <input type="submit" value="Search" />
      </div>
      <div class="clear"></div>
	  }
  </fieldset>
</div>
		
<div class="col_8">
  <fieldset>
	  <legend>
		  Search index
		  (@Html.ActionLink("Optimize", "OptimizeIndex"))
		  (@Html.ActionLink("Clear [X]", "ClearIndex"))
	  </legend> 
    
    @if (Model.SearchIndexData.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.SearchIndexData) {
          <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/>No search index records found...<br/></text>
    }
  </fieldset>
</div>

<div class="col_4">
  <fieldset class="add_record">
	  <legend>Add/Update search index record</legend>
    Use Id of existing one to update
	  @using (Html.BeginForm("AddToIndex", "Home")) {
      <div class="form_horizontal">
		    <p>
			    @Html.LabelFor(m => m.SampleData.Id)<br/>
			    @Html.TextBoxFor(m => m.SampleData.Id, new { style = "width:30px;" })
		    </p>
		    <p>
			    @Html.LabelFor(m => m.SampleData.Name)<br/>
			    @Html.TextBoxFor(m => m.SampleData.Name, new { style = "width:100px;" })
		    </p>
		    <p>
			    @Html.LabelFor(m => m.SampleData.Description)<br/>
			    @Html.TextBoxFor(m => m.SampleData.Description, new { style = "width:120px;" })
		    </p>
      </div>
      <div class="clear"></div>
		  <input type="submit" value="Add/Update Record" />
	  }
  </fieldset>
</div>

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