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;
    }
}

このタイプは、structuremapを使用して正常に接続されており、ISolrOperationsの具体的なインスタンスのmappingManagerは、このCustomMappingManagerタイプのインスタンスです。

実際の作業を行うsolrnet実装のViistorsまでスタックトレースをたどりました。これらには、意図したとおりに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 に答える