簡體   English   中英

在Solrnet中實現自定義IReadOnlyMappingManager

[英]Implementing a custom IReadOnlyMappingManager in solrnet

我正在嘗試在solrnet中實現自定義IReadOnlyMappingManager,以允許我們使用自己的Attribute類型來修飾表示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具體實例中,已經使用structuremap成功連接了該類型,並且mappingManager是此CustomMappingManager類型的實例。

我一直遵循堆棧跟蹤,一直到進行實際工作的solrnet實現中的Viistors。 這些具有所需的CustomMappingManager實例。 不幸的是,這種類型的GetFields和GetUniqueKey方法永遠不會被調用,而且我的文檔總是空的。

任何想法都非常歡迎。

我已經解決了。 問題中的方法是錯誤的解決方法。 這是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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM