Skip to content

Latest commit

 

History

History
174 lines (133 loc) · 5.64 KB

using-sharparchitecture-with-nhibernate.search-(lucene.net).rst

File metadata and controls

174 lines (133 loc) · 5.64 KB

S#arp Architecture with NHibernate.Search

Note

Do not use this with NHibernate Configuration Cache:

NHibernateSession.ConfigurationCache = new NHibernateConfigurationFileCache();

NuGet Install

Install-Package NHibernate.Search

(This used to be a big deal, building from source, etc.)

Configuring and Building the Index

Maintaining an index in Lucene is easy. There's a configuration setting to keep any deletes, inserts or updates current in the index, and a way to build the index from an existing dataset from scratch. You usually need both unless you're starting on a project from scratch, but even so, it is nice to have a way to build an index in case the existing index gets corrupted.

This example will assume you've created a folder in the root of your MVC project, and have given the ASP.NET security guy full permissions.

Open up your root Web.Config, add the following right above :

Right below add the following:

<nhs-configuration xmlns='urn:nhs-configuration-1.0'>
    <search-factory>
        <property  name="hibernate.search.default.indexBase">~\LuceneIndex</property>
    </search-factory>
</nhs-configuration>

Navigate to your NHibernate.Config, before add:

<listener class='NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search' type='post-insert'/>
<listener class='NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search' type='post-update'/>
<listener class='NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search' type='post-delete'/>

Add a Search Repository

In "Northwind.Infrastructure" add a repository called "ContractRepository.cs" (the example assumes you have an object you want to search over called contract):

Let's add a method to this repository that will create the initial index, if an index already exists, it will be deleted. We'll iterate through all the Suppliers to accomplish this:

Finally, we'll add a method to query the index:

Add Search Controller

  • Wire up a view to display the search results
  • Navigate to localhost:portnumber/ContractController/BuildSearchIndex
  • This will (quickly) build your index, it would be beneficial to pass status messages here
  • You should see a Suppliers folder in the LuceneIndex folder of the project
  • To verify the index, download Luke and point it to the LuceneIndex

Pre-Requisite Reading

I really recommend Hibernate Search in Action, you can really make queries do some neat things that aren't covered in this tutorial. It will, however, get you up and running quickly.