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.

Tomáš Herceg
  • 1,595
  • 1
  • 13
  • 18
  • 1
    what version of RavenDb are you using? – Cristian Lupascu Nov 24 '12 at 21:25
  • I use the latest build 2159. – Tomáš Herceg Nov 24 '12 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? – Cristian Lupascu Nov 25 '12 at 11:30
  • Happens to me every time. No bundles, no special settings. – Tomáš Herceg Nov 25 '12 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. – Cristian Lupascu Nov 26 '12 at 08:11
  • I have tried this, the RavenDB Management Studio shows that 0 indexes are stale. When I run those quesies, it behaves always the same. I don't think it is stale index issue. I'll try to reproduce the issue on another machine. – Tomáš Herceg Nov 26 '12 at 10:10
  • OK, indexes are not stale. Have you also checked the contents of the documents? – Cristian Lupascu Nov 26 '12 at 10:23
  • Yes, the documents are OK. I also tried to do the query directly in the RavenDB Management Studio and it behaves the same as the C# app. However I am unable to reproduce it on another computer. – Tomáš Herceg Nov 27 '12 at 08:24

1 Answers1

6

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

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • Strange. I have set store.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites; previously and even if I add .Customize(a => a.WaitForNonStaleResults()) it behaves the same. – Tomáš Herceg Nov 25 '12 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](http://stackoverflow.com/q/10316721/776476) – biofractal Nov 26 '12 at 09:09
  • 1
    To all future readers, make sure you don't use the solution from @biofractal in any location other than a unit test. – Chris Marisic Mar 20 '14 at 20:31