Querying a Static Index
If you want to only query current data, you can simply filter in the index and query non-temporally. It's helpful to name the index such that you can remember that it is filtered.
public class Foos_CurrentByBar : AbstractIndexCreationTask<Foo>
{
public Foos_CurrentByBar()
{
Map = foos => from foo in foos
let status = MetadataFor(foo).Value<TemporalStatus>(TemporalMetadata.RavenDocumentTemporalStatus)
where status == TemporalStatus.Current
select new {
foo.Bar
};
}
}
// Returns only current results. We must specifically disable temporal filtering, because the index contains only current data.
// Temporal filtering cannot work, because there are no revision documents in the index.
var results = session.Query<Foo, Foos_CurrentByBar>()
.Customize(x => x.DisableTemporalFiltering())
.Where(x=> x.Bar == 123);
If you might be querying for current data sometimes, and non-current data at other times, then it makes more sense to index the temporal revisions.
public class Foos_ByBar : AbstractIndexCreationTask<Foo>
{
public Foos_ByBar()
{
Map = foos => from foo in foos
let status = MetadataFor(foo).Value<TemporalStatus>(TemporalMetadata.RavenDocumentTemporalStatus)
where status == TemporalStatus.Revision
select new {
foo.Bar
};
}
}
// Returns current results, because the bundle filtered the results to those that are effective now.
var results = session.Query<Foo, Foos_ByBar>().Where(x=> x.Bar == 123);
// Returns past or future results, because the bundle filtered the results to those that matched the effective date we asked for.
var results = session.Effective(dto).Query<Foo, Foos_ByBar>().Where(x=> x.Bar == 123);
You can also just use an index that is not filtered at all. This is probably the most common scenario, as it doesn't require one to think about temporal versioning when building the index.
public class Foos_ByBar : AbstractIndexCreationTask<Foo>
{
public Foos_ByBar()
{
Map = foos => from foo in foos
select new {
foo.Bar
};
}
}
// Returns current results, because the bundle filtered the results to those that are effective now.
var results = session.Query<Foo, Foos_ByBar>().Where(x=> x.Bar == 123);
// Returns past or future results, because the bundle filtered the results to those that matched the effective date we asked for.
var results = session.Effective(dto).Query<Foo, Foos_ByBar>().Where(x=> x.Bar == 123);
When you query against this type of index, the bundle will return only temporal revisions that match the effective date - just like before. However, if you need to get back all documents that exist, including current data, revisions, and artifacts, you can disable temporal filtering.
// Returns ALL documents that are in the index, without filtering by status or effective date.
var results = session.Query<Foo, Foos_ByBar>()
.Customize(x => x.DisableTemporalFiltering())
.Where(x=> x.Bar == 123);
Introduction
Home
About Temporal Versioning
About DateTimeOffset
Setup
Installation
Enabling The Bundle
Client Initialization
Configuration
Migration
Usage
Specifying Effective Dates
Basic CRUD Operations
Temporal Activation
Querying Temporal Data
Querying Dynamically
Querying a Static Index
Map/Reduce of Current Data
Advanced
Temporal Map/Reduce
Temporal Relationships
Guidance
Getting an Audit Trail
Practical Application Guidance
Reference
Misc