Esempio n. 1
0
        public virtual XElement SerializeUpdateCommand(SolrSchema schema)
        {
            var existingFieldType = schema.SolrFieldTypes.Find(f => f.Name == Name);
            var fieldType         = new XElement(existingFieldType == null ? "add-field-type" : "replace-field-type");

            fieldType.Add(new XElement("name", Name));

            foreach (var pair in Properties)
            {
                fieldType.Add(new XElement(pair.Key, pair.Value));
            }

            // There can be an index + query analyzer, one common untyped analyzer or none at all.
            var indexAnalyzer   = _analyzers.SingleOrDefault(a => a.Attributes["type"]?.Value == "index");
            var queryAnalyzer   = _analyzers.SingleOrDefault(a => a.Attributes["type"]?.Value == "query");
            var genericAnalyzer = _analyzers.SingleOrDefault(a => !a.HasAttribute("type"));

            if (indexAnalyzer != null && queryAnalyzer != null)
            {
                fieldType.Add(SerializeAnalyzer(indexAnalyzer));
                fieldType.Add(SerializeAnalyzer(queryAnalyzer));
            }
            else if (genericAnalyzer != null)
            {
                fieldType.Add(SerializeAnalyzer(genericAnalyzer));
            }

            return(fieldType);
        }
Esempio n. 2
0
        public void SolrFieldParsing()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.Equal(4, schemaDoc.SolrFields.Count);
            Assert.Equal("id", schemaDoc.SolrFields[0].Name);

            Assert.True(schemaDoc.SolrFields[0].IsRequired);
            Assert.False(schemaDoc.SolrFields[2].IsRequired);

            Assert.True(schemaDoc.SolrFields[3].IsMultiValued);
            Assert.False(schemaDoc.SolrFields[0].IsMultiValued);

            Assert.True(schemaDoc.SolrFields[2].IsIndexed);
            Assert.False(schemaDoc.SolrFields[3].IsIndexed);

            Assert.True(schemaDoc.SolrFields[0].IsStored);
            Assert.False(schemaDoc.SolrFields[3].IsStored);

            Assert.False(schemaDoc.SolrFields[1].IsDocValues);
            Assert.False(schemaDoc.SolrFields[2].IsDocValues);
            Assert.True(schemaDoc.SolrFields[3].IsDocValues);

            Assert.Equal("string", schemaDoc.SolrFields[0].Type.Name);
        }
 public ISchemaPopulateHelper GetPopulateHelper(SolrSchema solrSchema)
 {
     // Sitecore default implementation returns SchemaPopulateHelper
     // Optionally, DefaultWithDebugPopulateHelper can be returned using
     // Sitecore's default implementation but adds logging.
     return(new ConfigurationDrivenPopulateHelper(solrSchema));
 }
        /// <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)));
                    }
                }
            }
        }
        public void UniqueKeyNotPresent()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaNoUniqueKey.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.IsNull(schemaDoc.UniqueKey);
        }
