/// <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 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)));
                    }
                }
            }
        }
 internal SolrQueryTranslator(Type resultType, IReadOnlyMappingManager mapper)
 {
     _resultType = resultType;
     this.mapper = mapper;
     solrFields = mapper.GetFields(resultType).Values.ToDictionary(x => x.Property.Name, x => x.FieldName);
     SortItems = new List<SortOrder>();
 }
        public static string FieldName <T>(this IReadOnlyMappingManager mapper, Expression <Func <T, object> > property)
        {
            var propertyName = property.MemberName();

            //return mapper.GetFields(typeof (T)).First(p => p.Property.Name == propertyName).FieldName;
            return(mapper.GetFields(typeof(T)).First(p => p.Key == propertyName).Value.FieldName);
        }
Exemple #5
0
        public void Visit(object doc, string fieldName, XElement field)
        {
            var            allFields = mapper.GetFields(doc.GetType());
            SolrFieldModel thisField;

            if (!allFields.TryGetValue(fieldName, out thisField))
            {
                return;
            }
            if (!thisField.Property.CanWrite)
            {
                return;
            }
            if (parser.CanHandleSolrType(field.Name.LocalName) &&
                parser.CanHandleType(thisField.Property.PropertyType))
            {
                var v = parser.Parse(field, thisField.Property.PropertyType);
                try
                {
                    thisField.Property.SetValue(doc, v, null);
                }
                catch (ArgumentException e)
                {
                    throw new ArgumentException(string.Format("Could not convert value '{0}' to property '{1}' of document type {2}", v, thisField.Property.Name, thisField.Property.DeclaringType), e);
                }
            }
        }
        /// <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));
                }
            }
        }
Exemple #7
0
        public SolrFieldModel GetThisField(Type t, string fieldName)
        {
            var allFields        = mapper.GetFields(t).Values;
            var fieldsICanHandle = allFields.Where(f => memoCanHandleType(f.Property.PropertyType));
            var matchingFields   = fieldsICanHandle.Where(f => f.FieldName == "*" || fieldName.StartsWith(f.FieldName));

            return(matchingFields.FirstOrDefault(f => !allFields.Any(x => x.FieldName == fieldName && !Equals(x, f))));
        }
        /// <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;
         }
     }
 }
Exemple #10
0
        public XmlDocument Serialize(T doc, double?boost)
        {
            var xml     = new XmlDocument();
            var docNode = xml.CreateElement("doc");

            if (boost.HasValue)
            {
                var boostAttr = xml.CreateAttribute("boost");
                boostAttr.Value = boost.Value.ToString(CultureInfo.InvariantCulture.NumberFormat);
                docNode.Attributes.Append(boostAttr);
            }
            var fields = mappingManager.GetFields(doc.GetType());

            foreach (var field in fields)
            {
                var p = field.Property;
                if (!p.CanRead)
                {
                    continue;
                }
                var value = p.GetValue(doc, null);
                if (value == null)
                {
                    continue;
                }
                var nodes = fieldSerializer.Serialize(value);
                foreach (var n in nodes)
                {
                    var fieldNode = xml.CreateElement("field");
                    var nameAtt   = xml.CreateAttribute("name");
                    nameAtt.InnerText = (field.FieldName == "*" ? "" : field.FieldName) + n.FieldNameSuffix;
                    fieldNode.Attributes.Append(nameAtt);

                    if (field.Boost != null && field.Boost > 0)
                    {
                        var boostAtt = xml.CreateAttribute("boost");
                        boostAtt.InnerText = field.Boost.ToString();
                        fieldNode.Attributes.Append(boostAtt);
                    }

                    fieldNode.InnerText = n.FieldValue;
                    docNode.AppendChild(fieldNode);
                }
            }
            xml.AppendChild(docNode);
            return(xml);
        }
Exemple #11
0
        /// <summary>
        /// Parses documents results
        /// </summary>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public IList <T> ParseResults(XElement parentNode)
        {
            var results = new List <T>();

            if (parentNode == null)
            {
                return(results);
            }
            var allFields = mappingManager.GetFields(typeof(T));
            var nodes     = parentNode.Elements("doc");

            foreach (var docNode in nodes)
            {
                results.Add(ParseDocument(docNode));
            }
            return(results);
        }
        public XElement Serialize(T doc, double?boost)
        {
            var docNode = new XElement("doc");

            if (boost.HasValue)
            {
                var boostAttr = new XAttribute("boost", boost.Value.ToString(CultureInfo.InvariantCulture.NumberFormat));
                docNode.Add(boostAttr);
            }
            var fields = mappingManager.GetFields(doc.GetType());

            foreach (var field in fields.Values)
            {
                var p = field.Property;
                if (!p.CanRead)
                {
                    continue;
                }
                var value = p.GetValue(doc, null);
                if (value == null)
                {
                    continue;
                }
                var nodes = fieldSerializer.Serialize(value);
                foreach (var n in nodes)
                {
                    var fieldNode = new XElement("field");
                    var nameAtt   = new XAttribute("name", (field.FieldName == "*" ? "" : field.FieldName) + n.FieldNameSuffix);
                    fieldNode.Add(nameAtt);

                    if (field.Boost != null && field.Boost > 0)
                    {
                        var boostAtt = new XAttribute("boost", field.Boost.Value.ToString(CultureInfo.InvariantCulture.NumberFormat));
                        fieldNode.Add(boostAtt);
                    }

                    var v = RemoveControlCharacters(n.FieldValue);
                    if (v != null)
                    {
                        fieldNode.Value = v;
                        docNode.Add(fieldNode);
                    }
                }
            }
            return(docNode);
        }
        /// <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)));
                }
            }
        }
        public void Visit(object doc, string fieldName, XElement field)
        {
            var allFields = mapper.GetFields(doc.GetType());
            var thisField = allFields.FirstOrDefault(p => p.FieldName == fieldName);

            if (thisField == null)
            {
                return;
            }
            if (!thisField.Property.CanWrite)
            {
                return;
            }
            if (parser.CanHandleSolrType(field.Name.LocalName) &&
                parser.CanHandleType(thisField.Property.PropertyType))
            {
                var v = parser.Parse(field, thisField.Property.PropertyType);
                thisField.Property.SetValue(doc, v, null);
            }
        }
Exemple #15
0
        /// <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)));
                }
            }
        }
Exemple #16
0
 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);
             }
         }
     }
 }