/// <summary>
        /// Validates that all properties in the mapping are present in the Solr schema
        /// as either a SolrField or a DynamicField
        /// </summary>
        /// <param name="documentType">Document type</param>
        /// <param name="solrSchema">The solr schema.</param>
        /// <param name="mappingManager">The mapping manager.</param>
        /// <returns>
        /// A collection of <see cref="ValidationResult"/> objects with any issues found during validation.
        /// </returns>
        public IEnumerable<ValidationResult> Validate(Type documentType, SolrSchema solrSchema, IReadOnlyMappingManager mappingManager) {
            foreach (var mappedField in mappingManager.GetFields(documentType).Values) {
                var field = mappedField;
                if (IgnoredFieldNames != null && IgnoredFieldNames.Any(f => f == field.FieldName))
                    continue;
                if (field.FieldName.Contains("*")) // ignore multi-mapped fields (wildcards or dictionary mappings)
                    continue;
                var fieldFoundInSolrSchema = false;
                foreach (var solrField in solrSchema.SolrFields) {
                    if (solrField.Name.Equals(field.FieldName)) {
                        fieldFoundInSolrSchema = true;
                        break;
                    }
                }

                if (!fieldFoundInSolrSchema) {
                    foreach (var dynamicField in solrSchema.SolrDynamicFields) {
                        if (IsGlobMatch(dynamicField.Name, field.FieldName)) {
                            fieldFoundInSolrSchema = true;
                            break;
                        }
                    }
                }

                if (!fieldFoundInSolrSchema)
                    // If field couldn't be matched to any of the solrfield, dynamicfields throw an exception.
                    yield return new ValidationError(String.Format("No matching SolrField or DynamicField '{0}' found in the Solr schema for document property '{1}' in type '{2}'.",
                                                                          field.FieldName, field.Property.Name, documentType.FullName));
            }
        }
 /// <summary>
 /// Validates the specified validation rules.
 /// </summary>
 /// <param name="documentType">The document type which needs to be validated</param>
 /// <param name="schema">The Solr schema.</param>
 /// <returns>A collection of <see cref="ValidationResult"/> objects with the problems found during validation. If Any.</returns>
 public IEnumerable<ValidationResult> EnumerateValidationResults(Type documentType, SolrSchema schema) {
     foreach (var rule in rules) {
         var items = rule.Validate(documentType, schema, mappingManager);
         foreach (var i in items)
             yield return i;
     }
 }
