Solr Schemaless Mode creating fields as MultiValue

2019-06-04 08:06发布

问题:

I'm using Solr 6.1 in Schemaless Mode. After creating a collection and indexing a sample data the fields created were all set to have MultiValued = true, except for unique id.

The problem is when querying this data using SolrNet it wouldn't map the result to the model correctly. The queried results is returned as an array and require all my properties in the model to be updated to ICollection type.

Is there anyway we can set these field to MultiValued = false when indexing the sample data?

An example to illustrate the problem:

1) Index a sample of the following model in Schemaless Mode:

public class TestModel
{
    [SolrUniqueKey("id")]
    public int Id { get; set; }

    [SolrField("guid")]
    public Guid Guid { get; set; }
}

2) Solr's managed-schema file will be added with the following fields

  <field name="guid" type="strings"/>
  <field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>

3) Error during querying / mapping of the model

Object of type 'System.Collections.ArrayList' cannot be converted to type

回答1:

The schemaless mode makes everything multiValued as it does not know if you have single values followed by multivalued values for the same field. So it makes all fields multivalued and also upgrades numeric types to the largest.

This is easily adjustable if you know your domain well. The whole mapping chain is defined in the solrconfig.xml's update request processor chain (add-unknown-fields-to-the-schema) and you can change the type mapping from multivalued type to an equivalent single valued type. For strings, you change the value in the defaultFieldType.