views:

75

answers:

1

Hi,

I'm trying using NHibernate.Search to get Lucene.NET Score through projections.

My domain object implements an interface IScorableEntity

public interface IScorableEntity
{
    float Score { get; set; }
}

...

IFullTextSession session = Search.CreateFullTextSession(database.Session);
IFullTextQuery textQuery = session.CreateFullTextQuery(query, typeof(Book));
textQuery.SetProjection(ProjectionConstants.SCORE);
var books = textQuery.List<Book>();

Without the score projection all is working, but with it a got an exception :

InvalidCastException : At least one element in the source array could not be cast down to the destination array type.

A: 

Found myself, i need to use 2 projections for this

textQuery.SetProjection(ProjectionConstants.SCORE, ProjectionConstants.THIS);

var list = textQuery.List();

var books = new List<Book>();
foreach(object[] o in list)
{
    var book= o[1] as Book;
    if (book!= null)
    {
        book.Score = (float)o[0];
    }
    books.Add(book);
}

return books;
Yoann. B