2

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
Jun Zheng
  • 677
  • 1
  • 15
  • 31
  • so, you asking if there is a way to index in schemaless mode and somehow still set one field to have only singleValue, right? – Mysterion Aug 03 '16 at 11:47
  • Yes precisely, I'm trying to run Solr in schemaless mode, but the dynamic field creation is creating everything as multivalued type. Doing so it cause a problem when mapping the result back to the model without converting all my properties into ICollection type. – Jun Zheng Aug 03 '16 at 16:55

1 Answers1

7

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.

Kerem
  • 11,377
  • 5
  • 59
  • 58
Alexandre Rafalovitch
  • 9,709
  • 1
  • 24
  • 27
  • Thanks Alex, this is very useful information, but if I define the default type to a single value type, what will happen when I try to index a multivalued array type? In another word, how would this affect the regular working of the field creation? – Jun Zheng Aug 03 '16 at 16:57
  • Well, if you were doing this manually, how would you know whether each new field should be single valued or multivalued? Is it by name pattern (and you can apply mapping to [only some fields](http://www.solr-start.com/javadoc/solr-lucene/org/apache/solr/update/processor/FieldMutatingUpdateProcessorFactory.html) )? Do you know the exceptions in advance (like in [shipped films example](http://blog.outerthoughts.com/2015/11/oh-solr-home-where-art-thou/) )? Do you want to [only keep first/last item](http://www.solr-start.com/info/update-request-processors/#FieldValueSubsetUpdateProcessorFactory)? – Alexandre Rafalovitch Aug 03 '16 at 23:32
  • So far changing the default value to the single value type works great, no more mapping issue as all the field is created in singular type. For all the field I need to index right now is all singular type, but not sure if it will expand to arrays and complex models. Not sure how it will handle at that point, I guess I'll have to test it to see. – Jun Zheng Aug 05 '16 at 02:51