Ejemplo n.º 1
0
        private FieldSelection <T> _GetFieldSelection <T>(string path) where T : class
        {
            var f        = new FieldSelection <T>();
            var response = this.Connection.GetSync(path);

            if (response.Result == null)             //a 404 is hit when there is an attempt to grab a non existant document by id, this causes the 'result' to be null
            {
                return(null);
            }

            var o      = JObject.Parse(response.Result);
            var source = o["_source"];

            if (source != null)
            {
                f.Document = this.Deserialize <T>(source.ToString());
            }
            source = o["fields"];
            if (source != null)
            {
                var json = source.ToString();
                f.Document    = this.Deserialize <T>(json);
                f.FieldValues = this.Deserialize <Dictionary <string, object> >(json);
            }
            return(f);
        }
        private static void CreateMultiHit <T>(MultiHitTuple tuple, JsonSerializer serializer, ICollection <IMultiGetHit <object> > collection) where T : class
        {
            var hit    = new MultiGetHit <T>();
            var reader = tuple.Hit.CreateReader();

            serializer.Populate(reader, hit);

            var settings = serializer.GetConnectionSettings();
            var f        = new FieldSelection <T>(settings);
            var source   = tuple.Hit["fields"];

            if (source != null)
            {
                ((IFieldSelection <T>)f).FieldValuesDictionary = serializer.Deserialize <Dictionary <string, object> >(source.CreateReader());
                hit.FieldSelection = f;
            }

            collection.Add(hit);
        }
Ejemplo n.º 3
0
        private static dynamic GetConcreteTypeUsingSelector(
            JsonSerializer serializer,
            ConcreteTypeConverter <T> realConcreteConverter,
            JObject jObject, out object selection)
        {
            var elasticContractResolver = serializer.ContractResolver as ElasticContractResolver;
            var baseType = realConcreteConverter._baseType;
            var selector = realConcreteConverter._concreteTypeSelector;

            //Hit<dynamic> hitDynamic = new Hit<dynamic>();
            dynamic d                  = jObject;
            var     fields             = jObject["fields"];
            var     fieldSelectionData = fields != null?fields.ToObject <IDictionary <string, object> >() : null;

            var sel        = new FieldSelection <T>(elasticContractResolver.ConnectionSettings, fieldSelectionData);
            var hitDynamic = new Hit <dynamic>();

            //favor manual mapping over doing Populate twice.
            hitDynamic._fields     = fieldSelectionData;
            hitDynamic.Fields      = sel;
            hitDynamic.Source      = d._source;
            hitDynamic.Index       = d._index;
            hitDynamic.Score       = (d._score is double) ? d._score : default(double);
            hitDynamic.Type        = d._type;
            hitDynamic.Version     = d._version;
            hitDynamic.Id          = d._id;
            hitDynamic.Sorts       = d.sort;
            hitDynamic._Highlight  = d.highlight is Dictionary <string, List <string> >?d.highlight : null;
            hitDynamic.Explanation = d._explanation is Explanation ? d._explanation : null;
            object o            = d._source ?? DynamicDictionary.Create(fieldSelectionData) ?? new object {};
            var    concreteType = selector(o, hitDynamic);

            Type fieldSelectionType;

            if (!TypeToFieldTypes.TryGetValue(concreteType, out fieldSelectionType))
            {
                fieldSelectionType = typeof(FieldSelection <>).MakeGenericType(concreteType);
                TypeToFieldTypes.TryAdd(concreteType, fieldSelectionType);
            }
            selection = fieldSelectionType.CreateInstance(elasticContractResolver.ConnectionSettings, fieldSelectionData);
            return(concreteType);
        }
Ejemplo n.º 4
0
        private IGetResponse <T> _GetFull <T>(string path) where T : class
        {
            var response    = this.Connection.GetSync(path);
            var getResponse = this.ToParsedResponse <GetResponse <T> >(response);

            if (response.Result != null)
            {
                var f      = new FieldSelection <T>();
                var o      = JObject.Parse(response.Result);
                var source = o["fields"];
                if (source != null)
                {
                    var json = source.ToString();
                    f.Document    = getResponse.Source;
                    f.FieldValues = this.Deserialize <Dictionary <string, object> >(json);
                }
                getResponse.Fields = f;
            }

            return(getResponse);
        }
Ejemplo n.º 5
0
 //This method is used through reflection (cached though)
 // do not remove
 private static void SetFields <TFieldsType>(
     Hit <TFieldsType> hit, FieldSelection <TFieldsType> fieldSelection)
     where TFieldsType : class
 {
     hit.Fields = fieldSelection;
 }