/// <summary>
        /// Based on the type information present in this descriptor create method that takes
        /// the returned _source and hit and returns the ClrType it should deserialize too.
        /// This is so that Documents[A] can contain actual instances of subclasses B, C as well.
        /// If you specify types using .Types(typeof(B), typeof(C)) then NEST can automagically
        /// create a TypeSelector based on the hits _type property.
        /// </summary>
        public static void CloseOverAutomagicCovariantResultSelector(ElasticInferrer infer, ISearchTemplateRequest self)
        {
            if (infer == null || self == null)
            {
                return;
            }
            var returnType = self.ClrType;

            if (returnType == null)
            {
                return;
            }

            var types = (self.Types ?? Enumerable.Empty <TypeNameMarker>()).Where(t => t.Type != null).ToList();

            if (self.TypeSelector != null || !types.HasAny(t => t.Type != returnType))
            {
                return;
            }

            var typeDictionary = types.ToDictionary(infer.TypeName, t => t.Type);

            self.TypeSelector = (o, h) =>
            {
                Type t;
                return(!typeDictionary.TryGetValue(h.Type, out t) ? returnType : t);
            };
        }
Пример #2
0
        /// <summary>
        /// _msearch needs a specialized json format in the body
        /// </summary>
        public string SerializeMultiSearch(IMultiSearchRequest multiSearchRequest)
        {
            var sb        = new StringBuilder();
            var inferrer  = new ElasticInferrer(this._settings);
            var indexName = inferrer.IndexName(multiSearchRequest.Index);

            foreach (var operation in multiSearchRequest.Operations.Values)
            {
                var path = operation.ToPathInfo(this._settings);
                if (path.Index == indexName)
                {
                    path.Index = null;
                }

                var op = new
                {
                    index              = path.Index,
                    type               = path.Type,
                    search_type        = this.GetSearchType(operation, multiSearchRequest),
                    preference         = operation.Preference,
                    routing            = operation.Routing,
                    ignore_unavailable = operation.IgnoreUnavalable
                };
                var opJson = this.Serialize(op, SerializationFormatting.None).Utf8String();

                var action = "{0}\n".F(opJson);
                sb.Append(action);
                var searchJson = this.Serialize(operation, SerializationFormatting.None).Utf8String();
                sb.Append(searchJson + "\n");
            }
            var json = sb.ToString();

            return(json);
        }
Пример #3
0
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettingsValues settings, K queryString)
            where K : FluentRequestParameters <K>, new()
        {
            var inferrer = new ElasticInferrer(settings);

            if (this._Index == null)
            {
                throw new DslException("Index() not specified");
            }
            if (this._Type == null)
            {
                throw new DslException("Type() not specified");
            }

            var index    = new ElasticInferrer(settings).IndexName(this._Index);
            var type     = new ElasticInferrer(settings).TypeName(this._Type);
            var pathInfo = new ElasticsearchPathInfo <K>()
            {
                Index = index,
                Type  = type
            };

            pathInfo.RequestParameters = queryString ?? new K();
            pathInfo.RequestParameters.RequestConfiguration(r => this._RequestConfiguration);
            return(pathInfo);
        }
Пример #4
0
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettings settings, K queryString)
            where K : FluentQueryString <K>, new()
        {
            var inferrer = new ElasticInferrer(settings);

            if (this._Index == null)
            {
                this._Index = inferrer.IndexName <T>();
            }
            if (this._Type == null)
            {
                this._Type = inferrer.TypeName <T>();
            }

            var index    = new ElasticInferrer(settings).IndexName(this._Index);
            var type     = new ElasticInferrer(settings).TypeName(this._Type);
            var pathInfo = new ElasticsearchPathInfo <K>()
            {
                Index = index,
                Type  = type
            };

            pathInfo.QueryString = queryString ?? new K();
            return(pathInfo);
        }
