Created
October 7, 2012 12:29
-
-
Save thoemmi/3848253 to your computer and use it in GitHub Desktop.
Find parent document by path in RavenDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program { | |
static void Main(string[] args) { | |
using (var store = new EmbeddableDocumentStore { RunInMemory = true }) { | |
store.Initialize(); | |
using (var session = store.OpenSession()) { | |
session.Store(new Document { Path = "a" }); | |
session.Store(new Document { Path = "a/b" }); | |
session.Store(new Document { Path = "a/b/c" }); | |
session.Store(new Document { Path = "a/d" }); | |
session.Store(new Document { Path = "a/d/e" }); | |
session.Store(new Document { Path = "a/f" }); | |
session.SaveChanges(); | |
} | |
const string path = "a/b/c/x/y"; | |
using (var session = store.OpenSession()) { | |
var pathParts = path.Split('/'); | |
var query = session.Query<Document>(); | |
for (var i = 1; i <= pathParts.Length; ++i) { | |
var shortenedPath = String.Join("/", pathParts, startIndex : 0, count : i); | |
query = query.Search(doc => doc.Path, shortenedPath, boost : i, options : SearchOptions.Or); | |
} | |
var document = query.Take(1).FirstOrDefault(); | |
Console.WriteLine("Found document: " + document.Path); | |
} | |
Console.WriteLine("READY."); | |
Console.ReadLine(); | |
} | |
} | |
} | |
public class Document { | |
public string Id { get; set; } | |
public string Path { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment