1

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?

ozu
  • 125
  • 1
  • 5

1 Answers1

0

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.

Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
  • This is the correct answer. But to treat every field as string for auto indexes makes the auto index feature quite useless. Even you would be ok with an auto index (if numbers are treated as numbers), you have to write all the boiler plate code since the auto generated one just uses string. – ozu Jun 02 '17 at 12:48