Skip to content

Instantly share code, notes, and snippets.

@brunomlopes
Created November 5, 2010 09:37
Show Gist options
  • Save brunomlopes/663887 to your computer and use it in GitHub Desktop.
Save brunomlopes/663887 to your computer and use it in GitHub Desktop.
Ravendb tag indexing
using System;
using System.Linq;
using Raven.Client.Client;
using Raven.Client.Indexes;
using Raven.Database.Indexing;
namespace ConsoleApplication2
{
internal class Program
{
public class Image
{
public string Name { get; set; }
public string[] Tags { get; set; }
}
private static void Main(string[] args)
{
var store = new EmbeddableDocumentStore() {RunInMemory = true};
store.Initialize();
using (var session = store.OpenSession())
{
session.Store(new Image
{
Name = "First",
Tags =
new[] {"Nice", "Color", "Big"}
});
session.Store(new Image
{
Name = "Second",
Tags =
new[] {"Nice", "Color"}
});
session.Store(new Image
{
Name = "Third",
Tags =
new[] {"Solo"}
});
session.SaveChanges();
var indexDefinition = new
IndexDefinition<Image, Image>()
{
Map = images => from image in images
from tag in
image.Tags
select
new {Tag = tag},
}.ToIndexDefinition(session.Advanced.Conventions);
indexDefinition.Stores.Add("Tag", FieldStorage.Yes);
indexDefinition.Indexes.Add("Tag", FieldIndexing.NotAnalyzed);
session.Advanced.DatabaseCommands.PutIndex("ImagesByTags",
indexDefinition);
var queryBigOrNice =
session.Advanced.LuceneQuery<Image>("ImagesByTags").WaitForNonStaleResults()
.WhereEquals("Tag", "Big")
.OrElse()
.WhereEquals("Tag", "Nice");
var queryBig =
session.Advanced.LuceneQuery<Image>("ImagesByTags").WaitForNonStaleResults()
.WhereEquals("Tag", "Big");
var queryNice =
session.Advanced.LuceneQuery<Image>("ImagesByTags").WaitForNonStaleResults()
.WhereEquals("Tag", "Nice");
var querySolo =
session.Advanced.LuceneQuery<Image>("ImagesByTags").WaitForNonStaleResults()
.WhereEquals("Tag", "Solo");
var manualQuery =
session.Advanced.LuceneQuery<Image>("ImagesByTags").WaitForNonStaleResults()
.Where("Tag:Nice or Tag: Big");
var resultBigOrNice = queryBigOrNice.ToArray();
var resultBig = queryBig.ToArray();
var resultNice = queryNice.ToArray();
var resultManual = manualQuery.ToArray();
var resultSolo = querySolo.ToArray();
Assert.AreEqual(2, resultManual.Length);
Assert.AreEqual(1, resultBig.Length);
Assert.AreEqual(2, resultNice.Length);
Assert.AreEqual(2, resultBigOrNice.Length);
Assert.AreEqual(1, resultSolo.Length);
}
}
public static class Assert
{
public static void AreEqual(int i, int j)
{
if (i != j)
{
throw new Exception(string.Format("i != j : {0},{1} ", i, j));
}
}
}
}
}
@philiphendry
Copy link

I changed the 'or' in the manualQuery to 'and' and expected the first assert to fail since there's only one Image with both tags. Any ideas why that's the case?

@brunomlopes
Copy link
Author

To be honest, I don't quite know why that's the case. My intuition is that it should fail. Perhaps someone on the ravendb mailing list would be able to give you some better insight, as I haven't been following RavenDB the last couple of months. Sorry

@philiphendry
Copy link

To be honest my intuition seems to suggest it should fail too but I can't put my finger on why. At the moment Ayende has suggested the Authorization bundle as the solution for the problem I currently have (assign 'audiences' to 'content' such that a user only sees content for which they are in the audience.) I was going to use tags to accomplish and this is something Authorization includes already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment