NHibernate.Search预测

时间:2010-03-05 07:46:49

标签: c# nhibernate nhibernate.search

我正在尝试使用NHibernate.Search通过预测获得Lucene.NET得分。

我的域对象实现了一个接口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>();

没有得分预测一切正常,但有了一个例外:

  

InvalidCastException:至少一个   源数组中的元素不能   被转发到目标数组   类型。

1 个答案:

答案 0 :(得分:1)

发现自己,我需要使用2个投影

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;