Пример #5
0
        public static void SetRouteParameters <TParameters, T>(
            IIndicesTypePath <TParameters> path,
            IConnectionSettingsValues settings,
            ElasticsearchPathInfo <TParameters> pathInfo)
            where TParameters : IRequestParameters, new()
            where T : class
        {
            var inferrer = new ElasticInferrer(settings);

            if (path.Type == null)
            {
                path.Type = inferrer.TypeName <T>();
            }

            var index = !path.Indices.HasAny()
                                ? inferrer.IndexName <T>()
                                : string.Join(",", path.Indices.Select(inferrer.IndexName));

            if (path.AllIndices.GetValueOrDefault(false))
            {
                index = "_all";
            }

            var type = inferrer.TypeName(path.Type);

            pathInfo.Index = index;
            pathInfo.Type  = type;
        }
Пример #6
0
        public static void SetRouteParameters <TParameters, T>(
            IIndexTypePath <TParameters> path,
            IConnectionSettingsValues settings,
            ElasticsearchPathInfo <TParameters> pathInfo)
            where TParameters : IRequestParameters, new()
            where T : class
        {
            var inferrer = new ElasticInferrer(settings);

            if (path.Index == null)
            {
                path.Index = inferrer.IndexName <T>();
            }

            if (path.Type == null)
            {
                path.Type = inferrer.TypeName <T>();
            }

            var index = inferrer.IndexName(path.Index);
            var type  = inferrer.TypeName(path.Type);

            pathInfo.Index = index;
            pathInfo.Type  = type;
        }
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettings settings, K queryString)
            where K : FluentQueryString <K>, new()
        {
            var inferrer = new ElasticInferrer(settings);

            if (!this._AllIndices.HasValue && this._Indices == null)
            {
                this._Indices = new[] { (IndexNameMarker)inferrer.DefaultIndex }
            }
            ;

            string index = "_all";

            if (!this._AllIndices.GetValueOrDefault(false))
            {
                index = string.Join(",", this._Indices.Select(inferrer.IndexName));
            }

            var pathInfo = new ElasticsearchPathInfo <K>()
            {
                Index = index,
            };

            pathInfo.QueryString = queryString ?? new K();
            return(pathInfo);
        }
    }
Пример #8
0
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettingsValues settings, K queryString)
            where K : FluentRequestParameters <K>, new()
        {
            var inferrer = new ElasticInferrer(settings);

            if (!this._AllIndices.HasValue && this._Index == null)
            {
                this._Index = inferrer.DefaultIndex;
            }

            string index = null;

            if (!this._AllIndices.GetValueOrDefault(false))
            {
                index = inferrer.IndexName(this._Index);
            }

            var pathInfo = new ElasticsearchPathInfo <K>()
            {
                Index = index,
            };

            pathInfo.RequestParameters = queryString ?? new K();
            pathInfo.RequestParameters.RequestConfiguration(r => this._RequestConfiguration);
            return(pathInfo);
        }
Пример #9
0
        /// <summary>
        /// _msearch needs a specialized json format in the body
        /// </summary>
        public string SerializeMultiSearch(MultiSearchDescriptor multiSearchDescriptor)
        {
            var sb       = new StringBuilder();
            var inferrer = new ElasticInferrer(this._settings);

            foreach (var operation in multiSearchDescriptor._Operations.Values)
            {
                var path = operation.ToPathInfo(this._settings);
                var op   = new
                {
                    index       = path.Index,
                    type        = path.Type,
                    search_type = this.GetSearchType(operation, multiSearchDescriptor),
                    preference  = operation._Preference,
                    routing     = operation._Routing
                };
                var opJson = this.Serialize(op, SerializationFormatting.None).Utf8String();

                var action = "{0}\n".F(opJson);
                sb.Append(action);
                var searchJson = this.Serialize(operation, SerializationFormatting.None).Utf8String();
                sb.Append(searchJson + "\n");
            }
            var json = sb.ToString();

            return(json);
        }
