views:

135

answers:

1

When I was using Lucene to index my entities, I had the habit of putting all my indexed properties in a field named "all", to perform a search on "all" of my entities types.

Now, using NHibernate.Search, I can't find how to do this. I tried this :

[Indexed(Index = "MyIndex")]
public class Post
{
    [DocumentId]
    public virtual int Id { get; set; }
    [IndexedEmbedded]
    public virtual Author Author { get; set; }
    [IndexedEmbedded]
    public virtual IEnumerable<Category> Categories { get; set; }
    [Field(Index.Tokenized, Store = Store.Yes)]
    [Field(Name = "All", Index = Index.Tokenized, Store = Store.Yes)]
    public virtual string Name { get; set; }
    [Field(Name = "All", Index = Index.Tokenized, Store = Store.Yes)]
    [Field(Index.Tokenized, Store = Store.Yes)]
    public virtual string Body { get; set; }
}

But I have an exception thrown : "key already present in dictionary", in ScopedAnalyzer.cs line 26 :

scopedAnalyzers.Add(scope, analyzer);

Where "scope" is the name of the index field (here, "All"). If I put a check like

if( !scopedAnalyzers.ContainsKey( scope ) )

it will work quite well : I will have 2 fields for each "Post" document, one with the body, one with the name. However, I'm not at easy modifying NHibernate.Search source code.

Anyone got a suggestion on how to index different properties in one field ?

+1  A: 

If you are having trouble using the Field attributes to specify what you need you can create a class bridge that will allow you to create your own document.

just use the ClassBridgeAttribute on the class definition and specify an type that implements the IFieldBridge interface. This will allow you to have complete control over how the Lucene Documents get built.

more information can be found at http://docs.jboss.org/hibernate/stable/search/reference/en/html/search-mapping-bridge.html see the 4.2.2.3. ClassBridge section

Andrew Smith