Question

I am asking how to create an index based upon two different nested properties on an document. I am executing these queries through C#.

public class LocationCode
{
    public string Code {get;set;}
    public string SeqId {get;set;}
}

public class ColorCode
{
    public string Code {get;set;}
    public string SeqId {get;set;}
}

public class TestDocument
{
    public int Id {get;set;}
    public List<LocationCode> Locations { get; set; }
    public List<ColorCode> Colors { get; set; }
}

I have experimented with various AbstractIndexCreationTask, Map, and Map+Reduce, but to no avail.

I would like to be able to do a query such as:

Get all documents where any Locations.Code property is "USA", AND/OR Colors.Code="RED", or on the SeqId property. I dont know whether this would mean I need multiple indexes. Normally I would either be comparing the Code property on both nested classes, or the Seq, but never mixed.

Please could someone point me in the right direction.

Many thanks Phil

Was it helpful?

Solution

Create your index like this:

public class TestIndex : AbstractIndexCreationTask<TestDocument, TestIndex.IndexEntry>
{
    public class IndexEntry
    {
        public IList<string> LocationCodes { get; set; }
        public IList<string> ColorCodes { get; set; }
    }

    public TestIndex()
    {
        Map = testDocs =>
            from testDoc in testDocs
            select new
            {
                LocationCodes = testDoc.Locations.Select(x=> x.Code),
                ColorCodes = testDoc.Colors.Select(x=> x.Code)
            };
    }
}

Then query it like this:

var q = session.Query<TestIndex.IndexEntry, TestIndex>()
    .Where(x => x.LocationCodes.Any(y => y == "USA") &&
                x.ColorCodes.Any(y => y == "RED"))
    .OfType<TestDocument>();

Full unit test here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top