Skip to content

Commit a16bad2

Browse files
committed
Added support for create op_type index parameter
See Operation Type http://www.elasticsearch.org/guide/reference/api/index_.html
1 parent efc54c8 commit a16bad2

File tree

7 files changed

+154
-59
lines changed

7 files changed

+154
-59
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Nest.Tests.MockData;
5+
using Nest.Tests.MockData.Domain;
6+
using NUnit.Framework;
7+
8+
namespace Nest.Tests.Integration.Core
9+
{
10+
[TestFixture]
11+
public class IndexTests : CleanStateIntegrationTests
12+
{
13+
private readonly Random idGen = new Random();
14+
15+
[Test]
16+
public void IndexUsingCreateFlag()
17+
{
18+
// Document to be indexed.
19+
ElasticSearchProject doc = new ElasticSearchProject
20+
{
21+
Country = "Mozambique",
22+
Followers = new List<Person>(),
23+
Id = idGen.Next(),
24+
Name = "Test Document for 'IndexDocument' Create Flag"
25+
};
26+
27+
// Index the document
28+
this._client.Index<ElasticSearchProject>(doc, new IndexParameters { OpType = OpType.Create });
29+
30+
// Grab the indexed document.
31+
var foundDoc = this._client.Get<ElasticSearchProject>(doc.Id);
32+
33+
// Check that the document was successfully indexed.
34+
Assert.NotNull(foundDoc);
35+
Assert.AreEqual(doc.Country, foundDoc.Country);
36+
Assert.AreEqual(doc.Followers.Count, foundDoc.Followers.Count);
37+
Assert.AreEqual(doc.Id, foundDoc.Id);
38+
Assert.AreEqual(doc.Name, foundDoc.Name);
39+
40+
// Now try to index the document again while using the Create Flag
41+
var response = this._client.Index<ElasticSearchProject>(doc, new IndexParameters { OpType = OpType.Create });
42+
43+
// Make sure the index request failed with HTTP status 409 since document with same id already exists.
44+
Assert.False(response.OK);
45+
Assert.AreEqual(System.Net.HttpStatusCode.Conflict, response.ConnectionStatus.Error.HttpStatusCode);
46+
}
47+
}
48+
}

