Simon Green's Developer Blog
Developing .NET in the cold white north ...

NHibernate.Search using Lucene.NET Full Text Index (Part 2)

Sunday, 30 March 2008 10:16 by simon

In NHibernate.Search using Lucene.NET Full Text Index (Part 1) we looked at setting up the NHibernate.Search extension to add full-text searching of -persisted objects.

Next, we'll look at how we can perform Google-like searches using the index and some tips on displaying the results including highlighting the search-terms.

Our Book class has the Title, Summary, Authors and Publisher field indexed so we'll allow searching in any of these fields. However, if a search-term exists in the title it is probably more relevant than if it just exists in the summary so we want to give more priority to certain fields than to others. Likewise, we probably want to be able to specify which fields to search on otherwise we would get books that make mention of "Martin Fowler" in the summary whereas we may want to only see books that have "Martin Fowler" as an author for example.

Also worth mentioning is the Summary field. In the Book class there is a SummaryHtml field which (you'll never guess) contains the Html summary retrieved from Amazon and also a Summary field which is the one that is actually indexed. In the full app this text field is generated from the Html content using the . The reason we want a version of the Summary in plain text is to make indexing easier / more accurate (no HTML tags) and also to allow result fragments to be created: imagine if a section of the SummaryHtml was output - it could potentially split across an Html element or attribute (producing invalid markup) or include the opening tag but not the matching closing one (producing runaway bold-text for instance).

Back to our example though. To be able to show the highlighted search terms in the results I found it easier to create a special BookSearchResult class that I can return from the data provider - the highlighting is something Lucene.NET can do for us and avoids us having to write our own presentation code to handle it. Here is the class:

/// <summary>
/// A wrapper for a book object returned from a full text index query
/// with additional properties for highlighted segments
/// </summary> public class BookSearchResult : IBookSearchResult { private readonly IBook _book; private string _highlightedTitle; private string _highlightedSummary; private string _highlightedAuthors; private string _highlightedPublisher; /// <summary> /// Initializes a new instance of the <see cref="BookSearchResult"/> class. /// </summary> /// <param name="book">The book.</param> public BookSearchResult(IBook book) { _book = book; } /// <summary> /// Gets the book. /// </summary> /// <value>The book.</value> public IBook Book { get { return _book; } } /// <summary> /// Gets or sets the highlighted title. /// </summary> /// <value>The highlighted title.</value> public string HighlightedTitle { get { if (_highlightedTitle == null || _highlightedTitle.Length == 0) { return _book.Title; } return _highlightedTitle; } set { _highlightedTitle = value; } } /// <summary> /// Gets or sets the highlighted summary. /// </summary> /// <value>The highlighted summary.</value> public string HighlightedSummary { get { if (_highlightedSummary == null || _highlightedSummary.Length == 0) { if (_book.Summary == null || _book.Summary.Length < 300) { return _book.Summary; } else { return _book.Summary.Substring(0,300) + " ..."; } } return _highlightedSummary; } set { _highlightedSummary = value; } } /// <summary> /// Gets or sets the highlighted authors. /// </summary> /// <value>The highlighted authors.</value> public string HighlightedAuthors { get { if (_highlightedAuthors == null || _highlightedAuthors.Length == 0) { return _book.Authors; } return _highlightedAuthors; } set { _highlightedAuthors = value; } } /// <summary> /// Gets or sets the highlighted publisher. /// </summary> /// <value>The highlighted publisher.</value> public string HighlightedPublisher { get { if (_highlightedPublisher == null || _highlightedPublisher.Length == 0) { return _book.Publisher; } return _highlightedPublisher; } set { _highlightedPublisher = value; } } }

 

You'll notice that the Highlighted... fields return the equivalent book field if the highlighted field does not exist. This just saves us having to check whether there is a highlighted term in each field when we're building the search result list.

Our data provider will accept a single string consisting of the entered search-terms and return a list of BookSearchResult objects that match. Here is the code and I'll then try and explain what it's doing:

/// <summary>
/// Finds the books.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public override IList<IBookSearchResult> FindBooks(string query)
{
    IList<IBookSearchResult> results = new List<IBookSearchResult>();

    Analyzer analyzer = new SimpleAnalyzer();
    MultiFieldQueryParser parser = new MultiFieldQueryParser(
new string[] { "Title", "Summary", "Authors", "Publisher"},
analyzer); Query queryObj; try { queryObj = parser.Parse(query); } catch (ParseException) { // TODO: provide feedback to user on failed search expressions return results; } IFullTextSession session = (IFullTextSession) NHibernateHelper.GetCurrentSession(); IQuery nhQuery = session.CreateFullTextQuery(queryObj, new Type[] {typeof (Book) } ); IList<IBook> books = nhQuery.List<IBook>(); IndexReader indexReader = IndexReader.Open(SearchFactory.GetSearchFactory(session)
.GetDirectoryProvider(typeof (Book)).Directory); Query simplifiedQuery = queryObj.Rewrite(indexReader); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b class='term'>", "</b>"); Highlighter hTitle = GetHighlighter(simplifiedQuery, formatter, "Title", 100); Highlighter hSummary = GetHighlighter(simplifiedQuery, formatter, "Summary", 200); Highlighter hAuthors = GetHighlighter(simplifiedQuery, formatter, "Authors", 100); Highlighter hPublisher = GetHighlighter(simplifiedQuery, formatter, "Publisher", 100); foreach(IBook book in books) { IBookSearchResult result = new BookSearchResult(book); TokenStream tsTitle = analyzer.TokenStream("Title",
new System.IO.StringReader(book.Title ?? string.Empty)); result.HighlightedTitle = hTitle.GetBestFragment(tsTitle, book.Title); TokenStream tsAuthors = analyzer.TokenStream("Authors",
new System.IO.StringReader(book.Authors ?? string.Empty)); result.HighlightedAuthors = hAuthors.GetBestFragment(tsAuthors, book.Authors); TokenStream tsPublisher = analyzer.TokenStream("Publisher",
new System.IO.StringReader(book.Publisher ?? string.Empty)); result.HighlightedPublisher = hPublisher.GetBestFragment(tsPublisher, book.Publisher); TokenStream tsSummary = analyzer.TokenStream("Summary",
new System.IO.StringReader(book.Summary ?? string.Empty)); result.HighlightedSummary = hSummary.GetBestFragments(tsSummary,
book.Summary, 3, " ... <br /><br /> ... "); results.Add(result); } return results; } /// <summary> /// Gets the highlighter for the given field. /// </summary> /// <param name="query">The query.</param> /// <param name="formatter">The formatter.</param> /// <param name="field">The field.</param> /// <param name="fragmentSize">Size of the fragment.</param> /// <returns></returns> private static Highlighter GetHighlighter(Query query, Formatter formatter,
string field, int fragmentSize) { // create a new query to contain the terms BooleanQuery termsQuery = new BooleanQuery(); // extract terms for this field only WeightedTerm[] terms = QueryTermExtractor.GetTerms(query, true, field); foreach (WeightedTerm term in terms) { // create new term query and add to list TermQuery termQuery = new TermQuery(new Term(field, term.GetTerm())); termsQuery.Add(termQuery, BooleanClause.Occur.SHOULD); } // create query scorer based on term queries (field specific) QueryScorer scorer = new QueryScorer(termsQuery); Highlighter highlighter = new Highlighter(formatter, scorer); highlighter.SetTextFragmenter(new SimpleFragmenter(fragmentSize)); return highlighter; }
 

First, we parse the user-entered query string indicating that we want to match on the fields Title, Summary, Authors and Publisher using the MultiFieldQueryParser. This turns the user entered search expression into Lucene specific instructions. Most users when searching will enter a simple expression containing the words or phrase that they want to find. If the search term "XML' is entered for example Lucene will convert this into the expression "Title:XML Summary:XML Authors:XML Publisher:XML" which effectively means "find any record where 'XML' exists in any of the fields".

The user can enter specific instructions directly such as "Title:Architecture Authors:Fowler" which means "Find any books that have 'Architecture' in the Title field or 'Fowler' in the Authors field". Boolean expressions can be used to control this further allowing "(Title:Architecture) AND (Authors:Fowler)" to find any books titled 'Architecture' authored by 'Fowler'. When specific searches like this have been entered then the MultiFieldQueryParser doesn't expand the search to include all fields (except for un-field-prefixed words and phrases).

Incidentally, in the original Book class we included attributes to control the indexing such as [Boost(10)] for the Title. This boosts the relevance of searches on certain fields so a search for 'XML' in the Title and Summary of a document will rank books with 'XML' in the Title higher than books that have 'XML' in the summary - they are more likely to be what the user is searching for in this case.

Lucene does provide many other ways to define a query but this is simple and easy for this example.

Once we have our Lucene query object we use this to create an NHibernate.Search full-text query to return Book objects. This is where NHibernate and Lucene meet (from a querying point of view). It is possible to combine full-text-queries of Lucene with NHibernate queries of the database - NHibernate.Search handles the searching and returns the relevant objects.

So, we now have a list of Book objects just the same as if it had come directly from NHibernate except that the results are in order based on the rank provided by the Lucene search.

Now, we'll use another part of Lucene to highlight the matches. This is done using the SimpleHTMLFormatter, QueryScorer and Highlighter objects which combined allow us to get a fragment for each field with the search terms highlighted.

Note that the SimpleHtmlFormatter class is not in the main Lucene.Net.dll assembly but instead in a separate contrib assembly called Highlighter.Net.dll - there are also some other interesting utilities worth exploring in the contrib folder of the Lucene.NET distribution. Remember in Part 1 I mentioned that I had problems with assembly references and different versions of Lucene.Net.dll being used by NHibernate.Search so if you have problems building the solution after adding references to these contrib assemblies, consider building NHibernate.Search making sure that it references the same Lucene.Net.dll as the Lucene contrib assemblies were built against.

The Highlighter object for each field has to be based on the query terms for that field only so the original query is re-written and split up so that only the terms searched for that field are used. This isn't strictly necessary but I think it makes more sense if when you search for 'Microsoft' in the Title of a book only that occurrences of 'Microsoft' in the Summary or Publisher fields are not highlighted: the highlighted results then show clearly which found terms influenced the results. I have split this functionality into a separate GetHighlighter() method.

For example, without doing this a search for 'Title:Microsoft' incorrectly highlights the occurrences of 'Microsoft' found within the Author, Publisher and Summary fields even though they did not really contribute to the Book being included in the results or it's rank within them:

highlight_wrong

By creating the proper Highlighter for each field based on the terms used to search it the search results can be shown correctly without highlighting the un-searched fields / terms:

highlight_correct

Also, not that the fragments produced for the Summary are different - if a separate terms are used for the Title and Summary then having the Title terms highlighted in the Summary would possibly produce incorrect or sub-standard fragments.

 

Having built our Highlighters we can then iterate over the results creating a BookSearchResult to wrap each book in the result set. The same analyzer used in the initial query is then used to get a TokenStream for each field which the Highlighter instance needs to create the highlighted fragment from.

For the Title, Authors and Publisher fields we return a single Fragment which will normally be the field itself with the highlighted search terms wrapped in <b class='term'> ... </b> Html tags (courtesy of the SimpleHtmlFormatter class). The highlighted Summary is set to the best 3 fragments separated by '... <br /><br /> ... '. However big the summary is this ensures that the results contain a similar sized chunk of text with the best fragments shown (those containing the most highlighted terms).

Here is an example of the results for 'Title:Software Summary:Requirements Authors:Steve' after formatting and CSS applied to show the highlighted terms in yellow:

search_results

 

Lucene.NET can do a lot more than I've shown here. I found the best resource for learning about how to use it is the 'Lucene in Action' book:

Lucene in Action (In Action series)
by Otis Gospodnetic, Erik Hatcher

Read more about this book...

Note that this covers the Java version but applies equally well to the .NET port which is practically identical.

 

I hope this has been useful. In Part 3 I'll try and demonstrate using the Lucene.NET index to find similar items based on the frequency of shared terms. This can be used to provide 'other books you may like' or 'blog posts like this one' type functionality.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:   ,
Categories:   .NET
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Use Aliases to develop against SQL Server on different machines

Thursday, 13 March 2008 08:11 by Simon

This is a little tip that I've found useful when working on projects on different machines.

If you have a desktop machine and separate database server then you generally wouldn't need to have SQL server running locally - either the full version OR the SQL Express edition.

So, within your app the connection string would reference the name of the server, e.g.:

<connectionStrings>
  <add name="Library" connectionString="Data Source=MyServer;Initial Catalog=Library;Integrated Security=False;User ID=library;Password=secret;" providerName="System.Data.SqlClient"/>
 </connectionStrings>

 

The problem is of course when you checkout this code on another machine such as a laptop when working on-the-road (or just down in the basement while watching an episode of 'Lost' Smile)

Sure, you can just change the config to say '(local)' or '(local)\SQL2005' or whatever ... but you run into issues with the file being changed and then having to change it back if you check it in.

Urgh.

The simplest solution I've found is to setup an Alias using the SQL Server Configuration Manager:

  • On the desktop machine, setup an alias called 'dbserver' pointing to the proper database server.
  • On the laptop machine, setup the same alias called 'dbserver' this time pointing to the local instance.

Now, the same connection string can be run on both machines (using 'Data Source=dbserver' in the connection string) without having to worry about changing it when checking it out and not checking it in if you changed it.

NOTE: I usually generate the database schema from the C# classes and NHibernate mapping file and include a data-setup tool so it isn't an issue having two separate databases and normally most of the work is on the actual application and not so much on the database schema.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:  
Categories:   .NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

NHibernate.Search using Lucene.NET Full Text Index (Part 1)

Monday, 10 March 2008 09:36 by Simon

Ayende added the NHibernate.Search last year but I've never seen a great deal of documentation or examples around it so hopefully this post will help others to get started with it.

Basically, this addition to NHibernate brings two of the best open source libraries together - NHibernate as the Object Relational Mapper that persists your objects to a database and Lucene.NET which provides full-text indexing and query support.

So how do you use it?

The first problem you will run into is actually finding it. Unfortunately the release of NHibernate does not include it in the \bin although it is there in the source. Download the latest version of the NHibernate source (1.2.1 GA as of writing) and compile it to produce the NHibernate.Search.dll assembly.

Before you do this though, you may want to also download the latest Lucene.NET release (2.0.004) and replace the Lucene.NET.dll assembly in the NHibernate \lib\net\2.0 folder (I'm assuming you are using .NET 2.0). While the Lucene.NET library has the same version number and did work fine, the sizes are different and I ran into some problems when trying to use some of the extra Lucene.NET assemblies for hit-highlighting and similarity matching.

The first step is of course to add a reference to NHibernate.Search.dll to your Visual Studio.NET Project.

Next, you need to add some additional properties to the session-factory element of the NHibernate configuration section(normally stored in your web.config file):

<property name="hibernate.search.default.directory_provider">NHibernate.Search.Storage.FSDirectoryProvider, NHibernate.Search</property><property name="hibernate.search.default.indexBase">~/Index</property>

 

If you've used Lucene.NET much you will know that it has the concept of different directory providers for storing the indexed such as RAM or FS (File System). The entries above are used to indicate that we want the Lucene index to be stored on the file system and located in the /Index folder of the website (it could of course be outside the website mapped folder). It's well worth reading a book such as Lucene in Action to get a good idea of how Lucene works and what it can do (it's for the Java version but is still excellent for learning the .NET implementation).

The next step requires that you decorate your C# class with some attributes to control the indexing operation. Personally, I don't like this as it means I need to start referencing NHibernate and Lucene assemblies from my otherwise nice, clean POCO (Plain Old CLR/C# Classes) project. It would have been much nicer IMO if this information could have been put in the NHibernate .hbm.xml mapping files but it's a small price to pay and some people already use the attribute approach for NHibernate anyway.

Here is an example of a Book class for a library application with the additional attributes:

[Indexed(Index = "Book")] public class Book : IBook {     private Guid _id;     private string _title;     private string _summary;     private string _summaryHtml;     private string _authors;     private string _url;     private string _smallImageUrl;     private string _mediumImageUrl;     private string _largeImageUrl;     private string _isbn;     private string _published;     private string _publisher;     private string _binding;     [DocumentId]     [FieldBridge(typeof(GuidBridge))]     public Guid Id     {         get { return _id; }         set { _id = value; }     }     [Field(Index.Tokenized, Store = Store.No)]     [Analyzer(typeof(StandardAnalyzer))]     [Boost(2)]     public string Title     {         get { return _title; }         set { _title = value; }     }     [Field(Index.Tokenized, Store = Store.No)]     [Analyzer(typeof(StandardAnalyzer))]     public string Summary     {         get { return _summary; }         set { _summary = value; }     }     public string SummaryHtml     {         get         {             if (_summaryHtml == null || _summaryHtml.Length == 0)             {                 return _summary;             }             return _summaryHtml;         }         set { _summaryHtml = value; }     }     [Field(Index.Tokenized, Store = Store.No)]     [Analyzer(typeof(StandardAnalyzer))]     public string Authors     {         get { return _authors; }         set { _authors = value; }     }     public string Url     {         get { return _url; }         set { _url = value; }     }     public string SmallImageUrl     {         get { return _smallImageUrl; }         set { _smallImageUrl = value; }     }     public string MediumImageUrl     {         get { return _mediumImageUrl; }         set { _mediumImageUrl = value; }     }     public string LargeImageUrl     {         get { return _largeImageUrl; }         set { _largeImageUrl = value; }     }     [Field(Index.UnTokenized, Store = Store.Yes)]     public string Isbn     {         get { return _isbn; }         set { _isbn = value; }     }     [Field(Index.UnTokenized, Store = Store.No)]     public string Published     {         get { return _published; }         set { _published = value; }     }     [Field(Index.Tokenized, Store = Store.No)]     [Analyzer(typeof(StandardAnalyzer))]     public string Publisher     {         get { return _publisher; }         set { _publisher = value; }     }     public string Binding     {         get { return _binding; }         set { _binding = value; }     } } 

Now we're ready to start using it from NHibernate. To do this we need to create a FullTextSession and use this instead of the regular NHibernate Session (which it wraps / extends):

ISession session = sessionFactory.OpenSession(new SearchInterceptor());IFullTextSession fullTextSession = Search.CreateFullTextSession(session);

 

And that's it. You can use the IFullTextSession in place of the regular ISession (even casting it for places where you are just doing normal NHibernate operations). All the magic happens inside NHibernate.Search - when you add, update or delete records the 'documents' in the Lucene index are automatically updated which provides you with an excellent Full Text index without a Windows Service in sight!

You can check that it's working by looking in the Index folder - there should be a 'Book' folder containing the Lucene index files (with CFS extensions).

In the next post I'll demonstrate using the index to do some queries including hit-highlighting for presenting the results but for now you may want to download and try Luke - a Java program to browser Lucene index catalogs (the file format is identical between the two implementations).

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET
Actions:   E-mail | del.icio.us | Permalink | Comments (2) | Comment RSSRSS comment feed