使用 RavenDb 跨多种类型查询

Query across multiple types using RavenDb

我是第一次看 raven db,我注意到它有一些非常强大的查询功能,与 Lucene.net

结合使用时更是如此

像下面的例子一样可以使用通配符。

BlogPost[] prefixedResultsWithMatch = session.Advanced
.LoadStartingWith<BlogPost>("blogposts/1", "*/Author/*t");

我很想做的一件事是跨多个对象类型进行查询。

例如,如果我的 'Customer' 和 'Business' 类型都带有 属性 'Name',我希望能够获得所有 'Customers' 和'Businesses' 在一次调用中(即使它们不是 "related")。

RavenDb 和 Lucene 可以吗?

不确定 Lucene,但您可以使用 MultiMap 索引执行您要求的操作:

public class CustomerAndBusiness_ByName : AbstractMultiMapIndexCreationTask<CustomerBusiness>
{
    public CustomerAndBusiness_ByName()
    {
        AddMap<Business>(businesses => from business in businesses
                                       select new
                                       {
                                           business.Id,
                                           business.Name
                                       });

        AddMap<Customer>(customers => from customer in customers
                                       select new
                                       {
                                           customer.Id,
                                           customer.Name
                                       });

        Index(x => x.Name, FieldIndexing.Analyzed);
    }
}

然后这样查询:

using (var session = documentStore.OpenSession())
{
    var results = session.Query<CustomerBusiness, CustomerAndBusiness_ByName>()
                            .Where(x => x.Name == "Name")
                            .ProjectFromIndexFieldsInto<CustomerBusiness>()
                            .ToList();
}

这当然只是 return 实体业务和客户(称为客户业务)的表示,而不是工作单元跟踪的实体本身。

在 Ayende 的博客上阅读更多相关信息:

http://ayende.com/blog/89089/ravendb-multi-maps-reduce-indexes

希望对您有所帮助!