Beispiel #1
0
        /// <inheritdoc />
        public IBulkResponse IndexMany <T>(IEnumerable <T> @objects, string index = null, string type = null) where T : class
        {
            @objects.ThrowIfEmpty("objects");
            var bulk = new BulkDescriptor().FixedPath(index, type);

            foreach (var o in @objects)
            {
                var o1 = o;
                bulk.Index <T>(b => b.Object(o1));
            }
            return(Bulk(b => bulk));
        }
Beispiel #2
0
        public IBulkResponse Bulk(BulkDescriptor bulkDescriptor)
        {
            bulkDescriptor.ThrowIfNull("bulkDescriptor");
            bulkDescriptor._Operations.ThrowIfEmpty("Bulk descriptor does not define any operations");
            var sb = new StringBuilder();

            foreach (var operation in bulkDescriptor._Operations)
            {
                var command = operation._Operation;
                var index   = operation._Index ??
                              bulkDescriptor._FixedIndex ??
                              new IndexNameResolver(this.Settings).GetIndexForType(operation._ClrType);
                var typeName = operation._Type
                               ?? bulkDescriptor._FixedType
                               ?? this.GetTypeNameFor(operation._ClrType);

                var id = operation.GetIdForObject(this.IdResolver);
                operation._Index = index;
                operation._Type  = typeName;
                operation._Id    = id;

                var opJson = JsonConvert.SerializeObject(operation, Formatting.None, IndexSerializationSettings);

                var action = "{{ \"{0}\" :  {1} }}\n".F(command, opJson);
                sb.Append(action);

                if (command == "index" || command == "create")
                {
                    string jsonCommand = JsonConvert.SerializeObject(operation._Object, Formatting.None, IndexSerializationSettings);
                    sb.Append(jsonCommand + "\n");
                }
                else if (command == "update")
                {
                    string jsonCommand = JsonConvert.SerializeObject(operation.GetBody(), Formatting.None, IndexSerializationSettings);
                    sb.Append(jsonCommand + "\n");
                }
            }
            var json = sb.ToString();
            var path = "_bulk";

            if (!bulkDescriptor._FixedIndex.IsNullOrEmpty())
            {
                if (!bulkDescriptor._FixedType.IsNullOrEmpty())
                {
                    path = bulkDescriptor._FixedType + "/" + path;
                }
                path = bulkDescriptor._FixedIndex + "/" + path;
            }
            var status = this.Connection.PostSync(path, json);

            return(this.ToParsedResponse <BulkResponse>(status));
        }
Beispiel #3
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);
        }
Beispiel #4
0
        //used by IndexMany and DeleteMany
        private string GenerateBulkCommand <T>(IEnumerable <BulkParameters <T> > @objects, string index, string typeName, string command) where T : class
        {
            objects.ThrowIfEmpty("objects");


            var b = new BulkDescriptor();

            b.FixedPath(index, typeName);
            foreach (var @object in @objects)
            {
                var o = @object;
                if (command == "index")
                {
                    b.Index <T>(bb => bb
                                .Object(o.Document)
                                .Id(o.Id)
                                .Parent(o.Parent)
                                .Percolate(o.Percolate)
                                .Routing(o.Routing)
                                .Timestamp(o.Timestamp)
                                .Ttl(o.Ttl)
                                .Version(o.Version)
                                .VersionType(o.VersionType));
                }
                else if (command == "delete")
                {
                    b.Delete <T>(bb => bb
                                 .Object(o.Document)
                                 .Parent(o.Parent)
                                 .Routing(o.Routing)
                                 .Timestamp(o.Timestamp)
                                 .Ttl(o.Ttl)
                                 .Version(o.Version)
                                 .VersionType(o.VersionType));
                }
            }

            string json, path;

            this.GenerateBulkPathAndJson(b, out json, out path);
            return(json);
        }
Beispiel #5
0
        private void GenerateBulkPathAndJson(BulkDescriptor bulkDescriptor, out string json, out string path)
        {
            bulkDescriptor.ThrowIfNull("bulkDescriptor");
            bulkDescriptor._Operations.ThrowIfEmpty("Bulk descriptor does not define any operations");
            var sb = new StringBuilder();

            foreach (var operation in bulkDescriptor._Operations)
            {
                var command = operation._Operation;
                var index   = operation._Index ??
                              bulkDescriptor._FixedIndex ??
                              new IndexNameResolver(this._connectionSettings).GetIndexForType(operation._ClrType);
                var typeName = operation._Type
                               ?? bulkDescriptor._FixedType
                               ?? this.Infer.TypeName(operation._ClrType);

                var id = operation.GetIdForObject(this.Infer);
                operation._Index = index;
                operation._Type  = typeName;
                operation._Id    = id;

                var opJson = this.Serializer.Serialize(operation, Formatting.None);

                var action = "{{ \"{0}\" :  {1} }}\n".F(command, opJson);
                sb.Append(action);

                if (command == "index" || command == "create")
                {
                    string jsonCommand = this.Serializer.Serialize(operation._Object, Formatting.None);
                    sb.Append(jsonCommand + "\n");
                }
                else if (command == "update")
                {
                    string jsonCommand = this.Serializer.Serialize(operation.GetBody(), Formatting.None);
                    sb.Append(jsonCommand + "\n");
                }
            }
            json = sb.ToString();
            path = "_bulk";
            if (!bulkDescriptor._FixedIndex.IsNullOrEmpty())
            {
                if (!bulkDescriptor._FixedType.IsNullOrEmpty())
                {
                    path = bulkDescriptor._FixedType + "/" + path;
                }
                path = bulkDescriptor._FixedIndex + "/" + path;
            }
            var queryString = new NameValueCollection();

            if (bulkDescriptor._Refresh.HasValue)
            {
                queryString.Add("refresh", bulkDescriptor._Refresh.ToString().ToLowerInvariant());
            }
            switch (bulkDescriptor._Consistency)
            {
            case Consistency.All:
                queryString.Add("consistency", "all");
                break;

            case Consistency.Quorum:
                queryString.Add("consistency", "quorem");
                break;

            case Consistency.One:
                queryString.Add("consistency", "one");
                break;
            }
            if (queryString.HasKeys())
            {
                path += queryString.ToQueryString();
            }
        }