Lucene error: The given key was not present in the dictionary.
I am trying to search for a text in a specific field using Lucene Examine but I am getting the following error:
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.ThrowHelper.ThrowKeyNotFoundException() at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at TheFarm.Umbraco.Lucene2.UmbracoIndexer.PrepareResults(TopDocs tDocs, String[] searchFields, IndexSearcher searcher) at TheFarm.Umbraco.Lucene2.UmbracoIndexer.Search(String text, String nodeTypeAlias, Boolean includeWildcards, Nullable`1 startNodeId, String[] searchFields, Int32 maxResults) at RoskildeDomkirke.Services.Frontend.hdrsearch(String word, Int32 node)
If I search in all field it works just fine.
Here is the code I am using:
UmbracoIndexer examine = new UmbracoIndexer("DK"); List<SearchResult> results = examine.Search("test", "", true, null, new string[] {"headingDK"}, 100);
It is running on a Umbraco version 4.0.2.1, IIS7, .net 3.5
Not sure if still relevant but i got caught with this recently you need to do a test for the field in the dictionary fields property becuase if the field is empty in the index it will not be present if the dictionary hence you get error so you need to do something like:
Your solution worked for me. I was pulling my hair out! Is this the only way or the best way? I tried making sure my query never returns null so the dictionary never gets null, did not work. I tried returning an empty string and even a string with a single space, neither of those worked. Unless there is real content in the column, the key does not get created.
Yes that's the best way. Lucene will only return fields which have values. If you don't have a value you don't have a field so you have to ensure the field exists
Lucene error: The given key was not present in the dictionary.
I am trying to search for a text in a specific field using Lucene Examine but I am getting the following error:
If I search in all field it works just fine.
Here is the code I am using:
It is running on a Umbraco version 4.0.2.1, IIS7, .net 3.5
Any suggestions on how to fix this?
Dan,
Not sure if still relevant but i got caught with this recently you need to do a test for the field in the dictionary fields property becuase if the field is empty in the index it will not be present if the dictionary hence you get error so you need to do something like:
}
Regards
Ismail
That version of Examine is no longer supported, you should use the latest version from http://examine.codeplex.com
And yes, there'll be breaking changes between the one you have, it was defunct over 6 months ago
This was my approach if it helps someone.
Matt
Matt,
Your solution worked for me. I was pulling my hair out! Is this the only way or the best way? I tried making sure my query never returns null so the dictionary never gets null, did not work. I tried returning an empty string and even a string with a single space, neither of those worked. Unless there is real content in the column, the key does not get created.
Yes that's the best way. Lucene will only return fields which have values. If you don't have a value you don't have a field so you have to ensure the field exists
Great, glad I posted it.
What would be the equivalent of this in the code-behind? I want to add this value to a datatable.
<%# ((Examine.SearchResult)Container.DataItem).Fields["name_first"]%>
@Connie In case its of use, I created a function that should work fine in codebehind (I am using it in a razor macro)
string FieldCheck(SearchResult result, string field)
{
return result.Fields.ContainsKey(field) == true ? @result.Fields[field] : "";
}
and called as @FieldCheck(result, "region")
where result is of type Examine.SearchResult and the field string is the name of the Lucene field you are checking for data in.
is working on a reply...