RavenDB StartsWith Linq(具体化路径)



此查询失败(RavenDB 1.0.701)

var items= RavenSession.Query<Item>().Where(x => "161 193".StartsWith(x.MaterializedPath)).ToList();

还有别的办法吗?

在您询问之前,我正在尝试获取具有物化路径(沿袭列)的树结构数据的父(祖先)。

在RavenDB中不能这样做-查询的字段必须在谓词的左边,谓词的右边不能引用另一个字段。

至于如何重组-对不起,不确定。

编辑:

好吧,这需要一些实验——但如果可以重组MaterializedPath或添加新属性,我设法使其发挥作用。为了避免混淆,我假设这是一处新房产。

// Sample class: 
public class Item 
{ 
  public string Name { get;set;}
  public Dictionary<int, string> Path { get;set;} // Zero-based key on path. 
}

// Query: Find nodes with path "A B"  
var query = session.Query<Item>().AsQueryable();
query = query.Where(item => item.Path[0] == "A");
query = query.Where(item => item.Path[1] == "B");
var found = query.ToList();

它在这里运行:

IDocumentStore store = new EmbeddableDocumentStore { RunInMemory = true };
store.Initialize();
// Install Data
using (var session = store.OpenSession())
{
    session.Store(new Item("Foo1", "A")); // NB: I have a constructor on Item which takes the path and splits it up. See below. 
    session.Store(new Item("Foo2", "A B"));
    session.Store(new Item("Foo3", "A C D"));
    session.Store(new Item("Foo4", "A B C D"));
    session.Store(new Item("Foo5", "C B A"));
    session.SaveChanges();
}
using (var session = store.OpenSession())
{
    var query = session
        .Query<Item>().AsQueryable();
    query = query.Where(item => item.Path[0] == "A");
    query = query.Where(item => item.Path[1] == "B");
    var found = query.ToList();
    Console.WriteLine("Found Items: {0}", found.Count );
    foreach(var item in found)
    {
        Console.WriteLine("Item Name {0}, Path = {1}", item.Name, string.Join(" ", item.Path));
    }
}

其输出为:

Found Items: 2
Item Name Foo2, Path = [0, A] [1, B]
Item Name Foo4, Path = [0, A] [1, B] [2, C] [3, D]

希望能有所帮助。

编辑2:

我在Item上的构造函数看起来是这样的,只是为了便于测试:

    public Item(string name, string materializedPath)
    {
        Name = name;
        var tmpPath = materializedPath.Split(' ');
        Path =
            tmpPath
                .Zip(Enumerable.Range(0, tmpPath.Count()), (item, index) => new {Item = item, Index = index})
                .ToDictionary(k => k.Index, v => v.Item);
    }

我建议建立一个索引,其中包含每种搜索可能性。这将在你的索引中创建很多项目,但同时Lucene的强大功能是快速搜索

Map = docs => from n in docs
              let allPaths = n.MaterializedPath.Split(new char[]{' '})
              from path in allPaths 
              select new
              {
                 Path = path
              };

重要的是,"路径"表示唯一文档ID

最新更新