I am working on a Sitecore 6.6 upgrade (yes, I know that that is still pretty much obsolete) and I came across a weird bug that took an age to track down. This is more a reminder for myself than anything else.
The code for Lucene searches frequently used lines of the form:
List<SearchHit> distinctHits = hits.Slice(0).Distinct(new SearchHitEqualityComparer()).ToList();
This always returned zero SearchHits. It seems that this is due to the call to ‘Slice(0)‘ bit. This does raise some questions…
Why would you want to .Slice(0)? This is saying ‘give me all contents of my enumerable, from the first to the last’. It’s not exactly a slice. I can only imagine someone didn’t know about .Hits property, which will give you that same enumerable.
How is this going wrong? Sure, the .Slice() call is weird, but it’s not wrong. I decompiled the Sitecore Kernel to try to figure out what was going on – and it’s a bit knotted. It’s not clear what class is doing what. I did find this interesting implementation:
This should never return any items. However, that was the same in the Kernel before upgrade, so I’m not sure what it is in my 6.5 to 6.6 upgrade that has exposed this issue – but if I replace the .Slice() with .Hits my search seems to work again.