Skip to content
pardahlman edited this page Sep 27, 2012 · 6 revisions

A Fluent NHibernate.Search Attributes-Less mapping interface for NHibernate provider implementation of Lucene.NET.

Current source code works against the following dependencies :

  • NHibernate : 3.2.0.4000
  • NHibernate.Search : Compiled from this repository against NH 3.2 and Lucene.NET 2.9.4.1
  • Lucene.NET : 2.9.4.1

Project Announcement

http://blog.sb2.fr/post/2010/03/05/Introducing-FluentNhibernateSearch-Mapping-Interface.aspx

0.3 Beta Announcement

http://blog.sb2.fr/post/2010/03/08/FluentNHibernarteSearch-03-Beta-Released-Now-Support-Fluent-XML-Less-Configuration.aspx

0.2 Beta Announcement

http://blog.sb2.fr/post/2010/03/07/FluentNHibernarteSearch-02-Beta-Released.aspx

Simple Mapping Sample

public class BookSearchMap : DocumentMap<Book>
{
    public BookSearchMap()
    {
        Id(p => p.BookId).Bridge().Guid();
        Name("Book");

        Map(x => x.Title)
            .Store().Yes()
            .Index().Tokenized();

        Map(x => x.Description)
            .Store().Yes()
            .Index().Tokenized();
    }
}

Embedded Mapping Sample

public class AuthorSearchMap : DocumentMap<Author>
{
    public AuthorSearchMap()
    {
        Id(p => p.AuthorId).Bridge().Guid();
        Name("Author");

        Map(x => x.Name)
            .Store().Yes()
            .Index().Tokenized();

        Embedded(x => x.Books).AsCollection();
    }
}

Getting Started

Create Entity Mappings Classes

public class AuthorSearchMap : DocumentMap<Author>
{
    public AuthorSearchMap()
    {
        Id(p => p.AuthorId).Bridge().Guid();
        Name("Author");

        Map(x => x.Name)
            .Store().Yes()
            .Index().Tokenized();

        Embedded(x => x.Books).AsCollection();
    }
}

Create NHibernate.Search Mapping Factory

public class LibrarySearchMapping : SearchMapping
{
    protected override void Configure()
    {
        AddAssembly(Assembly.GetExecutingAssembly());
    }
}

Configure NHibernate.Search Fluently

Configuration nhcfg = FluentSearch.Configure()
    .DefaultAnalyzer().Standard()
    .DirectoryProvider().FSDirectory()
    .IndexBase("~/Index")
    .IndexingStrategy().Event()
    .MappingClass<LibrarySearchMapping>()
    .BuildConfiguration();

Configure NHibernate.Search atop FluentNHibernate

Configuration config = Fluently
	.Configure()
	.Database(SQLiteConfiguration.Standard.InMemory())
	.ExposeConfiguration(cfg =>
	{
		FluentSearch.Configure(cfg)
			.DefaultAnalyzer().Standard()
			.DirectoryProvider().RAMDirectory()
			.IndexingStrategy().Event()
			.MappingClass<SearchMap>();

		cfg.SetListeners(ListenerType.PostInsert, new[] {new FullTextIndexEventListener()});
		cfg.SetListeners(ListenerType.PostUpdate, new[] {new FullTextIndexEventListener()});
		cfg.SetListeners(ListenerType.PostDelete, new[] {new FullTextIndexEventListener()});

		cfg.SetListener(ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener());
		cfg.SetListener(ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener());
		cfg.SetListener(ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener());
	})
	.BuildConfiguration();