Knowledgebase Home Page  >  SearchUnit
Search the Knowledge Base
Search with MVC (C#)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=266&catId=54

Options

Print this page
Email this to a friend

This article is relevant to Search 2012 and older.  SearchUnit v6 upwards includes a JavaScript based client interface that works with MVC out of the box.

Please download this simplified example project : http://keyoti.com/downloads/SearchMVCExample1.zip

(It includes a small document index built on the http://asp.net website, for demonstration purposes)

 

The project uses MVC Razor as the display engine, but should be easily adaptable to non Razor applications.  Basic search is demonstrated here as a starting point, to implement more features please email support @ keyoti.com

 

How it works

 

SearchController

 

Uses the SearchAgent API in the core Keyoti Search DLL to generate a SearchResult list that can be iterated over in the view.

ActionResult CreateView(string query, int? page){

            string licenseKey = "5A556A61675D69626B5C6D6C64485426403C453A3F403D423F4544464A444E4A539";//PRO license key, expires Jan 11, 2014

            //string licenseKey = "5752675E645A665F68596A69615A66383C3F3C3D383D3E3B403D43424448424B4E4B4C2";//LITE license key, expires Jan 11, 2014

            ViewData["Query"] = query;

            Keyoti.SearchEngine.ConfigurationManager configManager = new Keyoti.SearchEngine.ConfigurationManager(System.Web.HttpContext.Current.Server.MapPath("~/Content/IndexDirectory"));

            Keyoti.SearchEngine.Configuration configuration = new Keyoti.SearchEngine.Configuration();

            configManager.RetrieveConfiguration(configuration);

            Keyoti.SearchEngine.Search.SearchAgent searchAgent = new Keyoti.SearchEngine.Search.SearchAgent(licenseKey, configuration);

            int pageNumber = (page ?? 1);

            ViewData["PageNumber"] = pageNumber;

            int pageSize = 10;

            ViewData["PageSize"] = pageSize;

 

            Keyoti.SearchEngine.Search.SearchResult result = searchAgent.Search(query, 1+(pageSize * (pageNumber - 1)), pageSize * pageNumber);

 

            if (result.NumberOfResults > 0)

            {

                CreatePagingLinks(result, pageSize, pageNumber);

                return View(result);

            }

            else return View("NoResults");

        }

 

 

Search View

 

Iterates over the contents of the SearchResult list.

@model Keyoti.SearchEngine.Search.SearchResult

 

@section Styles {

    <link href="@Url.Content("~/Content/KeyotiSearch.css")" rel="stylesheet" type="text/css" />

}

 

@{

    ViewBag.Title = "Search";

}

 

<h2>Search</h2>

 

You searched for: @ViewData["Query"]

@(Model.IgnoredWords.Count>0?"The following common words were ignored; "+Model.IgnoredWords.ToCommaDelimitedString():"")

 

<form name=search>

<table>

    <tr>

    <td>

        <input type=text name=query size=22>

    </td>

    <td>

        <input type=submit value="Search">

    </td>   

    </tr>

</table>

</form>

 

<table>

    <tr>

        <th></th>

    </tr>

 

@foreach (Keyoti.SearchEngine.Search.ResultItem item in Model) {

    <tr>

        <td>

            <a href="@item.UriString">

                @item.Title

            </a>

            <br />

            @item.Summary

        </td>

    </tr>

}

</table>

 

<div class="searchPagingControls">Page:

@Html.Raw(ViewData["PreviousPageLink"])

@Html.Raw(ViewData["PageLinksBlock"])

@Html.Raw(ViewData["NextPageLink"])

</div>

 

Again, for information on extending this with extra features please email support @ keyoti.com

 


Related Questions:

Attachments:

No attachments were found.