/// <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));
            }
        }
Пример #2
0
 /// <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;
     }
 }
        /// <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 = false;
                    foreach (var mappedField in mappingManager.GetFields(documentType)) {
                        if (mappedField.FieldName.Equals(solrField.Name)) {
                            fieldFoundInMappingOrCopyFields = true;
                            break;
                        }
                    }

                    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));
                }
            }
        }
Пример #4
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);
        }
 public void SchemaNotNull_MappingNull_generates_warning() {
     var rule = new UniqueKeyMatchesMappingRule();
     var schema = new SolrSchema {UniqueKey = "id"};
     var validations = rule.Validate(typeof (SchemaMappingTestDocument), schema, new MappingManager()).ToList();
     Assert.IsNotNull(validations);
     Assert.AreEqual(1, validations.Count);
     foreach (var v in validations)
         Console.WriteLine("{0}: {1}", v.GetType(), v.Message);
     Assert.IsInstanceOfType<ValidationWarning>(validations[0]);
 }
 public void DictionaryFields_are_ignored()
 {
     var rule = new MappedPropertiesIsInSolrSchemaRule();
     var mapper = new MappingManager();
     mapper.Add(typeof(SchemaMappingTestDocument).GetProperty("DynamicMapped"), "ma_*");
     var schema = new SolrSchema();
     var fieldType = new SolrFieldType("string", "solr.StrField");
     schema.SolrFields.Add(new SolrField("ma_uaua", fieldType));
     var results = rule.Validate(typeof(SchemaMappingTestDocument), new SolrSchema(), mapper).ToList();
     Assert.AreEqual(0, results.Count);
 }
Пример #7
0
 /// <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;
         }
     }
 }
Пример #10
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;
        }
Пример #11
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(XmlDocument solrSchemaXml)
        {
            var result = new SolrSchema();

            foreach (XmlNode fieldNode in solrSchemaXml.SelectNodes("/schema/types/fieldType|/schema/types/fieldtype")) {
                var field = new SolrFieldType(fieldNode.Attributes["name"].Value, fieldNode.Attributes["class"].Value);
                result.SolrFieldTypes.Add(field);
            }

            foreach (XmlNode fieldNode in solrSchemaXml.SelectNodes("/schema/fields/field")) {
                var fieldTypeName = fieldNode.Attributes["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.Attributes["name"].Value, fieldType);
                field.IsRequired = fieldNode.Attributes["required"] != null ? fieldNode.Attributes["required"].Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
                field.IsMultiValued = fieldNode.Attributes["multiValued"] != null ? fieldNode.Attributes["multiValued"].Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
                result.SolrFields.Add(field);
            }

            foreach (XmlNode dynamicFieldNode in solrSchemaXml.SelectNodes("/schema/fields/dynamicField")) {
                var dynamicField = new SolrDynamicField(dynamicFieldNode.Attributes["name"].Value);
                result.SolrDynamicFields.Add(dynamicField);
            }

            foreach (XmlNode copyFieldNode in solrSchemaXml.SelectNodes("/schema/copyField")) {
                var copyField = new SolrCopyField(copyFieldNode.Attributes["source"].Value, copyFieldNode.Attributes["dest"].Value);
                result.SolrCopyFields.Add(copyField);
            }

            var uniqueKeyNode = solrSchemaXml.SelectSingleNode("/schema/uniqueKey");
            if (uniqueKeyNode != null && !string.IsNullOrEmpty(uniqueKeyNode.InnerText)) {
                result.UniqueKey = uniqueKeyNode.InnerText;
            }

            return result;
        }
 public IEnumerable<ValidationResult> Validate(Type propertyType, SolrSchema solrSchema, IReadOnlyMappingManager mappingManager)
 {
     return new ValidationResult[] {new ValidationError("Dummy warning validation rule")};
 }
 public void SchemaNull_MappingNull_no_errors()
 {
     var rule = new UniqueKeyMatchesMappingRule();
     var schema = new SolrSchema();
     var validations = rule.Validate(typeof (SchemaMappingTestDocument), schema, new MappingManager()).ToList();
     Assert.IsNotNull(validations);
     Assert.AreEqual(0, validations.Count);
 }
Пример #14
0
 public IEnumerable<ValidationResult> EnumerateValidationResults(Type documentType, SolrSchema schema) {
     return enumerate.Invoke(documentType, schema);
 }