Is Polymorphism working in RavenDb?

269 views
Skip to first unread message

Justin A

unread,
Dec 10, 2011, 8:27:49 AM12/10/11
to rav...@googlegroups.com
Hi folks,

A question was asked @ JabbR (http://jabbr.net/#/rooms/RavenDB) about how to retrieve a list of polymorphic documents. Seemed simple, but a run time error kept occuring. He even made a blog post about it: http://www.philliphaydon.com/2011/12/ravendb-inheritance/

The run time error was that when we asked for a list of Animals (assume Animals are the parent abstract class) .. we got the list of concretes. But if we just wanted a list of concretes (eg. Cats .. not cats and dogs) then we got a runtime error.

Now, I've created a Repo of this here: http://pastie.org/2995779

We're not sure what we've done wrong.

And also this once about a possible bug .. which has been fixed already?? : http://groups.google.com/group/ravendb/browse_thread/thread/3c57553f81840177

Can anyone see what is wrong with the Repo?

Matt Warren

unread,
Dec 10, 2011, 11:04:58 AM12/10/11
to rav...@googlegroups.com
I don't know if the dynamic indexes are expected to be able to do this or it's a bug?

BUT you can do it by writing an index yourself, see http://beta.ravendb.net/docs/consumer/advanced/document-metadata for more info.

Something like this:
    docs => from user in docs
       where user["@metadata"]["Raven-Entity-Name"] == "Animals"

       select new { Entity = user["@metadata"]["Raven-Entity-Name"], CLRType = user["@metadata"]["Raven-Clr-Type"] }

Then you can do a query like this:
    session.Advanced.LuceneQuery<Cat>("NameOfIndexCreatedAbove")
        .Where("Entity", "RavenDB.Testing.Cat, RavenDB.Testing")
        .ToList();

You could make is a bit nicer by storing "Cat" rather than "RavenDB.Testing.Cat, RavenDB.Test" in the index, just change the select to use String.Split or something like that. This is using the doc metadate from the blog post http://www.philliphaydon.com/2011/12/ravendb-inheritance/.

Oren Eini (Ayende Rahien)

unread,
Dec 11, 2011, 5:26:10 AM12/11/11
to rav...@googlegroups.com
Justin,
Based on the blog post, it is working as expected.
Raven-Clr-Type is a piece of metadata that is never actually used on the server, we are only ever using this on the client side.
When you are are asking to get all the Dogs which have the entity name "Animals" (which is effectively what you are doing), you are going to get an error, because you don't filter our the cats.

Phillip Haydon

unread,
Dec 11, 2011, 6:02:57 AM12/11/11
to ravendb
Oren - That's the problem we are trying to solve, selecting all Dog,
and not the Cat...

With NHibernate it would select by the discriminator, but I don't
understand how you would filter out with RavenDB.

Phill

On Dec 11, 9:26 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:


> Justin,
> Based on the blog post, it is working as expected.
> Raven-Clr-Type is a piece of metadata that is never actually used on the
> server, we are only ever using this on the client side.
> When you are are asking to get all the Dogs which have the entity name
> "Animals" (which is effectively what you are doing), you are going to get
> an error, because you don't filter our the cats.
>
>
>
>
>
>
>
> On Sat, Dec 10, 2011 at 3:27 PM, Justin A <jus...@adler.com.au> wrote:
> > Hi folks,
>
> > A question was asked @ JabbR (http://jabbr.net/#/rooms/RavenDB) about how
> > to retrieve a list of polymorphic documents. Seemed simple, but a run time
> > error kept occuring. He even made a blog post about it:
> >http://www.philliphaydon.com/2011/12/ravendb-inheritance/
>
> > The run time error was that when we asked for a list of Animals (assume
> > Animals are the parent abstract class) .. we got the list of concretes. But
> > if we just wanted a list of concretes (eg. Cats .. not cats and dogs) then
> > we got a runtime error.
>
> > Now, I've created a Repo of this here:http://pastie.org/2995779
>
> > We're not sure what we've done wrong.
>
> > I was also referencing this blog post:

> >http://groups.google.com/group/ravendb/browse_thread/thread/9fe0ccd9f...


> > And also this once about a possible bug .. which has been fixed already??
> > :

> >http://groups.google.com/group/ravendb/browse_thread/thread/3c57553f8...

Oren Eini (Ayende Rahien)

unread,
Dec 11, 2011, 6:08:58 AM12/11/11
to rav...@googlegroups.com
Phillip,
You _have_ a discriminator, that is what the Raven-Entity-Name is for :-)
You decided to make them both the same thing.

Let us take several steps back, what is it that you are actually trying to _do_?

Phillip Haydon

unread,
Dec 12, 2011, 6:41:25 AM12/12/11
to ravendb
Hey,

Well you solved all my problems last night during the screen cast. I'm
going to write up a blog post with what I was trying to do and with
the solution. Then I'll link it back in this thread so if someone else
finds this it may help them.

Cheers.

Phill

On Dec 11, 10:08 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:


> Phillip,
> You _have_ a discriminator, that is what the Raven-Entity-Name is for :-)
> You decided to make them both the same thing.
>
> Let us take several steps back, what is it that you are actually trying to
> _do_?
>

Oren Eini (Ayende Rahien)

unread,
Dec 12, 2011, 6:58:07 AM12/12/11
to rav...@googlegroups.com
Awesome

Phillip Haydon

unread,
Dec 13, 2011, 8:39:51 AM12/13/11
to ravendb
http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/

Updated blog post from the original.

Phill

On Dec 12, 10:58 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:
> Awesome

spokeypokey

unread,
Dec 13, 2011, 12:31:12 PM12/13/11
to rav...@googlegroups.com
Thanks so much for the blog post. It was wonderfully clear and concise.

I have a question, though, about the following line in the post:
]
"The really interesting thing I found is that if I look at what’s returned ... Are the correct CLR types that I originally defined. So I haven’t lost all the additional fields by not defining them. I’m still learning but for now I assume it allows those fields to be searchable."

I can't figure out how you could search for them using a polymorphic index simlar to your "All_Content" index.

Phillip Haydon

unread,
Dec 13, 2011, 1:27:40 PM12/13/11
to ravendb
Hey,

I didn't actually show it in the blog post, but I was playing around a
little bit with querying. One of the things I did was change the index
like so:

public class All_Content : AbstractMultiMapIndexCreationTask<Content>

So I've specified the type on the index, I used 'Content' but you
could have a class unrelated to mapped documents, and use something
that just specifies the mapped properties.

Then included an index property line within the constructor:

Indexes.Add(x => x.Title, FieldIndexing.Analyzed);

Then was using things like:

var result = session.Advanced.LuceneQuery<Content, All_Content>()
.Where("Title: article")
.ToList();

And it returned me a list of objects with 'article' written in the
title.

I plan on doing some blog posts in the future when I learn more about
searching and such.

Phill

On Dec 14, 4:31 am, spokeypokey <jeffrey.p.bern...@gmail.com> wrote:
> Thanks so much for the blog post. It was wonderfully clear and concise.
>
> I have a question, though, about the following line in the post:
> ]
> "The really interesting thing I found is that if I look at what’s returned
> ... Are the correct CLR types that I originally defined. So I haven’t lost

> all the additional fields by not defining them. I’m still learning but* for
> now I assume it allows those fields to be searchable*."

Oren Eini (Ayende Rahien)

unread,
Dec 14, 2011, 5:18:19 AM12/14/11
to rav...@googlegroups.com
Note that we usually define the Result object for indexes, and it allows us to select what are the things that we allow searching on.
Reply all
Reply to author
Forward
0 new messages