Gravatar for

Question by jkearns, Jan 7, 2015 4:12 PM

The given key was not present in the dictionary.

My project has a custom search page that gets search results using the Sitecore search API. The out of the box Coveo search page works fine but the custom search page gives me the error "The given key was not present in the dictionary."

To get results I'm using the Sitecore PredicateBuilder class to get a list of Sitecore SearchResultItems. The error occurs when I try to use SearchResultItem.GetItem() to get a Sitecore item. I've had trouble in the past where the query is actually the problem, and the error only appears when I try to read the result. This doesn't seem to be the case, since I am able to use functions like .Count(), so I am getting a countable number of search result items that are causing errors when I try to get the Sitecore item from them. Also, I get the error even when the predicate builder is just the default true value, so the predicate doesn't seem to be the issue.

I noticed this problem after updating my Coveo version. The code works on Coveo for Sitecore revision 742 (the October release) but breaks on revision 863 (December). I'm not sure which one is the anomaly, or how to fix it. Has anyone experienced this before?

Gravatar for

Comment by Jean-François L'Heureux, Jan 7, 2015 4:59 PM

You created a PredicateBuilder for a particular result type. Does that type contains an Item property or member?

Gravatar for

Comment by jkearns, Jan 7, 2015 6:00 PM

It's of type Sitecore.ContentSearch.SearchTypes.SearchResultItem. As I understand it, that class does have that member. It should also have members Uri and ItemID, which are both null when I try to load them. I'm making sure that my result item is of that type, so I don't really think it's possible for it to be overridden either.

1 Reply
Gravatar for

Answer by Jean-François L'Heureux, Jan 8, 2015 9:57 AM

I think your code throws the exception when calling

Item result = returned.GetItem();

The SearchResultItem.GetItem() method seems to only use the this["_uniqueid"] field accessor and the Uri property (that is populated when the _uniqueid field is returned on the document).

You specified that the Uri property of the results was null. This is probably related to the inability to find the _uniqueid field in the this[] fields dictionary leading to your exception.

It seems that the _uniqueid field is not returned in the results.

One possible reason is customization done in the <coveoQueryFieldPipeline> element of the Coveo.SearchProvider.config file (see Optimizing LINQ Query Performance). Can you provide me your <coveoQueryFieldPipeline> element please?

You can also check in the Coveo Administration Tool Index Browser to see if all the documents of your instance's Coveo_web_index source contains the @fz95xuniqueidXXXXX field and that it contains a value.

Gravatar for

Comment by jkearns, Jan 8, 2015 12:52 PM

I was missing the section from my config file. Thank you for the article link, it helped a lot. I'm not sure if revision 742 did not use that config section or whether I accidentally deleted it when updating the config file, but in any event it seems to be working now. Thank you!

Gravatar for

Comment by Jean-François L'Heureux, Jan 8, 2015 1:26 PM

I'm happy that it resolved your problem. For your information, the element was introduced in the December 2014 release :

When upgrading Coveo for Sitecore, it is recommended to follow these upgrade instructions. If you skip releases between two upgrades, you must manually gather all the required changes by reading all the skipped versions upgrade instructions.

Ask a question