Search code examples
c#solrsolrnet

Solr returning 400


here is the object model.When i try to commit Product to Solr, returning unknown field loca

public class Product
{
    [SolrUniqueKey("id")]
    public string Id { get; set; }

    [SolrField("manu")]
    public string Manufacturer { get; set; }

    [SolrField("cat")] // cat is a multiValued field
    public ICollection<string> Categories { get; set; }

    [SolrField("price")]
    public decimal Price { get; set; }

    [SolrField("inStock")]
    public bool InStock { get; set; }

    [SolrField("loca")]
    public Location Location { set; get; }
}

public class Location
{
    [SolrField("zipcode")]
    public int Zip { set; get; }
    [SolrField("country")]
    public string Country { set; get; }
}

Is nested classes legal with solr?

why is it failing to store? when i remove [SolrField("loca")] it works fine.

how do you store such classes?


Solution

  • You cannot do nested classes in Solr. So you will need to flatten the location information into the Product class. However, you can then represent it a nested class within your application, by mapping the data into/out of Solr as needed.

    As an example, update your Solr schema to store a loca_zipcode and loca_country field and then map those perhaps in a new SolrProduct class defined like the following:

    public class SolrProduct
    {
        [SolrUniqueKey("id")]
        public string Id { get; set; }
    
        [SolrField("manu")]
        public string Manufacturer { get; set; }
    
        [SolrField("cat")] // cat is a multiValued field
        public ICollection<string> Categories { get; set; }
    
        [SolrField("price")]
        public decimal Price { get; set; }
    
        [SolrField("inStock")]
        public bool InStock { get; set; }
    
        [SolrField("loca_zip")]
        public int Zip { set; get; }
    
        [SolrField("loca_country")]
        public string Country { get; set; }
    }
    

    Then you can use something like AutoMapper to map the SolrProduct flattened class to your Product class with the nested Location class.

    Another alternative would be to use dynamic fields in Solr and the dynamic mapping support in SolrNet using a Dictionary. Please see the SolrNet - Mapping section of the SolrNet wiki for more details and examples.