public static T GetObjectFieldValue <T>(this SearchDocument document)
            where T : class
        {
            T result = null;

            if (document.ContainsKey(ObjectFieldName))
            {
                var obj = document[ObjectFieldName];

                result = obj as T;
                if (result == null)
                {
                    var jobj = obj as JObject;
                    if (jobj != null)
                    {
                        result = jobj.ToObject <T>();
                    }
                    else
                    {
                        var productString = obj as string;
                        if (!string.IsNullOrEmpty(productString))
                        {
                            result = DeserializeObject <T>(productString);
                        }
                    }
                }
            }

            return(result);
        }
        public static T GetObjectFieldValue <T>(this SearchDocument document)
            where T : class
        {
            T result = null;

            if (document.ContainsKey(ObjectFieldName))
            {
                var obj     = document[ObjectFieldName];
                var objType = AbstractTypeFactory <T> .TryCreateInstance().GetType();

                result = obj as T;
                if (result == null)
                {
                    if (obj is JObject jobj)
                    {
                        result = jobj.ToObject(objType) as T;
                    }
                    else
                    {
                        var productString = obj as string;
                        if (!string.IsNullOrEmpty(productString))
                        {
                            result = DeserializeObject(productString, objType) as T;
                        }
                    }
                }
            }

            return(result);
        }
        public virtual object BindModel(SearchDocument doc)
        {
            var result = default(CatalogProduct);

            var fieldName = BindingInfo.FieldName;

            if (doc.ContainsKey(fieldName))
            {
                var obj = doc[fieldName];

                if (obj is JObject jobj)
                {
                    result = (CatalogProduct)jobj.ToObject(_productType);

                    var productProperties = result.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

                    foreach (var property in productProperties)
                    {
                        var binder = property.GetIndexModelBinder();

                        if (binder != null)
                        {
                            property.SetValue(result, binder.BindModel(doc));
                        }
                    }
                }
            }
            else
            {
                throw new  InvalidOperationException($"{BindingInfo.FieldName} is missed in index data. Unable to load CatalogProduct object from index.");
            }
            return(result);
        }
示例#4
0
        public virtual object BindModel(SearchDocument searchDocument)
        {
            var result = default(CatalogProduct);

            if (!searchDocument.ContainsKey(BindingInfo.FieldName))
            {
                // No object in index
                return(result);
            }

            var obj = searchDocument[BindingInfo.FieldName];

            if (obj is JObject jobj)
            {
                result = (CatalogProduct)jobj.ToObject(_productType);

                var productProperties = result.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

                foreach (var property in productProperties)
                {
                    var binder = property.GetIndexModelBinder();

                    if (binder != null)
                    {
                        property.SetValue(result, binder.BindModel(searchDocument));
                    }
                }
            }

            return(result);
        }
        public virtual object BindModel(SearchDocument doc)
        {
            var result = new List <Price>();

            if (doc.ContainsKey(BindingInfo.FieldName))
            {
                var obj = doc[BindingInfo.FieldName];
                if (obj is Array jobjArray)
                {
                    var prices = jobjArray.OfType <JObject>().Select(x => (Price)x.ToObject(typeof(Price)));
                    result.AddRange(prices);
                }
            }
            else
            {
                foreach (var pair in doc)
                {
                    var match = _priceFieldRegExp.Match(pair.Key);
                    if (match.Success)
                    {
                        foreach (var listPrice in pair.Value is Array ? (object[])pair.Value : new[] { pair.Value })
                        {
                            var price = new Price
                            {
                                Currency    = match.Groups[1].Value,
                                PricelistId = match.Groups[2].Value,
                                List        = Convert.ToDecimal(listPrice)
                            };
                            result.Add(price);
                        }
                    }
                }
            }
            return(result);
        }
        public virtual object BindModel(SearchDocument searchDocument)
        {
            var fieldName = BindingInfo.FieldName;

            return(searchDocument.ContainsKey(fieldName) && searchDocument[fieldName] is object[] objs
                ? objs.Select(x => (string)x).ToList()
                : Enumerable.Empty <string>().ToList());
        }
        public object BindModel(SearchDocument searchDocument)
        {
            var fieldName = BindingInfo?.FieldName;

            if (!string.IsNullOrEmpty(fieldName) && searchDocument.ContainsKey(fieldName))
            {
                return(searchDocument[fieldName]);
            }
            return(null);
        }
示例#8
0
        protected virtual SearchDocument ConvertToProviderDocument(IndexDocument document, Properties <IProperties> properties, string documentType)
        {
            var result = new SearchDocument {
                Id = document.Id
            };

            foreach (var field in document.Fields.OrderBy(f => f.Name))
            {
                var fieldName = ElasticSearchHelper.ToElasticFieldName(field.Name);

                if (result.ContainsKey(fieldName))
                {
                    var newValues = new List <object>();

                    var currentValue  = result[fieldName];
                    var currentValues = currentValue as object[];

                    if (currentValues != null)
                    {
                        newValues.AddRange(currentValues);
                    }
                    else
                    {
                        newValues.Add(currentValue);
                    }

                    newValues.AddRange(field.Values);
                    result[fieldName] = newValues.ToArray();
                }
                else
                {
                    var dictionary = properties as IDictionary <PropertyName, IProperty>;
                    if (dictionary != null && !dictionary.ContainsKey(fieldName))
                    {
                        // Create new property mapping
                        var providerField = CreateProviderField(field, documentType);
                        ConfigureProperty(providerField, field, documentType);
                        properties.Add(fieldName, providerField);
                    }

                    var isCollection = field.IsCollection || field.Values.Count > 1;

                    var point = field.Value as GeoPoint;
                    var value = point != null
                        ? (isCollection ? field.Values.Select(v => ((GeoPoint)v).ToElasticValue()).ToArray() : point.ToElasticValue())
                        : (isCollection ? field.Values : field.Value);

                    result.Add(fieldName, value);
                }
            }

            return(result);
        }
        public virtual object BindModel(SearchDocument searchDocument)
        {
            var fieldName = BindingInfo.FieldName;

            if (searchDocument.ContainsKey(BindingInfo.FieldName) && searchDocument[fieldName] is JObject jobj)
            {
                return((Category)jobj.ToObject(_productType));
            }

            // No object in index
            return(null);
        }
        private static SearchDocument ToSearchDocument(Document providerDocument, ICollection <string> availableFields)
        {
            var result = new SearchDocument();

            var documentFields = providerDocument.GetFields();

            foreach (var field in documentFields)
            {
                var stringValue = field.StringValue;

                if (field.Name.EqualsInvariant(LuceneSearchHelper.KeyFieldName))
                {
                    result.Id = stringValue;
                }
                else
                {
                    var isDateTimeField = availableFields.Contains(LuceneSearchHelper.GetDateTimeFieldName(field.Name));
                    if (isDateTimeField)
                    {
                        var ticks = long.Parse(stringValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
                        stringValue = new DateTime(ticks, DateTimeKind.Utc).ToString("O");
                    }

                    if (result.ContainsKey(field.Name)) // convert to array
                    {
                        var newValues = new List <object>();

                        var currentValue  = result[field.Name];
                        var currentValues = currentValue as object[];

                        if (currentValues != null)
                        {
                            newValues.AddRange(currentValues);
                        }
                        else
                        {
                            newValues.Add(currentValue);
                        }

                        newValues.Add(stringValue);
                        result[field.Name] = newValues.ToArray();
                    }
                    else
                    {
                        result.Add(field.Name, stringValue);
                    }
                }
            }

            return(result);
        }
示例#11
0
        protected virtual SearchDocument ConvertToProviderDocument(IndexDocument document, IList <Field> providerFields, string documentType)
        {
            var result = new SearchDocument {
                Id = document.Id
            };

            document.Fields.Insert(0, new IndexDocumentField(AzureSearchHelper.RawKeyFieldName, document.Id)
            {
                IsRetrievable = true, IsFilterable = true
            });

            foreach (var field in document.Fields.OrderBy(f => f.Name))
            {
                var fieldName = AzureSearchHelper.ToAzureFieldName(field.Name);

                if (result.ContainsKey(fieldName))
                {
                    var newValues = new List <object>();

                    var currentValue = result[fieldName];

                    if (currentValue is object[] currentValues)
                    {
                        newValues.AddRange(currentValues);
                    }
                    else
                    {
                        newValues.Add(currentValue);
                    }

                    newValues.AddRange(field.Values);
                    result[fieldName] = newValues.ToArray();
                }
                else
                {
                    var providerField = AddProviderField(documentType, providerFields, fieldName, field);
                    var isCollection  = providerField.Type.ToString().StartsWith("Collection(");

                    var point = field.Value as GeoPoint;
                    var value = point != null
                        ? (isCollection ? field.Values.Select(v => ((GeoPoint)v).ToDocumentValue()).ToArray() : point.ToDocumentValue())
                        : (isCollection ? field.Values : field.Value);

                    result.Add(fieldName, value);
                }
            }

            return(result);
        }
        public object BindModel(SearchDocument doc)
        {
            var result = new List <Inventory>();

            if (doc.ContainsKey(BindingInfo.FieldName))
            {
                var obj = doc[BindingInfo.FieldName];
                if (obj is Array jobjArray)
                {
                    var inventories = jobjArray.OfType <JObject>().Select(x => (Inventory)x.ToObject(typeof(Inventory)));
                    result.AddRange(inventories);
                }
            }

            return(result);
        }
        public virtual object BindModel(SearchDocument searchDocument)
        {
            var result = new List <Price>();

            if (searchDocument.ContainsKey(BindingInfo.FieldName))
            {
                var obj = searchDocument[BindingInfo.FieldName];
                if (obj is Array jobjArray)
                {
                    var prices = jobjArray.OfType <JObject>().Select(x => (Price)x.ToObject(typeof(Price)));
                    result.AddRange(prices);
                }
            }
            else
            {
                foreach (var pair in searchDocument)
                {
                    var match = _priceFieldRegExp.Match(pair.Key);
                    if (!match.Success)
                    {
                        continue;
                    }

                    foreach (var listPrice in pair.Value is Array ? (object[])pair.Value : new[] { pair.Value })
                    {
                        // If schema field required without filled values
                        if (listPrice is null)
                        {
                            continue;
                        }

                        var price = new Price
                        {
                            Currency    = match.Groups[1].Value.ToUpperInvariant(),
                            PricelistId = match.Groups[2].Value,
                            List        = Convert.ToDecimal(listPrice)
                        };

                        result.Add(price);
                    }
                }
            }
            return(result);
        }