Query arbitrary nested field

48 views
Skip to first unread message

Kavan Shaban

unread,
Jan 31, 2016, 9:47:17 PM1/31/16
to RavenDB - 2nd generation document database
I'd like to build an Index that would be used to query on a specific document property.

In our instance we do not know where the property will be in the document, since it is the property "$type" which is inserted by JSON.net during serialization.

In essence I would like to flatten the JSON document and index any properties that are named "$type".

I will need the value obviously, but ideally the full path would be very helpful as well.

I have tried AsDocument() and that only seems to return the first level KeyValuePairs.

I also attempted to use Recurse on the results of AsDocument() but I'm getting an exception relating to generic parameters on using Recurse.

There's got to be an easier way which I'm totally missing here!

Thanks in advance,

Kavan

Oren Eini (Ayende Rahien)

unread,
Feb 1, 2016, 12:24:15 AM2/1/16
to ravendb
Can you show us what you tried?

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kavan Shaban

unread,
Feb 2, 2016, 12:02:17 AM2/2/16
to RavenDB - 2nd generation document database
I attempted to build the recursive index like below, however, I get the exception regarding generic type arguments with Recurse.  The type arguments are RavenJObject and RavenJToken.

    sealed public class Index_PolymorphicTypeUsings
    : AbstractIndexCreationTask<CompoundedMedicationLot, Index_PolymorphicTypeUsings.Result> {
        public class Result {
            public string Id { get; set; }
            //public IEnumerable<string> Keys { get; set; }
            public IEnumerable<object> Values { get; set; }
        }

        public Index_PolymorphicTypeUsings() {
            Map = src => from p in src
                         let doc = AsDocument(p)
                         let values = Recurse(doc, x => x.Values())
                         select new {
                             Id = p.Id,
                             Values = values
                         };

            Reduce = results => from result in results
                                group result by result.Id into g
                                select new Result {
                                    Id = g.Key,
                                    //Keys = g.First().Keys,
                                    Values = g.First().Values,
                                };
            Index(p => p.Id, FieldIndexing.Analyzed);
            Index(p => p.Values, FieldIndexing.Analyzed);
        }
    }

Oren Eini (Ayende Rahien)

unread,
Feb 2, 2016, 1:49:35 AM2/2/16
to ravendb
That is because you are passing very different things here.
You are passing a document, and returning _values_.
If property is a string, you don't want to send it to the Recurse again, right?

                         let values = Recurse(doc, x => x.Values())


What does CompoundedMedicationLot looks like, and what do you want to fetch from it?

Kavan Shaban

unread,
Feb 3, 2016, 12:24:55 AM2/3/16
to RavenDB - 2nd generation document database
I was mistakenly assuming that AsDocument() returns a RavenJObject and not a string.

What I'm after is the property "$type" that maybe at an arbitrary depth in the document.

My approach was to attempt to flatten the document and then index the properties named $type.

This actually has a very practical use when applied to updating type names in the application model.

Oren Eini (Ayende Rahien)

unread,
Feb 3, 2016, 4:41:24 AM2/3/16
to ravendb
If you need to do this just for updates, using a scripted patch would be much easier.
Reply all
Reply to author
Forward
0 new messages