0
votes

I have a problem with RavenDB indexing.

Simple query looks like this:

        var values =
            myCollection.Query.Where(w =>
                w.MyId == MyId &&
                w.IsReady == false &&
                w.IsDeleted &&
                w.Rate > 0)

During execution Raven creates dynamic index:

from doc in docs.MyCollection
select new { Rate = doc.Rate, IsReady = doc.IsReady, IsDeleted = doc.IsDeleted, MyId = doc.MyId }

with extra options: Field -> Rate; Storage -> No; Indexing -> Default; Sort -> Double;

Field Rate has decimal type.

Problem:

I wanted to add static index, but when I specified index like this:

public class MyIndex : AbstractIndexCreationTask<MyCollection> {
    public MyIndex () {
        Map = d => d.Select(s => new { Rate = s.Rate, IsReady = s.IsReady, IsDeleted = s.IsDeleted, MyId = s.MyId });
        Sort(x => x.Rate, SortOptions.Double);
    }
}

Raven is creating index slightly different:

from doc in docs.MyCollection
select new { Rate = (decimal)doc.Rate, IsReady = doc.IsReady, IsDeleted =    doc.IsDeleted, MyId = doc.MyId }

with extra options: Field -> Rate; Storage -> No; Indexing -> Default; Sort -> Double;

The only difference is that I have casting in static index, because my field type is decimal and I'm using Double sort option.

Because of that Raven is not using my static index but instead creates dynamic one every time query is being executed.

I tried to do some casting inside Sort() but then index has not been created at all. One way to overcome this issue is to manually modify static index from management console after it was created, but it's not good solution.

Any ideas how to deal with that? Thanks.

Edit: Another example:

Type of field DateTime and querying using DateTime values as predicates (greater than / less than). Raven in dynamic index creation picks String as a SortOption, and when I try to prepare static index I get casting issue.

1

1 Answers

0
votes

You can use the IDocumentSession.Query(string indexName, [bool isMapReduce]) or the IDocumentSession.Query<TResult, TIndexCreator>() overloads to explicitly specify a static index. So in your specific case, either IDocumentSession.Query<MyCollection, MyIndex>() or IDocumentSession.Query("MyIndex").