Querying a RavenDB index on ID given not-indexed-e

2019-07-23 04:08发布

问题:

I have a RavenDB database with two document collections. I need to combine documents from these two into a single business entity using a multi map/reduce index. The business entity isn't complete if I don't combine the two collections. You could probably argue that this indicates that my domain model or data model is broken but it is what it is and there isn't anythig I can do about it. So, on with the question :-).

Basically the three documents:

{ // RootDocuments/1
    "Foo" : "Bar",
    "Bar" : "Foo"
}

{ // ExtraDocuments/1
    "RootId" : "RootDocuments/1",
    "Value" : 2
}

{ // ExtraDocuments/2
    "RootId" : "RootDocuments/1",
    "Value" : 3
}

is combined into the following by the index:

{
    "Id" : "RootDocuments/1",
    "Foo" : "Bar",
    "Bar" : "Foo",
    "Value" : 5 // The sum of values from the extra documents.
}

Since I always need to combine the two collections with my multi map index, even retrieving an entity based on an ID needs to query using the index. When I make a query looking like

session.Query<RootDocumentsByIdIndex.Result, RootDocumentsByIdIndex>().Where(d => d.Id == id).Single();

I get the following error:

System.ArgumentException: The field '__document_id' is not indexed, cannot query on fields that are not indexed

Can I force RavenDB to index the Id field? Or is there another way to do this query?