src/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
</Reference>
5252
</ItemGroup>
5353
<ItemGroup>
54+
<Compile Include="Core\IndexTests.cs" />
5455
<Compile Include="IntegrationTests.cs" />
5556
<Compile Include="Cluster\HealthTests.cs" />
5657
<Compile Include="Cluster\NodeTests.cs" />
Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using NUnit.Framework;
6-
using Newtonsoft.Json;
7-
using Newtonsoft.Json.Linq;
8-
9-
using Nest;
10-
using Newtonsoft.Json.Converters;
11-
using Nest.Resolvers.Converters;
12-
using Nest.Tests.MockData.Domain;
13-
14-
namespace Nest.Tests.Unit.Core.Versioning
15-
{
16-
[TestFixture]
17-
public class VersioningTests : BaseJsonTests
18-
{
19-
[Test]
20-
public void IndexSupportsVersioning()
21-
{
22-
var o = new ElasticSearchProject { Id = 1, Name = "Test" };
23-
var result = this._client.Index(o, new IndexParameters { Version = "1" });
24-
var status = result.ConnectionStatus;
25-
StringAssert.Contains("version=1", status.RequestUrl);
26-
}
27-
}
28-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
9+
using Nest;
10+
using Newtonsoft.Json.Converters;
11+
using Nest.Resolvers.Converters;
12+
using Nest.Tests.MockData.Domain;
13+
14+
namespace Nest.Tests.Unit.Core.Versioning
15+
{
16+
[TestFixture]
17+
public class VersioningTests : BaseJsonTests
18+
{
19+
[Test]
20+
public void IndexSupportsVersioning()
21+
{
22+
var o = new ElasticSearchProject { Id = 1, Name = "Test" };
23+
var result = this._client.Index(o, new IndexParameters { Version = "1" });
24+
var status = result.ConnectionStatus;
25+
StringAssert.Contains("version=1", status.RequestUrl);
26+
}
27+
28+
[Test]
29+
public void IndexOpTypeDefault()
30+
{
31+
var o = new ElasticSearchProject { Id = 1, Name = "Test" };
32+
var result = this._client.Index(o, new IndexParameters());
33+
var status = result.ConnectionStatus;
34+
StringAssert.DoesNotContain("op_type=create", status.RequestUrl);
35+
}
36+
37+
[Test]
38+
public void IndexOpTypeNone()
39+
{
40+
var o = new ElasticSearchProject { Id = 1, Name = "Test" };
41+
var result = this._client.Index(o, new IndexParameters { OpType = OpType.None });
42+
var status = result.ConnectionStatus;
43+
StringAssert.DoesNotContain("op_type=create", status.RequestUrl);
44+
}
45+
46+
[Test]
47+
public void IndexOpTypeCreate()
48+
{
49+
var o = new ElasticSearchProject { Id = 1, Name = "Test" };
50+
var result = this._client.Index(o, new IndexParameters { OpType = OpType.Create });
51+
var status = result.ConnectionStatus;
52+
StringAssert.Contains("op_type=create", status.RequestUrl);
53+
}
54+
}
55+
}
Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
6-
namespace Nest
7-
{
8-
public interface ISimpleUrlParameters
9-
{
10-
Replication Replication { get; set; }
11-
bool Refresh { get; set; }
12-
}
13-
public interface IUrlParameters
14-
{
15-
string Version { get; set; }
16-
string Routing { get; set; }
17-
string Parent { get; set; }
18-
Replication Replication { get; set; }
19-
Consistency Consistency { get; set; }
20-
bool Refresh { get; set; }
21-
}
22-
public class BaseParameters : IUrlParameters
23-
{
24-
public string Version { get; set; }
25-
public string Routing { get; set; }
26-
public string Parent { get; set; }
27-
public Replication Replication { get; set; }
28-
public Consistency Consistency { get; set; }
29-
public bool Refresh { get; set; }
30-
}
31-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public interface ISimpleUrlParameters
9+
{
10+
Replication Replication { get; set; }
11+
bool Refresh { get; set; }
12+
}
13+
public interface IUrlParameters
14+
{
15+
string Version { get; set; }
16+
OpType OpType { get; set; }
17+
string Routing { get; set; }
18+
string Parent { get; set; }
19+
Replication Replication { get; set; }
20+
Consistency Consistency { get; set; }
21+
bool Refresh { get; set; }
22+
}
23+
public class BaseParameters : IUrlParameters
24+
{
25+
public string Version { get; set; }
26+
public OpType OpType { get; set; }
27+
public string Routing { get; set; }
28+
public string Parent { get; set; }
29+
public Replication Replication { get; set; }
30+
public Consistency Consistency { get; set; }
31+
public bool Refresh { get; set; }
32+
}
33+
}

src/Nest/Enums/OpType.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public enum OpType
9+
{
10+
None, // default in ES
11+
Create
12+
}
13+
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
<Compile Include="Enums\GeoTree.cs" />
208208
<Compile Include="Enums\NumericIndexOption.cs" />
209209
<Compile Include="Enums\IndexOptions.cs" />
210+
<Compile Include="Enums\OpType.cs" />
210211
<Compile Include="Enums\SearchType.cs" />
211212
<Compile Include="Enums\Lang.cs" />
212213
<Compile Include="Enums\DateHistogramComparatorType.cs" />

src/Nest/Resolvers/PathResolver.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public string AppendParametersToPath(string path, IUrlParameters urlParameters)
185185
if (!urlParameters.Parent.IsNullOrEmpty())
186186
parameters.Add("parent=" + urlParameters.Parent);
187187

188+
if (urlParameters.OpType != OpType.None) // default not set
189+
parameters.Add("op_type=" + urlParameters.OpType.ToString().ToLower());
190+
188191
if (urlParameters.Replication != Replication.Sync) //sync == default
189192
parameters.Add("replication=" + urlParameters.Replication.ToString().ToLower());
190193

0 commit comments

Comments
 (0)