IndexOf Support

123 views
Skip to first unread message

Ben

unread,
Jul 2, 2013, 9:46:04 AM7/2/13
to rav...@googlegroups.com
I need to obtain the index of an item from another document within a RavenDB index. The following index produces the following error:

"'Raven.Abstractions.Linq.DynamicList' does not contain a definition for 'IndexOf'"


    public class Projects_Search : AbstractIndexCreationTask<Project>
   
{
       
public Projects_Search()
       
{
           
Map = projects => from p in projects
                              let portfolio
= LoadDocument<Portfolio>(p.SiteId + "/portfolio")
                              let category
= LoadDocument<PortfolioCategory>(p.CategoryId)
                             
select new
                             
{
                                 
SiteId = p.SiteId,
                                 
Slug = p.Slug,
                                 
Tags_Slugs = p.Tags.Select(t => t.Slug),
                                 
Deleted = p.Deleted,
                                 
CategoryId = p.CategoryId,
                                 
CategorySlug = category.Slug,
                                 
Published = p.Published,
                                 
Terms = new object[]
                                 
{
                                      p
.Title,
                                      p
.Summary,
                                      category
.Title,
                                      p
.Tags.Select(t => t.ToString()),
                                      p
.Content
                                 
},
                                 
PortfolioIndex = portfolio.Projects.IndexOf(p.Id)
                             
};


           
Index("Terms", Raven.Abstractions.Indexing.FieldIndexing.Analyzed);
       
}
   
}

Fitzchak Yitzchaki

unread,
Jul 2, 2013, 9:48:56 AM7/2/13
to <ravendb@googlegroups.com>
Can you provide a full failing test?


--
You received this message because you are subscribed to the Google Groups "ravendb" 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/groups/opt_out.
 
 

Ben

unread,
Jul 2, 2013, 10:12:59 AM7/2/13
to rav...@googlegroups.com
Interestingly there is no error when calling Execute on the index but the query returns no results:

using NUnit.Framework;
using Raven.Client.Indexes;
using Raven.Tests.Helpers;
using System.Collections.Generic;
using System.Linq;

namespace RavenTests
{
    [TestFixture]
    public class IndexOfTest : RavenTestBase
    {
        [Test]
        public void Can_use_index_of()
        {
            using (var store = NewDocumentStore())
            {
                new ProjectsIndex().Execute(store);
                
                using (var session = store.OpenSession())
                {
                    var portfolio = new Portfolio { Id = "sites/1/portfolio" };
                    var p1 = new Project { Title = "Project 1", SiteId = "sites/1" };
                    var p2 = new Project { Title = "Project 2", SiteId = "sites/1" };

                    session.Store(p1);
                    session.Store(p2);
                    portfolio.Projects.AddRange(new[] { p2.Id, p1.Id });
                    session.Store(portfolio);

                    session.SaveChanges();
                }

                WaitForIndexing(store);

                using (var session = store.OpenSession())
                {
                    var results = session.Advanced.LuceneQuery<Project, ProjectsIndex>()
                        .OrderBy("PortfolioIndex")
                        .ToList();
                        
                    Assert.AreEqual("Project 2", results.First().Title);
                }
            }
        }
    }

    public class Project
    {
        public string Id { get; set; }
        public string SiteId { get; set; }
        public string Title { get; set; }
    }

    public class Portfolio
    {
        public string Id {get;set; }
        public List<string> Projects { get; set; }
        
        public Portfolio()
        {
            Projects = new List<string>();
        }
    }

    public class ProjectsIndex : AbstractIndexCreationTask<Project>
    {
        public ProjectsIndex()
        {
            Map = projects => from p in projects
                              let portfolio = LoadDocument<Portfolio>(p.SiteId + "/portfolio")
                              select new
                              {
                                  Id = p.Id,
                                  Title = p.Title,
                                  SiteId = p.SiteId,
                                  PortfolioIndex = portfolio.Projects.IndexOf(p.Id)
                              };
        }
    }
}

Ben

unread,
Jul 2, 2013, 12:16:24 PM7/2/13
to rav...@googlegroups.com
I was able to get this working by calling ToList on the Raven DynamicList:

    public class ProjectsIndex : AbstractIndexCreationTask<Project>
    {
        public ProjectsIndex()
        {
            Map = projects => from p in projects
                              let portfolio = LoadDocument<Portfolio>(p.SiteId + "/portfolio")
                              let ids = portfolio.Projects.ToList()
                              select new
                              {
                                  Id = p.Id,
                                  Title = p.Title,
                                  SiteId = p.SiteId,
                                  PortfolioIndex = ids.IndexOf(p.Id)
                              };
        }
    }

Fitzchak Yitzchaki

unread,
Jul 3, 2013, 3:20:04 AM7/3/13
to <ravendb@googlegroups.com>
The issue with this solution that if Projects is null, the index will throw.
This is way we wrap the list in a DynamicList. This will be fixed in the next build.


Fitzchak Yitzchaki

unread,
Jul 3, 2013, 4:09:50 AM7/3/13
to <ravendb@googlegroups.com>
And btw, there is an index error here. You just need to call AssertNoIndexErrors(store); in order to throw for index errors.
This will past the next build:

Ben Foster

unread,
Jul 3, 2013, 5:08:40 AM7/3/13
to rav...@googlegroups.com

In our scenario projects will never be null but can see how this could be an issue.

You received this message because you are subscribed to a topic in the Google Groups "ravendb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ravendb/6HhKcTeZo5Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ravendb+u...@googlegroups.com.

Ben Foster

unread,
Jul 3, 2013, 5:09:23 AM7/3/13
to rav...@googlegroups.com

Thanks, good to know. Will look out for it in the next release.

You received this message because you are subscribed to a topic in the Google Groups "ravendb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ravendb/6HhKcTeZo5Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ravendb+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages