Example #1
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;
        }
        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;
        }
Example #3
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);
        }
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettingsValues settings, K queryString)
            where K : FluentRequestParameters <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.RequestParameters = queryString ?? new K();
            pathInfo.RequestParameters.RequestConfiguration(r => this._RequestConfiguration);
            return(pathInfo);
        }
Example #5
0
        public static void SetRouteParameters <TParameters>(
            IIndexTypePath <TParameters> path,
            IConnectionSettingsValues settings,
            ElasticsearchPathInfo <TParameters> pathInfo)
            where TParameters : IRequestParameters, new()
        {
            var inferrer = new ElasticInferrer(settings);

            if (path.Index == null)
            {
                if (path.Type != null && path.Type.Type != null)
                {
                    path.Index = path.Type.Type;
                }
                else
                {
                    throw new DslException("Index() not specified");
                }
            }

            if (path.Type == null)
            {
                throw new DslException("Type() not specified");
            }

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

            pathInfo.Index = index;
            pathInfo.Type  = type;
        }
        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);
        }
        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);
        }
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettings settings, K queryString)
            where K : FluentQueryString <K>, new()
        {
            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 = new ElasticsearchPathInfo <K>()
            {
                Index = index,
                Type  = type
            };

            pathInfo.QueryString = queryString ?? new K();
            return(pathInfo);
        }
        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);
        }
Example #10
0
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettingsValues settings, K queryString)
            where K : FluentRequestParameters <K>, new()
        {
            //start out with defaults
            var inferrer = new ElasticInferrer(settings);
            var index    = inferrer.IndexName <T>();
            var type     = inferrer.TypeName <T>();
            var pathInfo = new ElasticsearchPathInfo <K>()
            {
                Index = index,
                Type  = type
            };

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

            if (this._Indices.HasAny())
            {
                pathInfo.Index = inferrer.IndexNames(this._Indices);
            }
            else if (this._AllIndices && !pathInfo.Type.IsNullOrEmpty())
            {
                pathInfo.Index = "_all";
            }
            else
            {
                pathInfo.Index = this._AllIndices ? null : inferrer.IndexName <T>();
            }



            pathInfo.RequestParameters = queryString ?? new K();
            pathInfo.RequestParameters.RequestConfiguration(r => this._RequestConfiguration);
            return(pathInfo);
        }
Example #11
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 indices = inferrer.IndexNames(operation._Indices);
                if (operation._AllIndices.GetValueOrDefault(false))
                {
                    indices = "_all";
                }

                var index = indices
                            ?? inferrer.IndexName(multiSearchDescriptor._Index)
                            ?? inferrer.IndexName(operation._ClrType);

                var types    = inferrer.TypeNames(operation._Types);
                var typeName = types
                               ?? inferrer.TypeName(multiSearchDescriptor._Type)
                               ?? inferrer.TypeName(operation._ClrType);
                if (operation._AllTypes.GetValueOrDefault(false))
                {
                    typeName = null;                     //force empty typename so we'll query all types.
                }
                var op = new
                {
                    index       = index,
                    type        = typeName,
                    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);
        }
Example #12
0
        public static void SetRouteParameters <TParameters, T>(
            IQueryPath <TParameters> path,
            IConnectionSettingsValues settings,
            ElasticsearchPathInfo <TParameters> pathInfo)
            where TParameters : IRequestParameters, new()
            where T : class
        {
            //start out with defaults
            var inferrer = new ElasticInferrer(settings);


            var index = inferrer.IndexName <T>();
            var type  = inferrer.TypeName <T>();

            pathInfo.Index = index;
            pathInfo.Type  = type;

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

            if (path.Indices.HasAny())
            {
                pathInfo.Index = inferrer.IndexNames(path.Indices);
            }
            else if (path.AllIndices.GetValueOrDefault(false) && !pathInfo.Type.IsNullOrEmpty())
            {
                pathInfo.Index = "_all";
            }
            else
            {
                pathInfo.Index = path.AllIndices.GetValueOrDefault(false) ? null : inferrer.IndexName <T>();
            }
        }
Example #13
0
        internal virtual ElasticsearchPathInfo <K> ToPathInfo <K>(IConnectionSettings settings, K queryString)
            where K : FluentQueryString <K>, new()
        {
            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 = new ElasticsearchPathInfo <K>()
            {
                Index = index,
                Type  = type,
                Id    = id
            };

            pathInfo.QueryString = queryString ?? new K();
            return(pathInfo);
        }
Example #14
0
        public string SerializeBulkDescriptor(BulkDescriptor bulkDescriptor)
        {
            bulkDescriptor.ThrowIfNull("bulkDescriptor");
            bulkDescriptor._Operations.ThrowIfEmpty("Bulk descriptor does not define any operations");
            var sb       = new StringBuilder();
            var inferrer = new ElasticInferrer(this._settings);

            foreach (var operation in bulkDescriptor._Operations)
            {
                var command = operation._Operation;
                var index   = operation._Index
                              ?? inferrer.IndexName(bulkDescriptor._Index)
                              ?? inferrer.IndexName(operation._ClrType);
                var typeName = operation._Type
                               ?? inferrer.TypeName(bulkDescriptor._Type)
                               ?? inferrer.TypeName(operation._ClrType);

                var id = operation.GetIdForObject(inferrer);
                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);

                if (command == "index" || command == "create")
                {
                    var jsonCommand = this.Serialize(operation._Object, SerializationFormatting.None).Utf8String();
                    sb.Append(jsonCommand + "\n");
                }
                else if (command == "update")
                {
                    var jsonCommand = this.Serialize(operation.GetBody(), SerializationFormatting.None).Utf8String();
                    sb.Append(jsonCommand + "\n");
                }
            }
            var json = sb.ToString();

            return(json);
        }
        internal virtual ElasticsearchPathInfo <TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
        {
            //start out with defaults
            var inferrer = new ElasticInferrer(settings);
            var index    = inferrer.IndexName <T>();
            var type     = inferrer.TypeName <T>();
            var pathInfo = base.ToPathInfo(queryString);

            pathInfo.Index = index;
            pathInfo.Type  = type;

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

            if (this._Indices.HasAny())
            {
                pathInfo.Index = inferrer.IndexNames(this._Indices);
            }
            else if (this._AllIndices && !pathInfo.Type.IsNullOrEmpty())
            {
                pathInfo.Index = "_all";
            }
            else
            {
                pathInfo.Index = this._AllIndices ? null : inferrer.IndexName <T>();
            }

            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);
        }
Example #17
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);
        }
 string IUrlParameter.GetString(IConnectionConfigurationValues settings)
 {
     return(this.Match(
                all => null,
                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 types = this.Item2.Types.Select(t => infer.TypeName(t)).Distinct();
         return string.Join(",", types);
     }
                ));
 }
Example #19
0
        public PutTemplateDescriptor AddMapping <T>(Func <PutMappingDescriptor <T>, PutMappingDescriptor <T> > mappingSelector)
            where T : class
        {
            mappingSelector.ThrowIfNull("mappingSelector");
            var rootObjectMappingDescriptor = mappingSelector(new PutMappingDescriptor <T>(this._connectionSettings));

            rootObjectMappingDescriptor.ThrowIfNull("rootObjectMappingDescriptor");

            var inferrer = new ElasticInferrer(this._connectionSettings);
            var typeName = inferrer.TypeName(rootObjectMappingDescriptor._Type);

            if (typeName == null)
            {
                return(this);
            }
            this._TemplateMapping.Mappings[typeName] = rootObjectMappingDescriptor._Mapping;
            return(this);
        }
Example #20
0
        public PutTemplateDescriptor AddMapping <T>(Func <PutMappingDescriptor <T>, PutMappingDescriptor <T> > mappingSelector)
            where T : class
        {
            mappingSelector.ThrowIfNull("mappingSelector");
            var putMappingDescriptor = mappingSelector(new PutMappingDescriptor <T>(this._connectionSettings));

            putMappingDescriptor.ThrowIfNull("rootObjectMappingDescriptor");

            var inferrer = new ElasticInferrer(this._connectionSettings);
            IPutMappingRequest request = putMappingDescriptor;
            var typeName = inferrer.TypeName(request.Type ?? typeof(T));

            if (typeName == null)
            {
                return(this);
            }
            Self.TemplateMapping.Mappings[typeName] = request.Mapping;
            return(this);
        }
Example #21
0
        internal virtual ElasticsearchPathInfo <TParameters> ToPathInfo(IConnectionSettingsValues settings, TParameters queryString)
        {
            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 = inferrer.IndexName(this._Index);
            var type  = inferrer.TypeName(this._Type);

            var pathInfo = base.ToPathInfo(queryString);

            pathInfo.Index = index;
            pathInfo.Type  = type;
            return(pathInfo);
        }