Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Christoffersen 64 posts 119 karma points
    Oct 28, 2009 @ 16:27
    Dan Christoffersen
    0

    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

    Any suggestions on how to fix this?

     

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Sep 27, 2010 @ 10:55
    Ismail Mayat
    1

    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:

    var sr = (SearchResult)rptItem.DataItem;

                if (sr.Fields.ContainsKey("bodyText"))
                {
           }

    Regards

    Ismail

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 27, 2010 @ 14:09
    Aaron Powell
    0

    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

  • Matt Taylor 873 posts 2086 karma points
    Feb 01, 2011 @ 12:37
    Matt Taylor
    1

    This was my approach if it helps someone.

    <p><%# ((Examine.SearchResult)Container.DataItem).Fields.ContainsKey("summary") == true ? ((Examine.SearchResult)Container.DataItem).Fields["summary"] : "" %></p>

    Matt

  • Connie DeCinko 931 posts 1160 karma points
    Jun 29, 2011 @ 20:54
    Connie DeCinko
    0

    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.

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jun 30, 2011 @ 04:41
    Aaron Powell
    0

    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

  • Matt Taylor 873 posts 2086 karma points
    Jun 30, 2011 @ 10:50
    Matt Taylor
    0

    Great, glad I posted it.

  • Connie DeCinko 931 posts 1160 karma points
    Jul 13, 2011 @ 00:27
    Connie DeCinko
    0

    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"]%> 
    

  • Barry Fogarty 493 posts 1129 karma points
    Aug 17, 2011 @ 19:02
    Barry Fogarty
    0

    @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.

Please Sign in or register to post replies

Write your reply to:

Draft