0

我正在尝试在 solrnet 中实现自定义 IReadOnlyMappingManager 以允许我们使用我们自己的属性类型来装饰代表我们的 solr 索引记录的文档的属性。由于我只需要替换GetFields和GetUniqueKey方法的实现,目前的实现如下:

public class CustomMappingManager : AttributesMappingManager
{        
    public new ICollection<KeyValuePair<PropertyInfo, string>> GetFields(Type type)
    {
        IEnumerable<KeyValuePair<PropertyInfo, IndexFieldAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexFieldAttribute>(type);

        IEnumerable<KeyValuePair<PropertyInfo, string>> fields = from mapping in mappedProperties
                                                                 select new KeyValuePair<PropertyInfo, string>(mapping.Key, mapping.Value[0].FieldName ?? mapping.Key.Name);

        return new List<KeyValuePair<PropertyInfo, string>>(fields);
    }

    public new KeyValuePair<PropertyInfo, string> GetUniqueKey(Type type)
    {
        KeyValuePair<PropertyInfo, string> uniqueKey;

        IEnumerable<KeyValuePair<PropertyInfo, IndexUniqueKeyAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexUniqueKeyAttribute>(type);

        IEnumerable<KeyValuePair<PropertyInfo, string>> fields = from mapping in mappedProperties
                                                                 select new KeyValuePair<PropertyInfo, string>(mapping.Key, mapping.Value[0].FieldName ?? mapping.Key.Name);

        uniqueKey = fields.FirstOrDefault();

        return uniqueKey;
    }
}

此类型已使用结构映射成功连接,并且我的 IsolrOperations 具体实例中的 mappingManager 是此 CustomMappingManager 类型的实例。

我一直在跟踪堆栈跟踪,一直到执行实际工作的 solrnet 实现中的 Viistor;这些具有预期的 CustomMappingManager 实例。不幸的是,这种类型的 GetFields 和 GetUniqueKey 方法永远不会被调用,而且我的文档总是空的。

任何想法都非常受欢迎。

4

1 回答 1

1

我已经解决了这个问题。问题中的方法是错误的解决方法。这是 CustomMappingManager 实现的工作代码的等效部分:

public class CustomMappingManager : IReadOnlyMappingManager
{

public ICollection<SolrFieldModel> GetFields(Type type)
    {
        IEnumerable<KeyValuePair<PropertyInfo, IndexFieldAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexFieldAttribute>(type);

        IEnumerable<SolrFieldModel> fields = from mapping in mappedProperties
                                             select new SolrFieldModel()
                                             {
                                                 Property = mapping.Key,
                                                 FieldName = mapping.Value[0].FieldName ?? mapping.Key.Name
                                             };

        return new List<SolrFieldModel>(fields);
    }

public SolrFieldModel GetUniqueKey(Type type)
    {
        SolrFieldModel uniqueKey;

        IEnumerable<KeyValuePair<PropertyInfo, IndexUniqueKeyAttribute[]>> mappedProperties = this.GetPropertiesWithAttribute<IndexUniqueKeyAttribute>(type);

        IEnumerable<SolrFieldModel> fields = from mapping in mappedProperties
                                             select new SolrFieldModel()
                                             {
                                                 Property = mapping.Key,
                                                 FieldName = mapping.Value[0].FieldName ?? mapping.Key.Name
                                             };

        uniqueKey = fields.FirstOrDefault();

        if (uniqueKey == null)
        {
            throw new Exception("Index document has no unique key attribute");
        }

        return uniqueKey;
    }
}
于 2011-02-25T11:26:05.817 回答