Search code examples
solrsolrnet

How to retrieve dynamic fields in SolrNet 3.6?


We are working on ASP.NET MVC3 C# with SolrNet 3.6. We have used dynamic fields.Solr index has been created successfully with appropriate data and it is working perfectly in Solr Admin as well as in our application also without dynamic fields.
We have retrieved all static fields like Id,Name etc using QueryOption and all that.

But We don't know how to retrieve dynamic fields?

So please suggest us how to retrieve it?


Solution

  • Accessing Dynamic fields with SolrNet is pretty straight forward. Here is an example of mapping a set of dynamic string fields:

    The following field is defined in the schema.xml

     <field name="dynamicFields_*" fieldType="string" stored="true" indexed="true" />
    

    Then let's assume that you have indexed documents with the following fields:

    dynamicFields_item1
    dynamicFields_item2
    

    You would then add the following property to your C# class:

     public class IndexItem
     {
        ...
    
        [SolrField("dynamicFields_")]
        Dictionary<string, string> DynamicFields { get; set;}
    
        ....
     }
    

    Then once you have queried Solr and have an IndexItem class, you can access the dynamic data fields like this:

    //already have gotten the indexItem before here
    
     var item1Value = indexItem.DynamicFields["item1"];
     var item2Value = indexItem.DynamicFields["item2"];
    

    Hopefully this will help you get dynamic fields working in your code...