RavenDB auto index: search option wrong for numeric fields (SortOptions.String)

StackOverflow https://stackoverflow.com/questions/21865876

  •  13-10-2022
  •  | 
  •  

Question

When querying RavenDB with a simple query, the autoindex is pretty useless as the SortOptions is always set to String even when the property is an integer.

var test = session.Query<Cup>()
    .OrderBy(o => o.Order)
    .ToList();


public class Cup
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Order { get; set; }
}

Do i really have to make a static index manually?

Was it helpful?

Solution

By default it will sort on a String basis. If you want to customize the behaviour, you have to write the index.

... which doesn't take much time

public class Cup_ByOrder : AbstractIndexCreationTask<Cup>
{
   public Cup_ByOrder()
   {
      Map = cups => from cup in cups
                    select new
                    {
                       cup.Order
                    }

      Sort(x=>x.Order, SortOptions.Int);
   }
}

In your application load, add the index through:

// Assuming ASP.NET
public class Global
{
   public static IDocumentStore Store;

   public void Application_Start(object sender, EventArgs e)
   {
      Store = new DocumentStore  { ... }.Initialize();
      Indexes.CreateIndexes(typeof(Cup_ByOrder).Assembly, Store);
   }
}

And now it works as you expect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top