【问题标题】:ravendb index across multiple nested properties跨多个嵌套属性的 ravendb 索引
【发布时间】:2014-01-02 12:08:15
【问题描述】:

我在问如何根据文档上的两个不同嵌套属性创建索引。我正在通过 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; }
}

我尝试过各种 AbstractIndexCreationTask、Map 和 Map+Reduce,但都无济于事。

我希望能够进行如下查询:

获取任何 Locations.Code 属性为“USA”、AND/OR Colors.Code="RED" 或 SeqId 属性的所有文档。我不知道这是否意味着我需要多个索引。通常我要么比较两个嵌套类的 Code 属性,要么比较 Seq,但从不混合。

请有人指出正确的方向。

非常感谢 菲尔

【问题讨论】:

    标签: c# indexing ravendb


    【解决方案1】:

    像这样创建索引:

    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)
                };
        }
    }
    

    然后这样查询:

    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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多