8

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?

Edit

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.

8
  • 1
    what version of RavenDb are you using? Nov 24, 2012 at 21:25
  • I use the latest build 2159. Nov 24, 2012 at 22:50
  • I have tested with builds 960 (stable) and 2159, 2160 (unstable) and could not reproduce the error. For me it worked fine even without the WaitForNonStaleResults option. Do you have any Bundles activated or any unusual settings? Does this happen every time you run the code or just occasionally? Nov 25, 2012 at 11:30
  • Happens to me every time. No bundles, no special settings. Nov 25, 2012 at 15:28
  • Another idea: 1. Run the code that stores the items; 2. Inspect the documents in the DB with RavenDb Management Studio - see if they look like you'd expect; 3. run the queries only to see if you get the weird results again. This should avoid any stale index issues. Nov 26, 2012 at 8:11

1 Answer 1

6

You are running into stale indexes, which is why you get strange results. You need to wait for the indexing to complete first.

3
  • Strange. I have set store.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites; previously and even if I add .Customize(a => a.WaitForNonStaleResults()) it behaves the same. Nov 25, 2012 at 10:10
  • The DefaultQueryingConsistency feature did not work for me either. I have written an extension method that helps. See RavenDb : Force indexes to wait until not stale whilst unit testing
    – biofractal
    Nov 26, 2012 at 9:09
  • 1
    To all future readers, make sure you don't use the solution from @biofractal in any location other than a unit test. Mar 20, 2014 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.