Пример #3
0
        /// <summary>
        /// Parses the specified Solr schema xml.
        /// </summary>
        /// <param name="solrSchemaXml">The Solr schema xml to parse.</param>
        /// <returns>A strongly styped representation of the Solr schema xml.</returns>
        public SolrSchema Parse(XDocument solrSchemaXml)
        {
            var result = new SolrSchema();

            var schemaElem = solrSchemaXml.Element("schema");

            foreach (var fieldNode in schemaElem.XPathSelectElements("types/fieldType|types/fieldtype"))
            {
                var field = new SolrFieldType(fieldNode.Attribute("name").Value, fieldNode.Attribute("class").Value);
                result.SolrFieldTypes.Add(field);
            }

            var fieldsElem = schemaElem.Element("fields");

            foreach (var fieldNode in fieldsElem.Elements("field"))
            {
                var fieldTypeName = fieldNode.Attribute("type").Value;
                var fieldType     = result.FindSolrFieldTypeByName(fieldTypeName);
                if (fieldType == null)
                {
                    throw new SolrNetException(string.Format("Field type '{0}' not found", fieldTypeName));
                }
                var field = new SolrField(fieldNode.Attribute("name").Value, fieldType);
                field.IsRequired = fieldNode.Attribute("required") != null?fieldNode.Attribute("required").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;

                field.IsMultiValued = fieldNode.Attribute("multiValued") != null?fieldNode.Attribute("multiValued").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;

                field.IsStored = fieldNode.Attribute("stored") != null?fieldNode.Attribute("stored").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;

                field.IsIndexed = fieldNode.Attribute("indexed") != null?fieldNode.Attribute("indexed").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;

                field.IsDocValues = fieldNode.Attribute("docValues") != null?fieldNode.Attribute("docValues").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;

                result.SolrFields.Add(field);
            }

            foreach (var dynamicFieldNode in fieldsElem.Elements("dynamicField"))
            {
                var dynamicField = new SolrDynamicField(dynamicFieldNode.Attribute("name").Value);
                result.SolrDynamicFields.Add(dynamicField);
            }

            foreach (var copyFieldNode in schemaElem.Elements("copyField"))
            {
                var copyField = new SolrCopyField(copyFieldNode.Attribute("source").Value, copyFieldNode.Attribute("dest").Value);
                result.SolrCopyFields.Add(copyField);
            }

            var uniqueKeyNode = schemaElem.Element("uniqueKey");

            if (uniqueKeyNode != null && !string.IsNullOrEmpty(uniqueKeyNode.Value))
            {
                result.UniqueKey = uniqueKeyNode.Value;
            }

            return(result);
        }
 /// <summary>
 /// Validates that the uniqueKey mapped in the type is the same as in the Solr schema.
 /// </summary>
 /// <param name="documentType">Document type</param>
 /// <param name="solrSchema">The solr schema.</param>
 /// <param name="mappingManager">The mapping manager.</param>
 /// <returns>
 /// A collection of <see cref="ValidationResult"/> objects with any issues found during validation.
 /// </returns>
 public IEnumerable<ValidationResult> Validate(Type documentType, SolrSchema solrSchema, IReadOnlyMappingManager mappingManager) {
     var mappedKey = mappingManager.GetUniqueKey(documentType);
     if (mappedKey == null && solrSchema.UniqueKey == null)
         yield break;
     if (mappedKey == null && solrSchema.UniqueKey != null) {
         yield return new ValidationWarning(string.Format("Solr schema has unique key field '{0}' but mapped type '{1}' doesn't have a declared unique key", solrSchema.UniqueKey, documentType));
     } else if (mappedKey != null && solrSchema.UniqueKey == null) {
         yield return new ValidationError(string.Format("Type '{0}' has a declared unique key '{1}' but Solr schema doesn't have a unique key", documentType, mappedKey.FieldName));
     } else if (!mappedKey.FieldName.Equals(solrSchema.UniqueKey)) {
         yield return new ValidationError(String.Format("Solr schema unique key '{0}' does not match document unique key '{1}' in type '{2}'.", solrSchema.UniqueKey, mappedKey, documentType));
     }
 }
        /// <summary>
        /// Validates the specified the mapped document against the solr schema.
        /// </summary>
        /// <param name="documentType">Document type</param>
        /// <param name="solrSchema">The solr schema.</param>
        /// <param name="mappingManager">The mapping manager.</param>
        /// <returns>
        /// A collection of <see cref="ValidationResult"/> objects with any issues found during validation.
        /// </returns>
        public IEnumerable<ValidationResult> Validate(Type documentType, SolrSchema solrSchema, IReadOnlyMappingManager mappingManager) {
            var collectionFieldParser = new CollectionFieldParser(null); // Used to check if the type is a collection type.

            foreach (var prop in mappingManager.GetFields(documentType)) {
                var solrField = solrSchema.FindSolrFieldByName(prop.Key);
                if (solrField == null)
                    continue;
                var isCollection = collectionFieldParser.CanHandleType(prop.Value.Property.PropertyType);
                if (solrField.IsMultiValued && !isCollection)
                    yield return new ValidationError(String.Format("SolrField '{0}' is multivalued while property '{1}.{2}' is not mapped as a collection.", solrField.Name, prop.Value.Property.DeclaringType, prop.Value.Property.Name));
                else if (!solrField.IsMultiValued && isCollection)
                    yield return new ValidationError(String.Format("SolrField '{0}' is not multivalued while property '{1}.{2}' is mapped as a collection.", solrField.Name, prop.Value.Property.DeclaringType, prop.Value.Property.Name));
            }
        }
 public IEnumerable<ValidationResult> Validate(Type documentType, SolrSchema solrSchema, IReadOnlyMappingManager mappingManager) {
     foreach (var x in mappingManager.GetFields(documentType)) {
         var solrField = solrSchema.FindSolrFieldByName(x.Key);
         if (solrField == null)
             continue;
         foreach (var checker in fieldTypeCheckers) {
             if (!checker.CanHandleType(x.Value.Property.PropertyType))
                 continue;
             var i = checker.Validate(solrField.Type, x.Value.Property.Name, x.Value.Property.PropertyType);
             if (i != null)
                 yield return i;
         }
     }
 }
        /// <summary>
        /// Parses the specified Solr schema xml.
        /// </summary>
        /// <param name="solrSchemaXml">The Solr schema xml to parse.</param>
        /// <returns>A strongly styped representation of the Solr schema xml.</returns>
        public SolrSchema Parse(XDocument solrSchemaXml) {
            var result = new SolrSchema();

            var schemaElem = solrSchemaXml.Element("schema");

            foreach (var fieldNode in schemaElem.XPathSelectElements("types/fieldType|types/fieldtype")) {
                var field = new SolrFieldType(fieldNode.Attribute("name").Value, fieldNode.Attribute("class").Value);
                result.SolrFieldTypes.Add(field);
            }

            var fieldsElem = schemaElem.Element("fields");

            foreach (var fieldNode in fieldsElem.Elements("field")) {
                var fieldTypeName = fieldNode.Attribute("type").Value;
                var fieldType = result.FindSolrFieldTypeByName(fieldTypeName);
                if (fieldType == null)
                    throw new SolrNetException(string.Format("Field type '{0}' not found", fieldTypeName));
                var field = new SolrField(fieldNode.Attribute("name").Value, fieldType);
                field.IsRequired = fieldNode.Attribute("required") != null ? fieldNode.Attribute("required").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
                field.IsMultiValued = fieldNode.Attribute("multiValued") != null ? fieldNode.Attribute("multiValued").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
                field.IsStored = fieldNode.Attribute("stored") != null ? fieldNode.Attribute("stored").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
                field.IsIndexed = fieldNode.Attribute("indexed") != null ? fieldNode.Attribute("indexed").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
                field.IsDocValues = fieldNode.Attribute("docValues") != null ? fieldNode.Attribute("docValues").Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false; 

                result.SolrFields.Add(field);
            }

            foreach (var dynamicFieldNode in fieldsElem.Elements("dynamicField")) {
                var dynamicField = new SolrDynamicField(dynamicFieldNode.Attribute("name").Value);
                result.SolrDynamicFields.Add(dynamicField);
            }

            foreach (var copyFieldNode in schemaElem.Elements("copyField")) {
                var copyField = new SolrCopyField(copyFieldNode.Attribute("source").Value, copyFieldNode.Attribute("dest").Value);
                result.SolrCopyFields.Add(copyField);
            }

            var uniqueKeyNode = schemaElem.Element("uniqueKey");
            if (uniqueKeyNode != null && !string.IsNullOrEmpty(uniqueKeyNode.Value)) {
                result.UniqueKey = uniqueKeyNode.Value;
            }

            return result;
        }
        /// <summary>
        /// Validates that all SolrFields in the SolrSchema which are required are
        /// either present in the mapping or as a CopyField.
        /// </summary>
        /// <param name="documentType">Document type</param>
        /// <param name="solrSchema">The solr schema.</param>
        /// <param name="mappingManager">The mapping manager.</param>
        /// <returns>
        /// A collection of <see cref="ValidationResult"/> objects with any issues found during validation.
        /// </returns>
        public IEnumerable<ValidationResult> Validate(Type documentType, SolrSchema solrSchema, IReadOnlyMappingManager mappingManager) {

            foreach (SolrField solrField in solrSchema.SolrFields) {
                if (solrField.IsRequired) {
                    bool fieldFoundInMappingOrCopyFields = mappingManager.GetFields(documentType).ContainsKey(solrField.Name);

                    if (!fieldFoundInMappingOrCopyFields) {
                        foreach (SolrCopyField copyField in solrSchema.SolrCopyFields) {
                            if (copyField.Destination.Equals(solrField.Name)) {
                                fieldFoundInMappingOrCopyFields = true;
                                break;
                            }
                        }
                    }

                    if (!fieldFoundInMappingOrCopyFields)
                        yield return new ValidationError(String.Format("Required field '{0}' in the Solr schema is not mapped in type '{1}'.",
                                                     solrField.Name, documentType.FullName));
                }
            }
        }