简体   繁体   English

NHibernate。搜索投影

[英]NHibernate.Search Projections

I'm trying using NHibernate.Search to get Lucene.NET Score through projections. 我正在尝试使用NHibernate.Search通过投影获得Lucene.NET分数。

My domain object implements an interface IScorableEntity 我的域对象实现了一个接口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. InvalidCastException:无法将源数组中的至少一个元素转换为目标数组类型。

Found myself, i need to use 2 projections for this 发现自己,我需要为此使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM