/// <summary>
        /// Extracts searchable information from a given data object
        /// (including text parts, field values for search results preview and faceted search).
        /// </summary>
        /// <param name="data">The data item to collect searchable information from.</param>
        /// <param name="skipInheritedInterfaces">When <value>true</value>, the inherited properties will not be processed.</param>
        public void CrawlData(IData data, bool skipInheritedInterfaces = false)
        {
            var interfaceType = data.DataSourceId.InterfaceType;
            var fields        = DataTypeSearchReflectionHelper.GetSearchableFields(interfaceType);

            foreach (var field in fields)
            {
                var propertyInfo = field.Key;

                if (skipInheritedInterfaces && propertyInfo.DeclaringType != interfaceType)
                {
                    continue;
                }

                var fieldProcessor = DataTypeSearchReflectionHelper.GetDataFieldProcessor(propertyInfo);

                object value = propertyInfo.GetValue(data);
                if (value == null)
                {
                    continue;
                }

                var attr = field.Value;

                // Text indexing
                if (attr.IndexText)
                {
                    var textParts = fieldProcessor.GetTextParts(value);
                    if (textParts != null)
                    {
                        _textParts.AddRange(textParts);
                    }
                }

                // Field previewing
                if (attr.Previewable)
                {
                    var indexValue = fieldProcessor.GetIndexValue(value);
                    if (indexValue != null)
                    {
                        _fieldValues.Add(new KeyValuePair <string, object>(
                                             fieldProcessor.GetDocumentFieldName(propertyInfo),
                                             indexValue));
                    }
                }

                // Faceted fields
                if (attr.Faceted)
                {
                    string[] facetValues = fieldProcessor.GetFacetValues(value);
                    if (facetValues != null && facetValues.Length > 0)
                    {
                        _facetFieldValues.Add(new KeyValuePair <string, string[]>(
                                                  fieldProcessor.GetDocumentFieldName(propertyInfo),
                                                  facetValues));
                    }
                }
            }
        }
        /// <summary>
        /// Gets the field name for the given data type property
        /// </summary>
        /// <param name="dataType">The data type.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns></returns>
        public static string GetFieldName(Type dataType, string propertyName)
        {
            Verify.ArgumentNotNull(dataType, nameof(dataType));
            Verify.ArgumentNotNull(propertyName, nameof(propertyName));

            var propertyInfo = dataType.GetProperty(propertyName);

            Verify.IsNotNull(propertyInfo, "Type '{0}' does not have a prorety '{1}'", dataType.FullName, propertyName);

            var processor = DataTypeSearchReflectionHelper.GetDataFieldProcessor(propertyInfo);

            return(processor.GetDocumentFieldName(propertyInfo));
        }
Exemple #3
0
        /// <summary>
        /// Extracts searchable information from a given data object
        /// (including text parts, field values for search results preview and faceted search).
        /// </summary>
        /// <param name="data">The data item to collect searchable information from.</param>
        /// <param name="skipInheritedInterfaces">When <value>true</value>, the inherited properties will not be processed.</param>
        public void CrawlData(IData data, bool skipInheritedInterfaces = false)
        {
            var interfaceType = data.DataSourceId.InterfaceType;
            var fields        = DataTypeSearchReflectionHelper.GetSearchableFields(interfaceType);

            if (data is IPage page)
            {
                _currentPage = page;
            }

            foreach (var field in fields)
            {
                var propertyInfo = field.Key;

                if (skipInheritedInterfaces && propertyInfo.DeclaringType != interfaceType)
                {
                    continue;
                }

                var fieldProcessor = DataTypeSearchReflectionHelper.GetDataFieldProcessor(propertyInfo);

                object value = propertyInfo.GetValue(data);
                if (value == null)
                {
                    continue;
                }

                var attr = field.Value;

                // Text indexing
                if (attr.IndexText)
                {
                    var textParts = fieldProcessor.GetTextParts(value);
                    if (textParts != null)
                    {
                        _textParts.AddRange(textParts.SelectMany(ProcessXhtml));
                    }
                }

                // Field previewing
                if (attr.Previewable)
                {
                    var indexValue = fieldProcessor.GetIndexValue(value);
                    if (indexValue != null)
                    {
                        _fieldValues.Add(new KeyValuePair <string, object>(
                                             fieldProcessor.GetDocumentFieldName(propertyInfo),
                                             indexValue));
                    }
                }

                // Faceted fields
                if (attr.Faceted)
                {
                    string[] facetValues = fieldProcessor.GetFacetValues(value);
                    if (facetValues != null && facetValues.Length > 0)
                    {
                        _facetFieldValues.Add(new KeyValuePair <string, string[]>(
                                                  fieldProcessor.GetDocumentFieldName(propertyInfo),
                                                  facetValues));
                    }
                }
            }

            _extensions?.ForEach(e =>
            {
                try
                {
                    e.Populate(this, data);
                }
                catch (Exception ex)
                    when(!(ex is ThreadAbortException))
                    {
                        Log.LogError(nameof(SearchDocumentBuilder), ex);
                    }
            });
        }