How to Count the size of an IndexQuery

35 views
Skip to first unread message

Pete

unread,
May 26, 2016, 6:21:12 AM5/26/16
to RavenDB - 2nd generation document database
Hi, we have an IndexQuery;

                DocumentStore.DatabaseCommands.DeleteByIndex("RecordIndex",
                new IndexQuery
                {
                    Query = "Keywords:\"metadata-admin/Delete\""
                });

I would like to get a count of this query before the command is issued, but I can't see how to with the Raven API.

Thank you.

Oren Eini (Ayende Rahien)

unread,
May 26, 2016, 6:49:42 AM5/26/16
to ravendb
That isn't possible, you need to query that first, then issue the query

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.

Pete

unread,
May 26, 2016, 7:22:58 AM5/26/16
to RavenDB - 2nd generation document database
Thanks Oren. I'm afraid I'm not sure how to write the query with the API. It seems it should be simple, but the best I can come up with is this.

            string luceneQuery = "Keywords:\"metadata-admin/Delete\"";

            using (var db = DocumentStore.OpenSession())
            {
                // this loads all the records into memory because i can't figure out how to do it better
                int count = db.Advanced.DocumentQuery<Record>("RecordIndex").Where(luceneQuery).ToList().Count;
                Console.WriteLine("Deleting {0} records...", count);
            }

Grisha Kotler

unread,
May 26, 2016, 7:54:15 AM5/26/16
to rav...@googlegroups.com
You'll only get a maximum of 128 results like this.

This is what you should use:
            string luceneQuery = "Keywords:\"metadata-admin/Delete\"";

            using (var db = DocumentStore.OpenSession())
            {
                // you only need the total results count of the query
                RavenQueryStatistics stats;
                db.Advanced.DocumentQuery<Record>("RecordIndex")
                                .Where(luceneQuery)
                                .Statistics(out stats)
                                .Take(0)
                                .ToList();

                Console.WriteLine("Deleting {0} records...", stats.TotalResults);
            }

Hibernating Rhinos Ltd  cid:image001.png@01CF95E2.8ED1B7D0

Grisha Kotler l RavenDB Core Team Developer Mobile: +972-54-586-8647

RavenDB paving the way to "Data Made Simplehttp://ravendb.net/

Pete

unread,
May 27, 2016, 5:55:37 AM5/27/16
to RavenDB - 2nd generation document database
Thanks very much!  I'd have never thought to do that.

It seems inconsistent that I can make an IndexQuery object which can be used in some API calls but not in others - ie execute it, for example. I see there is a GetIndexQuery() to go one way but not the other.

Thanks again.
Reply all
Reply to author
Forward
0 new messages