示例#1
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));
        }
示例#2
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);
        }
示例#3
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();
            }
        }