给定的密​​钥不在字典solrnet中

时间:2014-01-03 13:57:57

标签: solr solrnet

请注意:我知道问题SolrNet - The given key was not present in the dictionary,我已经像Mauricio建议的那样初始化了solr对象。

我正在使用solr 4.6.0和solrnet build#173,.net framework 4.0和VS2012进行开发。由于某些未知原因,我收到错误'字典中没有给定的密钥'。我在solr中有一个带有该id的文档,我已经通过浏览器进行了检查。这是一个像任何其他文件一样的文件。为什么错误弹出?我的代码(我对错误发生的地方做了评论):

//establishes connection with solr
        private void ConnectToSolr()
        {
            try
            {
                if (_solr != null) return;               
                Startup.Init<Register>(SolrAddress);
                _solr = ServiceLocator.Current.GetInstance<ISolrOperations<Register>>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    //Returns snippets from solr as BindingSource
    public BindingSource GetSnippets(string searchTerm, DateTime? startDate = null, DateTime? endDate = null)
    {
        ConnectToSolr();

        string dateQuery = startDate == null
                               ? ""
                               : endDate == null
                                     ? "savedate:\"" + convertDateToSolrFormat(startDate) + "\"" //only start date
                                     : "savedate:[" + convertDateToSolrFormat(startDate) + " TO " +
                                       convertDateToSolrFormat(endDate) + "]";//range between start and end date

        string textQuery = string.IsNullOrEmpty(searchTerm) ? "text:*" : "text:*" + searchTerm + "*";
        List<Register> list = new List<Register>();
        SolrQueryResults<Register> results;
        string currentId = "";

        try
        {

            results = _solr.Query(textQuery,
                new QueryOptions
                {
                    Highlight = new HighlightingParameters
                    {
                        Fields = new[] { "*" },
                    },
                    ExtraParams = new Dictionary<string, string>
                    {
                        {"fq", dateQuery},
                        {"sort", "savedate desc"}
                    }
                });

            for (int i = 0; i < results.Highlights.Count; i++)
            {
                currentId = results[i].Id;
                var h = results.Highlights[currentId];
                if (h.Snippets.Count > 0)
                {
                    list.Add(new Register//here the error "the given key was not present in the dictionary pops up 
                    {   
                        Id = currentId, 
                        ContentLiteral = h.Snippets["content"].ToArray()[0].Trim(new[]{' ', '\n'}), 
                        SaveDateLiteral = results[i].SaveDate.ToShortDateString()
                    });
                }
            }

            BindingList<Register> bindingList = new BindingList<Register>(list);                
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = bindingList;
            return bindingSource;
        }
        catch(Exception e)
        {
            MessageBox.Show(string.Format("{0}\nId:{1}", e.Message, currentId), "Solr error");
            return null;
        }            
    }

2 个答案:

答案 0 :(得分:0)

我发现了导致问题的原因:将空文档保存到solr中。如果我通过solrnet创建一个空查询(带有text:*)(通常我这样做,如果我想查看所有保存的文档)并且空文档是已保存的文档之一,那么'给出的键不会出现在字典中弹出' 。如果所有文档中都包含文本,则不会弹出此错误。

答案 1 :(得分:0)

如果您的文档包含字符串不是字符串的字段,并且您将空值索引到double或整数字段,则会得到相同的错误。

solr query将null字段返回为:

<null name="fieldname"/>

应该是

<double name="fieldname">0.0</double>

<double name="fieldname"/>