Skip to content

Instantly share code, notes, and snippets.

@thoemmi
Created October 7, 2012 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoemmi/3848253 to your computer and use it in GitHub Desktop.
Save thoemmi/3848253 to your computer and use it in GitHub Desktop.
Find parent document by path in RavenDB
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