I probably don't understand how the "In" extension method in RavenDb works. I thought that it is an equivalent of the IN command in SQL world, e.g. WHERE Number IN (1, 2, 3). But it behaves quite weird.
// i have 3 records - two with Normal severity, one with Low severity
using (var session = store.OpenSession())
{
session.Store(new TestObject { Name = "o1", Severity = Severity.Normal });
session.Store(new TestObject { Name = "o2", Severity = Severity.Low });
session.Store(new TestObject { Name = "o3", Severity = Severity.Normal });
session.SaveChanges();
}
// this writes the Low severity record, it seems correct
using (var session = store.OpenSession())
{
var data = session.Query<TestObject>()
.Where(o => o.Severity.In(new[] { Severity.Low }))
.OrderBy(o => o.Name).ToList();
data.ForEach(r => Console.WriteLine(r));
}
// this writes also the Low severity record, it still seems correct
using (var session = store.OpenSession())
{
var data = session.Query<TestObject>()
.Where(o => o.Severity.In(new[] { Severity.Low, Severity.High }))
.OrderBy(o => o.Name).ToList();
data.ForEach(r => Console.WriteLine(r));
}
// this writes all records, still seems correct
using (var session = store.OpenSession())
{
var data = session.Query<TestObject>()
.Where(o => o.Severity.In(
new[] { Severity.High, Severity.Low, Severity.Normal }))
.OrderBy(o => o.Name).ToList();
data.ForEach(r => Console.WriteLine(r));
}
// but this does not write anything
// despite there are 2 records with Normal severity
using (var session = store.OpenSession())
{
var data = session.Query<TestObject>()
.Where(o => o.Severity.In(new[] { Severity.High, Severity.Normal }))
.OrderBy(o => o.Name).ToList();
data.ForEach(r => Console.WriteLine(r));
}
// and this does not write anything either,
// I just tried whether the order of values in the array matters
using (var session = store.OpenSession())
{
var data = session.Query<TestObject>()
.Where(o => o.Severity.In(new[] { Severity.Normal, Severity.High }))
.OrderBy(o => o.Name).ToList();
data.ForEach(r => Console.WriteLine(r));
}
Or did I find a bug in the RavenDb/Lucene engine?
I have found another strange thing. It has to do something with the alphabetical order of enum members. When I rename the Severity.Normal to Severity.A, the last two queries behave correctly and return the results. When I rename the Severity.Normal to Severity.La, it still works (because La < Low). But when I rename the Severity.Normal to Severity.Lu (Lu > Low), it breaks and the last two queries are not returning results any more. The original sample does not work because Normal > Low. But I still wonder why it happens because it makes no sense to me.
You are running into stale indexes, which is why you get strange results. You need to wait for the indexing to complete first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With