Skip to content

Commit 4a93517

Browse files
committed
Add test to demonstrate indexing and bulk indexing of Json.Net JObject instances
Reference to #1609
1 parent 1b22e17 commit 4a93517

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

Diff for: src/Elasticsearch.Net/Transport/PostData.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void Write(Stream writableStream, IConnectionConfigurationValues settings
9696
ms = new MemoryStream(WrittenBytes);
9797
break;
9898
case PostType.LiteralString:
99-
ms = new MemoryStream(_literalString?.Utf8Bytes());
99+
ms = !string.IsNullOrEmpty(_literalString) ? new MemoryStream(_literalString.Utf8Bytes()) : null;
100100
break;
101101
case PostType.EnumerableOfString:
102102
ms = _enumurabeOfStrings.HasAny() ? new MemoryStream((string.Join("\n", _enumurabeOfStrings) + "\n").Utf8Bytes()) : null;

Diff for: src/Tests/Document/Single/Index/IndexApiTests.cs

+67-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Elasticsearch.Net;
55
using FluentAssertions;
66
using Nest;
7+
using Newtonsoft.Json.Linq;
78
using Tests.Framework;
89
using Tests.Framework.Integration;
910
using Tests.Framework.MockData;
@@ -69,16 +70,19 @@ protected override LazyResponses ClientUsage() => Calls(
6970
[Collection(IntegrationContext.Indexing)]
7071
public class IndexIntegrationTests : SimpleIntegration
7172
{
72-
public IndexIntegrationTests(IndexingCluster cluster) : base(cluster) { }
73+
public IndexIntegrationTests(IndexingCluster cluster) : base(cluster)
74+
{
75+
}
7376

74-
[I] public void OpTypeCreate()
77+
[I]
78+
public void OpTypeCreate()
7579
{
7680
var indexName = this.RandomString();
7781
var project = Project.Generator.Generate(1).First();
7882
var indexResult = this.Client.Index(project, f => f
7983
.Index(indexName)
8084
.OpType(OpType.Create)
81-
);
85+
);
8286
indexResult.IsValid.Should().BeTrue();
8387
indexResult.ApiCall.HttpStatusCode.Should().Be(201);
8488
indexResult.Created.Should().BeTrue();
@@ -89,18 +93,19 @@ [I] public void OpTypeCreate()
8993
indexResult = this.Client.Index(project, f => f
9094
.Index(indexName)
9195
.OpType(OpType.Create)
92-
);
96+
);
9397

9498
indexResult.IsValid.Should().BeFalse();
9599
indexResult.Created.Should().BeFalse();
96100
indexResult.ApiCall.HttpStatusCode.Should().Be(409);
97101
}
98102

99-
[I] public void Index()
103+
[I]
104+
public void Index()
100105
{
101106
var indexName = this.RandomString();
102107
var project = Project.Generator.Generate(1).First();
103-
var indexResult = this.Client.Index(project, f => f .Index(indexName));
108+
var indexResult = this.Client.Index(project, f => f.Index(indexName));
104109
indexResult.IsValid.Should().BeTrue();
105110
indexResult.ApiCall.HttpStatusCode.Should().Be(201);
106111
indexResult.Created.Should().BeTrue();
@@ -109,13 +114,68 @@ [I] public void Index()
109114
indexResult.Id.Should().Be(project.Name);
110115
indexResult.Version.Should().Be(1);
111116

112-
indexResult = this.Client.Index(project, f => f .Index(indexName));
117+
indexResult = this.Client.Index(project, f => f.Index(indexName));
113118

114119
indexResult.IsValid.Should().BeTrue();
115120
indexResult.Created.Should().BeFalse();
116121
indexResult.ApiCall.HttpStatusCode.Should().Be(200);
117122
indexResult.Version.Should().Be(2);
118123
}
119124

125+
[Collection(IntegrationContext.Indexing)]
126+
public class IndexJObjectIntegrationTests : SimpleIntegration
127+
{
128+
public IndexJObjectIntegrationTests(IndexingCluster cluster) : base(cluster){}
129+
130+
[I]
131+
public void Index()
132+
{
133+
var indexName = this.RandomString();
134+
135+
var jObjects = Enumerable.Range(1, 1000)
136+
.Select(i =>
137+
new JObject
138+
{
139+
{ "id", i },
140+
{"name", $"name {i}"},
141+
{"value", Math.Pow(i,2) },
142+
{"date", new DateTime(2016, 1, 1)},
143+
{"child", new JObject
144+
{
145+
{ "child_name", $"child_name {i}{i}" },
146+
{ "child_value", 3 }
147+
}
148+
}
149+
});
150+
151+
var jObject = jObjects.First();
152+
153+
var indexResult = this.Client.Index(jObject, f => f
154+
.Index(indexName)
155+
.Type("example")
156+
.Id(jObject["id"].Value<int>())
157+
);
158+
159+
indexResult.IsValid.Should().BeTrue();
160+
indexResult.ApiCall.HttpStatusCode.Should().Be(201);
161+
indexResult.Created.Should().BeTrue();
162+
indexResult.Index.Should().Be(indexName);
163+
164+
var bulkResponse = this.Client.Bulk(b => b
165+
.IndexMany(jObjects.Skip(1), (bi, d) => bi
166+
.Document(d)
167+
.Id(d["id"].Value<int>())
168+
.Index(indexName)
169+
.Type("example")
170+
)
171+
);
172+
173+
foreach (var response in bulkResponse.Items)
174+
{
175+
response.IsValid.Should().BeTrue();
176+
response.Status.Should().Be(201);
177+
}
178+
}
179+
}
120180
}
121181
}

0 commit comments

Comments
 (0)