Esempio n. 6
0
        public void UniqueKeyEmptyManagedSchema()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrManagedSchemaEmptyUniqueKey.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.Null(schemaDoc.UniqueKey);
        }
        public void UniqueKeyPresent()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.AreEqual("id", schemaDoc.UniqueKey);
        }
        public void SolrDynamicFieldParsing()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.AreEqual(1, schemaDoc.SolrDynamicFields.Count);
            Assert.AreEqual("*_s", schemaDoc.SolrDynamicFields[0].Name);
        }
 public SynthesisSolrFieldNameTranslator(SolrFieldMap solrFieldMap, SolrIndexSchema solrIndexSchema,
                                         ISettings settings, ISolrFieldConfigurationResolver fieldConfigurationResolver,
                                         IExtensionStripHelper extensionStripHelper, TemplateFieldTypeResolverFactory typeResolverFactory,
                                         ICultureContextGuard cultureContextGuard)
     : base(solrFieldMap, solrIndexSchema, settings, fieldConfigurationResolver, extensionStripHelper,
            typeResolverFactory, cultureContextGuard)
 {
     _schema = solrIndexSchema.SolrSchema;
 }
        public void SolrDynamicFieldParsingManagedSchema()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrManagedSchemaBasic.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.Single(schemaDoc.SolrDynamicFields);
            Assert.Equal("*_s", schemaDoc.SolrDynamicFields[0].Name);
        }
        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);
        }
        public void SolrFieldTypeParsing()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.AreEqual(2, schemaDoc.SolrFieldTypes.Count);
            Assert.AreEqual("string", schemaDoc.SolrFieldTypes[0].Name);
            Assert.AreEqual("solr.StrField", schemaDoc.SolrFieldTypes[0].Type);
        }
        public void SolrCopyFieldParsing()
        {
            var        schemaParser = new SolrSchemaParser();
            var        xml          = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
            SolrSchema schemaDoc    = schemaParser.Parse(xml);

            Assert.AreEqual(1, schemaDoc.SolrCopyFields.Count);
            Assert.AreEqual("name", schemaDoc.SolrCopyFields[0].Source);
            Assert.AreEqual("nameSort", schemaDoc.SolrCopyFields[0].Destination);
        }
        public SynthesisSolrFieldNameTranslator(IProviderSearchContext context, AbstractFieldNameTranslator translator)
            : base(translator)
        {
            //sitecore hides the solr schema behind a private field, we need it to find dynamic fields.
            var fieldInfo = context.Index.Schema.GetType().GetField("schema", BindingFlags.NonPublic | BindingFlags.Instance);

            if (fieldInfo != null)
            {
                _schema = (SolrSchema)fieldInfo.GetValue(context.Index.Schema);
            }
        }
        public virtual XElement SerializeDeleteCommand(SolrSchema schema)
        {
            if (schema.SolrFields.Find(f => f.Name == Name) == null)
            {
                return(null);
            }

            XElement deleteField = new XElement("delete-field");

            deleteField.Add(new XElement("name", Name));
            return(deleteField);
        }
        public XElement SerializeDeleteCommand(SolrSchema schema)
        {
            if (schema.SolrCopyFields.Find(f => f.Source == Source && f.Destination == Destination) == null)
            {
                return(null);
            }

            XElement deleteField = new XElement("delete-copy-field");

            deleteField.Add(new XElement("source", Source));
            deleteField.Add(new XElement("dest", Destination));
            return(deleteField);
        }
        public virtual XElement SerializeUpdateCommand(SolrSchema schema)
        {
            XElement field = new XElement(GetOperation(schema));

            field.Add(new XElement("name", Name));

            foreach (var pair in Properties)
            {
                field.Add(new XElement(pair.Key, pair.Value));
            }

            return(field);
        }
        public ConfigurationDrivenPopulateHelper([NotNull] SolrSchema solrSchema)
        {
            _solrSchema = solrSchema;

            var xmlNode = Sitecore.Configuration.Factory.GetConfigNode("contentSearch/indexConfigurations/solrManagedSchema");

            _solrSolrSchemaConfiguration = Sitecore.Configuration.Factory.CreateObject <ISolrSchemaConfiguration>(xmlNode);

            if (!_solrSolrSchemaConfiguration.IsValid())
            {
                Log.Error("Solr Schema configuration errors found. Schema population may fail.", nameof(ConfigurationDrivenPopulateHelper));
            }
        }
        public virtual XElement SerializeUpdateCommand(SolrSchema schema)
        {
            var copyField = new XElement("add-copy-field");

            copyField.Add(new XElement("source", Source));
            copyField.Add(new XElement("dest", Destination));
            if (MaxChars > 0)
            {
                copyField.Add(new XElement("maxChars", MaxChars));
            }

            return(copyField);
        }
Esempio n. 20
0
        public override XElement SerializeDeleteCommand(SolrSchema schema)
        {
            var existingField = schema.SolrDynamicFields.Find(f => f.Name == Name);

            if (existingField == null)
            {
                return(null);
            }

            XElement deleteField = new XElement("delete-dynamic-field");

            deleteField.Add(new XElement("name", Name));
            return(deleteField);
        }
        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.Empty(results);
        }
Esempio n. 22
0
        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.NotNull(validations);
            Assert.Single(validations);
            foreach (var v in validations)
            {
                testOutputHelper.WriteLine("{0}: {1}", v.GetType(), v.Message);
            }
            Assert.IsType <ValidationWarning>(validations[0]);
        }
        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.IsInstanceOf <ValidationWarning>(validations[0]);
        }
        /// <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 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)));
            }
        }
Esempio n. 26
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)));
                }
            }
        }
Esempio n. 27
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);
             }
         }
     }
 }
Esempio n. 28
0
 public IEnumerable <ValidationResult> Validate(Type propertyType, SolrSchema solrSchema, IReadOnlyMappingManager mappingManager)
 {
     return(new ValidationResult[] { new ValidationError("Dummy warning validation rule") });
 }
 public IEnumerable <ValidationResult> EnumerateValidationResults(Type documentType, SolrSchema schema)
 {
     return(enumerate.Invoke(documentType, schema));
 }
Esempio n. 30
0
 protected override string GetOperation(SolrSchema schema)
 {
     return(schema.SolrDynamicFields.Find(f => f.Name == Name) != null ?
            "replace-dynamic-field" : "add-dynamic-field");
 }