Skip to content
This repository has been archived by the owner on Nov 3, 2018. It is now read-only.

Querying a Static Index

Matt Johnson edited this page Jan 26, 2013 · 3 revisions

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);