Пример #10
0
        public static void SetRouteParameters <TParameters>(
            IQueryPath <TParameters> path,
            IConnectionSettingsValues settings,
            ElasticsearchPathInfo <TParameters> pathInfo)
            where TParameters : IRequestParameters, new()
        {
            var inferrer = new ElasticInferrer(settings);

            if (path.Types.HasAny())
            {
                pathInfo.Type = inferrer.TypeNames(path.Types);
            }
            else if (path.AllTypes.GetValueOrDefault(false))
            {
                pathInfo.Type = null;
            }

            if (path.Indices.HasAny())
            {
                pathInfo.Index = inferrer.IndexNames(path.Indices);
            }
            else if (path.AllIndices.GetValueOrDefault(false) && !pathInfo.Type.IsNullOrEmpty())
            {
                pathInfo.Index = "_all";
            }
            else if (!path.AllIndices.GetValueOrDefault(false) && pathInfo.Index.IsNullOrEmpty())
            {
                pathInfo.Index = inferrer.DefaultIndex;
            }
        }
        internal virtual ElasticsearchPathInfo <TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
        {
            var inferrer = new ElasticInferrer(settings);

            if (this._Type == null)
            {
                this._Type = inferrer.TypeName <T>();
            }

            var index = !this._Indices.HasAny()
                                ? inferrer.IndexName <T>()
                                : string.Join(",", this._Indices.Select(inferrer.IndexName));

            if (this._AllIndices.GetValueOrDefault(false))
            {
                index = "_all";
            }

            var type     = new ElasticInferrer(settings).TypeName(this._Type);
            var pathInfo = base.ToPathInfo(queryString);

            pathInfo.Index = index;
            pathInfo.Type  = type;
            return(pathInfo);
        }
 public PutMappingDescriptor(IConnectionSettingsValues connectionSettings)
 {
     this._connectionSettings = connectionSettings;
     this._Mapping            = new RootObjectMapping()
     {
     };
     this.Infer = new ElasticInferrer(this._connectionSettings);
 }
Пример #13
0
 internal GetFieldMappingResponse(IApiCallDetails status, IndexFieldMappings dict, ElasticInferrer inferrer)
 {
     this.Indices   = dict ?? new IndexFieldMappings();
     this._inferrer = inferrer;
     //TODO can dict truely ever be null, whats the response look like when field mapping is not found.
     //does status.Success not already reflect this?
     //this.IsValid = status.Success && dict != null && dict.Count > 0;
 }
Пример #14
0
 public NestedObjectMappingDescriptor(IConnectionSettingsValues connectionSettings)
 {
     this._connectionSettings = connectionSettings;
     this._TypeName           = TypeNameMarker.Create <TChild>();
     this._Mapping            = new NestedObjectMapping()
     {
     };
     this.Infer = new ElasticInferrer(this._connectionSettings);
 }
Пример #15
0
        internal override string GetIdForObject(ElasticInferrer inferrer)
        {
            if (!this._Id.IsNullOrEmpty())
            {
                return(this._Id);
            }

            return(inferrer.Id((T)_Object));
        }
Пример #16
0
        string IUrlParameter.GetString(IConnectionConfigurationValues settings)
        {
            var nestSettings = settings as IConnectionSettingsValues;

            if (nestSettings == null)
            {
                throw new Exception("Tried to pass field name on querysting but it could not be resolved because no nest settings are available");
            }
            var infer = new ElasticInferrer(nestSettings);

            return(infer.Field(this));
        }
Пример #17
0
        public RawElasticClient(IConnectionSettings settings, IConnection connection = null, IElasticsearchSerializer serializer = null)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.Settings   = settings;
            this.Connection = connection ?? new Connection(settings);
            this.Serializer = serializer ?? new NestSerializer(settings);
            this.Serializer = new NestSerializer(this.Settings);
            this.Infer      = new ElasticInferrer(this.Settings);
        }
        internal virtual ElasticsearchPathInfo <TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
        {
            if (this._Index == null)
            {
                throw new DslException("missing call to Index()");
            }

            var index    = new ElasticInferrer(settings).IndexName(this._Index);
            var pathInfo = base.ToPathInfo(queryString);

            pathInfo.Index = index;
            return(pathInfo);
        }
        internal virtual ElasticsearchPathInfo <TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
        {
            var inferrer = new ElasticInferrer(settings);

            var index = inferrer.IndexName(this._Index);
            var type  = inferrer.TypeName(this._Type);

            var pathInfo = base.ToPathInfo(queryString);

            pathInfo.Index = index;
            pathInfo.Type  = type;
            return(pathInfo);
        }
 public static void Update(IConnectionSettingsValues settings, ElasticsearchPathInfo <IndicesStatsRequestParameters> pathInfo, IIndicesStatsRequest request)
 {
     if (request.Types.HasAny())
     {
         var inferrer = new ElasticInferrer(settings);
         var types    = inferrer.TypeNames(request.Types);
         pathInfo.RequestParameters.AddQueryString("types", string.Join(",", types));
     }
     if (request.Metrics != null)
     {
         pathInfo.Metric = request.Metrics.Cast <Enum>().GetStringValue();
     }
     pathInfo.HttpMethod = PathInfoHttpMethod.GET;
 }
        internal virtual ElasticsearchPathInfo <TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
        {
            if (this._Name == null)
            {
                throw new DslException("missing Repository()");
            }
            var inferrer = new ElasticInferrer(settings);
            var index    = inferrer.IndexName(this._Index) ?? inferrer.DefaultIndex;
            var pathInfo = base.ToPathInfo(queryString);

            pathInfo.Index = index;
            pathInfo.Name  = this._Name;
            return(pathInfo);
        }
Пример #22
0
        public static void SetRouteParameters <TParameters>(
            IIndexPath <TParameters> path,
            IConnectionSettingsValues settings,
            ElasticsearchPathInfo <TParameters> pathInfo)
            where TParameters : IRequestParameters, new()
        {
            if (path.Index == null)
            {
                throw new DslException("missing index route parameter");
            }

            var index = new ElasticInferrer(settings).IndexName(path.Index);

            pathInfo.Index = index;
        }
Пример #23
0
        public ElasticClient(IConnectionSettings settings, IConnection connection = null, INestSerializer serializer = null)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this._connectionSettings = settings;
            this.Connection          = connection ?? new Connection(settings);

            this.Serializer  = serializer ?? new NestSerializer(this._connectionSettings);
            this.Raw         = new RawElasticClient(this._connectionSettings, this.Connection, this.Serializer);
            this.RawDispatch = new RawDispatch(this.Raw);
            this.Infer       = new ElasticInferrer(this._connectionSettings);
        }
        internal virtual ElasticsearchPathInfo <TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
        {
            var inferrer = new ElasticInferrer(settings);
            var index    = this._Index != null?inferrer.IndexName(this._Index) : inferrer.IndexName <T>();

            var type = this._Type != null?inferrer.TypeName(this._Type) : inferrer.TypeName <T>();

            var id = this._Id ?? inferrer.Id(this._Object);

            var pathInfo = base.ToPathInfo(queryString);

            pathInfo.Index = index;
            pathInfo.Type  = type;
            pathInfo.Id    = id;
            return(pathInfo);
        }
Пример #25
0
        public static void SetRouteParameters <TParameters, T>(
            IIndexNamePath <TParameters> path,
            IConnectionSettingsValues settings,
            ElasticsearchPathInfo <TParameters> pathInfo)
            where TParameters : IRequestParameters, new()
            where T : class
        {
            if (path.Name == null)
            {
                throw new DslException("missing name route parameter");
            }
            var inferrer = new ElasticInferrer(settings);
            var index    = inferrer.IndexName(path.Index) ?? inferrer.IndexName(typeof(T)) ?? inferrer.DefaultIndex;

            pathInfo.Index = index;
            pathInfo.Name  = path.Name;
        }
