views:

245

answers:

2

I'm trying to find out how to programmatically (i.e. without using the FieldAttribute) add an index column for NHibernate Search (Lucene.net).

I'm having inheritance issues due to the fact that the FieldAttribute is not automatically inherited.

The following code illustrates what I want to do.

class A
{
    [Field(Index.Tokenized)]
    public virtual string P1
    {
        get
        {
            return "P1";
        }
    }
}

class B : A
{
    public override string P1
    {
        get
        {
            return "P1+";
        }
    }
}

I expected the override of P1 to be indexed but it didn't. When I inspected the FieldAttribute class, I found that it didn't have Inherited = true specified in the AttributeUsage attribute.

I then added a FieldAttribute to the overridden property but that resulted in NHibernate Search bailing out with an exception stating that an item with the same key has already been added to a dictionary. I figure that's because there's two equally named properties both with a FieldAttribute on them in the type chain and it accepts only one.

So, how do I programmatically solve this by not using FieldAttribute?

A: 

Non-attribute mapping was recently implemented, take a look at this blog post.

Mauricio Scheffer
Great, I didn't catch that edit but I'm delighted to see that patch got in!
Sandor Drieënhuizen
+1  A: 

I've just started a Fluent NHibernate.Search mapping interface similar to FluentNHibarnate, which allow you to map your entities without attributes.

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

        Map(x => x.Title)
            .Analyzer<StandardAnalyzer>()
            .Boost(500);

        Map(x => x.Description)
            .Boost(500)
            .Name("Description")
            .Store().Yes()
            .Index().Tokenized();
    }
}

You should take a look on the project site hosted on codeplex.

http://fnhsearch.codeplex.com/

Yoann. B