Create index: Sort by custom metadata

290 views
Skip to first unread message

ErikHeemskerk

unread,
Aug 3, 2012, 4:46:54 AM8/3/12
to rav...@googlegroups.com
I'm trying to define an explicit index using an AbstractIndexCreationTask which will sort documents by a custom metadata property, but I get a runtime exception if I want to create the index.

The index is defined as follows:
internal sealed class FoosByClassificationOrderedByCreatedIndex: AbstractIndexCreationTask<Foo>
{
public FoosByClassificationOrderedByCreatedIndex()
{
Map = Foos => from foo in Foos
                   select new
                   {
                   foo.Classification,
                   Created = this.MetadataFor(foo).Value<DateTime>("Created"),
                   };
Sort(foo=> MetadataFor(foo).Value<DateTime>("Created"), SortOptions.None);
}
}

The exception I get:
System.InvalidOperationException in Raven.Abstractions: Not idea how to deal with convert value(Services.Raven.Index.FoosByClassificationOrderedByCreatedIndex).MetadataFor(foo).Value("Created") to a member expression

I'm using the embedded client (1.2) running in memory. Apparently, the map is not a problem, but the sort is.

Oren Eini (Ayende Rahien)

unread,
Aug 3, 2012, 4:49:41 AM8/3/12
to rav...@googlegroups.com
internal sealed class FoosByClassificationOrderedByCreatedIndex : AbstractIndexCreationTask<Foo>
{
public FoosByClassificationOrderedByCreatedIndex()
{
Map = Foos => from foo in Foos
 select new
 {
 foo.Classification,
 Created = this.MetadataFor(foo).Value<DateTime>("Created"),
 };
}

public override IndexDefinition CreateIndexDefinition()
{
var indexDefinition = base.CreateIndexDefinition();
indexDefinition.SortOptions["Created"] = SortOptions.None;
return indexDefinition;
}
}

Note that for dates, you don't need to do anything.
This is identical to the defaults.

ErikHeemskerk

unread,
Aug 3, 2012, 5:09:57 AM8/3/12
to rav...@googlegroups.com
So you mean I only have to map the metadata and I can then sort by it?


On Friday, August 3, 2012 10:49:41 AM UTC+2, Oren Eini wrote:
internal sealed class FoosByClassificationOrderedByCreatedIndex : AbstractIndexCreationTask<Foo>
{
public FoosByClassificationOrderedByCreatedIndex()
{
Map = Foos => from foo in Foos
 select new
 {
 foo.Classification,
 Created = this.MetadataFor(foo).Value<DateTime>("Created"),
 };
}

public override IndexDefinition CreateIndexDefinition()
{
var indexDefinition = base.CreateIndexDefinition();
indexDefinition.SortOptions["Created"] = SortOptions.None;
return indexDefinition;
}
}

Note that for dates, you don't need to do anything.
This is identical to the defaults.

Oren Eini (Ayende Rahien)

unread,
Aug 3, 2012, 5:11:21 AM8/3/12
to rav...@googlegroups.com
Yes
Reply all
Reply to author
Forward
0 new messages