Пример #26
0
        ElasticsearchPathInfo <SearchQueryString> IPathInfo <SearchQueryString> .ToPathInfo(IConnectionSettings settings)
        {
            var pathInfo = new ElasticsearchPathInfo <SearchQueryString>();

            pathInfo.HttpMethod = this._QueryString.ContainsKey("source")
                                ? PathInfoHttpMethod.GET
                                : PathInfoHttpMethod.POST;

            pathInfo.QueryString = this._QueryString;

            var    inferrer = new ElasticInferrer(settings);
            string indices;

            if (this._AllIndices.GetValueOrDefault(false))
            {
                indices = !this._AllTypes.GetValueOrDefault(false) ? "_all" : null;
            }
            else if (this._Indices.HasAny())
            {
                indices = inferrer.IndexNames(this._Indices);
            }
            else
            {
                indices = inferrer.IndexName <T>();
            }

            string types;

            if (this._AllTypes.GetValueOrDefault(false))
            {
                types = null;
            }
            else if (this._Types.HasAny())
            {
                types = inferrer.TypeNames(this._Types);
            }
            else
            {
                types = inferrer.TypeName <T>();
            }

            pathInfo.Index = indices;
            pathInfo.Type  = types;

            return(pathInfo);
        }
Пример #27
0
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettings settings, K queryString)
            where K : FluentQueryString <K>, new()
        {
            if (this._Index == null)
            {
                throw new DslException("missing call to Index()");
            }

            var index    = new ElasticInferrer(settings).IndexName(this._Index);
            var pathInfo = new ElasticsearchPathInfo <K>()
            {
                Index = index,
            };

            pathInfo.QueryString = queryString ?? new K();
            return(pathInfo);
        }
Пример #28
0
        internal virtual ElasticsearchPathInfo <K> ToPathInfo(IConnectionSettingsValues settings, K queryString)
        {
            var inferrer = new ElasticInferrer(settings);

            var index = inferrer.IndexName(this._Index);
            var type  = inferrer.TypeName(this._Type);

            var pathInfo = new ElasticsearchPathInfo <K>()
            {
                Index = index,
                Type  = type
            };

            pathInfo.RequestParameters = queryString ?? new K();
            pathInfo.RequestParameters.RequestConfiguration(r => this._RequestConfiguration);
            return(pathInfo);
        }
Пример #29
0
        public string SerializeBulkDescriptor(IBulkRequest bulkRequest)
        {
            bulkRequest.ThrowIfNull("bulkRequest");
            bulkRequest.Operations.ThrowIfEmpty("Bulk request does not define any operations");
            var sb       = new StringBuilder();
            var inferrer = new ElasticInferrer(this._settings);

            foreach (var operation in bulkRequest.Operations)
            {
                var command = operation.Operation;
                var index   = operation.Index
                              ?? inferrer.IndexName(bulkRequest.Index)
                              ?? inferrer.IndexName(operation.ClrType);
                var typeName = operation.Type
                               ?? inferrer.TypeName(bulkRequest.Type)
                               ?? inferrer.TypeName(operation.ClrType);

                var id = operation.GetIdForOperation(inferrer);
                if (index.EqualsMarker(bulkRequest.Index))
                {
                    operation.Index = null;
                }
                else
                {
                    operation.Index = index;
                }

                operation.Type = typeName;
                operation.Id   = id;

                var opJson = this.Serialize(operation, SerializationFormatting.None).Utf8String();

                var action = "{{ \"{0}\" :  {1} }}\n".F(command, opJson);
                sb.Append(action);
                var body = operation.GetBody();
                if (body == null)
                {
                    continue;
                }
                var jsonCommand = this.Serialize(body, SerializationFormatting.None).Utf8String();
                sb.Append(jsonCommand + "\n");
            }
            var json = sb.ToString();

            return(json);
        }
Пример #30
0
 string IUrlParameter.GetString(IConnectionConfigurationValues settings)
 {
     return(this.Match(
                all => "_all",
                many =>
     {
         var nestSettings = settings as IConnectionSettingsValues;
         if (nestSettings == null)
         {
             throw new Exception("Tried to pass field name on querysting but it could not be resolved because no nest settings are available");
         }
         var infer = new ElasticInferrer(nestSettings);
         var indices = many.Indices.Select(i => infer.IndexName(i)).Distinct();
         return string.Join(",", indices);
     }
                ));
 }