Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for custom bridge mapping as described by sadomovalex. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/FluentNHibernate.Search.Tests/Domain/Tag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FluentNHibernate.Search.Tests.Domain
{
public class Tag
{
public virtual Guid TagId { get; set; }
public virtual string Name { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/FluentNHibernate.Search.Tests/Domain/TestDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace FluentNHibernate.Search.Tests.Domain
{
Expand All @@ -7,5 +8,12 @@ public class TestDocument
public Guid Id { get; set; }

public string StringProperty { get; set; }

public ICollection<Tag> Tags { get; private set; }

public TestDocument()
{
Tags = new List<Tag>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
</Choose>
<ItemGroup>
<Compile Include="Domain\Address.cs" />
<Compile Include="Domain\Tag.cs" />
<Compile Include="Integration\Bridge\CustomBridge.cs" />
<Compile Include="Mappings\TagBridge.cs" />
<Compile Include="Mappings\TagDocumentMap.cs" />
<Compile Include="Unit\DocumentId\CustomName.cs" />
<Compile Include="Unit\DocumentId\NoOptions.cs" />
<Compile Include="Domain\Author.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Search.Tests.Mappings;
using FluentNHibernate.Search.Tests.Domain;
using FluentNHibernate.Search.Tests.Extensions;

namespace FluentNHibernate.Search.Tests.Integration.Bridge
{
public class CustomBridge : TestDocumentIntegrationSpecification
{
private TestDocument document;
private IList<TestDocument> results;

protected override void configureSearchMapping()
{
Id(d => d.Id);
Map(d => d.StringProperty)
.Index().No();
Map(d => d.Tags)
.Index().Tokenized()
.Store().Yes()
.Bridge().Custom<TagBridge>();
}

protected override void Given()
{
document = new TestDocument { StringProperty = "lorem ipsum dolor sit amet" };
var fiction = new Tag { Name = "Fiction" };
var latin = new Tag { Name = "Latin" };
document.Tags.Add(fiction);
document.Tags.Add(latin);

session.Save(fiction);
session.Save(latin);
session.Save(document);
session.Flush();
}

protected override void When()
{
results = fullTextSession
.CreateFullTextQuery<TestDocument>("Tags:latin")
.List<TestDocument>();
}

[Then]
public void The_document_should_be_found()
{
results.Count.ShouldEqual(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace FluentNHibernate.Search.Tests.Integration
{
[TestFixture]
public abstract class Integration_Specification<TDocument> : DocumentMap<TDocument>
{
protected ISession session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class TestDocumentIntegrationSpecification : Integration_Specifi
protected override void fnhMappings(MappingConfiguration mapping)
{
mapping.FluentMappings.Add<TestDocumentMap>();
mapping.FluentMappings.Add<TagDocumentMap>();
}

protected override Configuration searchConfig(FluentSearchConfiguration config)
Expand Down
28 changes: 28 additions & 0 deletions src/FluentNHibernate.Search.Tests/Mappings/TagBridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Search.Bridge;
using FluentNHibernate.Search.Tests.Domain;
using Lucene.Net.Documents;

namespace FluentNHibernate.Search.Tests.Mappings
{
public class TagBridge : IFieldBridge
{
public void Set(string name, object value, Lucene.Net.Documents.Document document, Lucene.Net.Documents.Field.Store store, Lucene.Net.Documents.Field.Index index, float? boost)
{
var tags = value as ICollection<Tag>;
if (tags == null)
return;

foreach (var tag in tags)
{
var field = new Field(name, tag.Name, store, index);
if (boost.HasValue)
field.SetBoost(boost.Value);
document.Add(field);
}
}
}
}
19 changes: 19 additions & 0 deletions src/FluentNHibernate.Search.Tests/Mappings/TagDocumentMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Search.Tests.Domain;
using FluentNHibernate.Mapping;

namespace FluentNHibernate.Search.Tests.Mappings
{
public class TagDocumentMap : ClassMap<Tag>
{
public TagDocumentMap()
{
Id(t => t.TagId);
Map(t => t.Name);
}

}
}
1 change: 0 additions & 1 deletion src/FluentNHibernate.Search.Tests/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace FluentNHibernate.Search.Tests
{
[TestFixture]
public abstract class Specification
{
[SetUp]
Expand Down
11 changes: 8 additions & 3 deletions src/FluentNHibernate.Search/Mapping/Parts/FluentMappingPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ protected FluentMappingPart(PropertyInfo propertyInfo)
// set the default getter
this.Getter = new BasicPropertyAccessor.BasicGetter(propertyInfo.DeclaringType, propertyInfo, propertyInfo.Name);

// set the default bridge
var bridge = BridgeFactory.GuessType(propertyInfo.Name, propertyInfo.PropertyType, null, null);
(this as IHasBridge).FieldBridge = bridge;
try
{
// set the default bridge
var bridge = BridgeFactory.GuessType(propertyInfo.Name, propertyInfo.PropertyType, null, null);
(this as IHasBridge).FieldBridge = bridge;
}
catch
{ }
}
}
}