Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB: Indexing documents from multiple collections

Tags:

ravendb

I have several document collections that occasionally need to be pulled together into a single index for reporting purposes.

This FAQ provides a solution for writing such an index in Raven Studio: http://ravendb.net/faq/indexing-across-entities

While I understand I won't get full compile-time checking, I'm trying to avoid completely unchecked code like this:

public class Assets_ById : AbstractIndexCreationTask
{
    public override IndexDefinition CreateIndexDefinition()
    {
        return new IndexDefinition
        {
            Map = @"from doc in docs
                    where doc[""@metadata""][""Raven-Entity-Name""] == ""Cars"" ||
                          doc[""@metadata""][""Raven-Entity-Name""] == ""Trains"" ||
                          doc[""@metadata""][""Raven-Entity-Name""] == ""Boats"" ||
                          doc[""@metadata""][""Raven-Entity-Name""] == ""Planes""
                    select new
                    {
                        Cost = doc.Cost,
                        Id = doc.Id,
                        Name = doc.Name,
                        Type = doc.Type,
                    };"
        }
    }
}

Is there something similar to the generic AbstractIndexCreationTask<T> that will allow me to define a heterogeneous index with lambda expressions?

like image 223
Richard Poole Avatar asked Aug 22 '11 13:08

Richard Poole


People also ask

How does RavenDB work?

RavenDB takes the query, analyses it and extracts an index which can answer the query. Also, it has the ability to auto generate indexes from your queries. RavenDB uses JSON (JavaScript Object Notation) to store documents. JSON is a way to serialize data and is a lightweight data-interchange format.

How do you create a collection in RavenDB?

To create a collection you need to create an entity with instance of a Class as parameter.

What is RavenDB used for?

RavenDB is multimodel offering data models Document, Graph, Key/Value, and Time Series making it ideal for any microservices architecture. The Developer's Database, RavenDB was designed to be easy to use.

Is RavenDB open source?

RavenDB is an open-source fully ACID document-oriented database written in C#, developed by Hibernating Rhinos Ltd. It is cross-platform, supported on Windows, Linux, and Mac OS. RavenDB stores data as JSON documents and can be deployed in distributed clusters with master-master replication.


2 Answers

You can use WhereEntityIs(names), like this:

from doc in docs.WhereEntityIs<Vehicle>("Cars", "Trains", "Boats", "Planes")
select new 
{
  doc.Cost,
  doc.Name,
  doc.Type
}
like image 173
Ayende Rahien Avatar answered Jan 03 '23 00:01

Ayende Rahien


Take a look here: https://groups.google.com/forum/#!topic/ravendb/9wvRY0OiGBs

It's basically the same question and the short answer is:

"right now there isn't a better option, but there will be in the future"

like image 29
Daniel Lang Avatar answered Jan 02 '23 22:01

Daniel Lang