48
48
import org .elasticsearch .client .core .MultiTermVectorsResponse ;
49
49
import org .elasticsearch .client .core .TermVectorsRequest ;
50
50
import org .elasticsearch .client .core .TermVectorsResponse ;
51
+ import org .elasticsearch .client .indices .CreateIndexRequest ;
51
52
import org .elasticsearch .common .Strings ;
52
53
import org .elasticsearch .common .bytes .BytesReference ;
53
54
import org .elasticsearch .common .settings .Settings ;
95
96
public class CrudIT extends ESRestHighLevelClientTestCase {
96
97
97
98
public void testDelete () throws IOException {
99
+ highLevelClient ().indices ().create (
100
+ new CreateIndexRequest ("index" ).settings (Collections .singletonMap ("index.number_of_shards" , "1" )),
101
+ RequestOptions .DEFAULT );
98
102
{
99
103
// Testing deletion
100
104
String docId = "id" ;
101
- highLevelClient ().index (
105
+ IndexResponse indexResponse = highLevelClient ().index (
102
106
new IndexRequest ("index" , "type" , docId ).source (Collections .singletonMap ("foo" , "bar" )), RequestOptions .DEFAULT );
103
107
DeleteRequest deleteRequest = new DeleteRequest ("index" , "type" , docId );
104
108
if (randomBoolean ()) {
105
- deleteRequest .version (1L );
109
+ deleteRequest .setIfSeqNo (indexResponse .getSeqNo ());
110
+ deleteRequest .setIfPrimaryTerm (indexResponse .getPrimaryTerm ());
111
+ } else {
112
+ deleteRequest .version (indexResponse .getVersion ());
106
113
}
107
114
DeleteResponse deleteResponse = execute (deleteRequest , highLevelClient ()::delete , highLevelClient ()::deleteAsync ,
108
115
highLevelClient ()::delete , highLevelClient ()::deleteAsync );
@@ -127,13 +134,26 @@ public void testDelete() throws IOException {
127
134
String docId = "version_conflict" ;
128
135
highLevelClient ().index (
129
136
new IndexRequest ("index" , "type" , docId ).source (Collections .singletonMap ("foo" , "bar" )), RequestOptions .DEFAULT );
130
- DeleteRequest deleteRequest = new DeleteRequest ("index" , "type" , docId ).version (2 );
137
+ DeleteRequest deleteRequest = new DeleteRequest ("index" , "type" , docId );
138
+ final boolean seqNos = randomBoolean ();
139
+ if (seqNos ) {
140
+ deleteRequest .setIfSeqNo (2 ).setIfPrimaryTerm (2 );
141
+ } else {
142
+ deleteRequest .version (2 );
143
+ }
144
+
131
145
ElasticsearchException exception = expectThrows (ElasticsearchException .class ,
132
146
() -> execute (deleteRequest , highLevelClient ()::delete , highLevelClient ()::deleteAsync ,
133
147
highLevelClient ()::delete , highLevelClient ()::deleteAsync ));
134
148
assertEquals (RestStatus .CONFLICT , exception .status ());
135
- assertEquals ("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][" + docId + "]: " +
136
- "version conflict, current version [1] is different than the one provided [2]]" , exception .getMessage ());
149
+ if (seqNos ) {
150
+ assertEquals ("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][" + docId + "]: " +
151
+ "version conflict, required seqNo [2], primary term [2]. current document has seqNo [3] and primary term [1]]" ,
152
+ exception .getMessage ());
153
+ } else {
154
+ assertEquals ("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][" + docId + "]: " +
155
+ "version conflict, current version [1] is different than the one provided [2]]" , exception .getMessage ());
156
+ }
137
157
assertEquals ("index" , exception .getMetadata ("es.index" ).get (0 ));
138
158
}
139
159
{
@@ -453,18 +473,29 @@ public void testIndex() throws IOException {
453
473
assertEquals ("type" , indexResponse .getType ());
454
474
assertEquals ("id" , indexResponse .getId ());
455
475
assertEquals (2L , indexResponse .getVersion ());
476
+ final boolean seqNosForConflict = randomBoolean ();
456
477
457
478
ElasticsearchStatusException exception = expectThrows (ElasticsearchStatusException .class , () -> {
458
479
IndexRequest wrongRequest = new IndexRequest ("index" , "type" , "id" );
459
480
wrongRequest .source (XContentBuilder .builder (xContentType .xContent ()).startObject ().field ("field" , "test" ).endObject ());
460
- wrongRequest .version (5L );
481
+ if (seqNosForConflict ) {
482
+ wrongRequest .setIfSeqNo (2 ).setIfPrimaryTerm (2 );
483
+ } else {
484
+ wrongRequest .version (5 );
485
+ }
461
486
462
487
execute (wrongRequest , highLevelClient ()::index , highLevelClient ()::indexAsync ,
463
488
highLevelClient ()::index , highLevelClient ()::indexAsync );
464
489
});
465
490
assertEquals (RestStatus .CONFLICT , exception .status ());
466
- assertEquals ("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][id]: " +
467
- "version conflict, current version [2] is different than the one provided [5]]" , exception .getMessage ());
491
+ if (seqNosForConflict ) {
492
+ assertEquals ("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][id]: " +
493
+ "version conflict, required seqNo [1], primary term [5]. current document has seqNo [2] and primary term [1]]" ,
494
+ exception .getMessage ());
495
+ } else {
496
+ assertEquals ("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][id]: " +
497
+ "version conflict, current version [2] is different than the one provided [5]]" , exception .getMessage ());
498
+ }
468
499
assertEquals ("index" , exception .getMetadata ("es.index" ).get (0 ));
469
500
}
470
501
{
@@ -763,7 +794,8 @@ public void testBulk() throws IOException {
763
794
if (opType == DocWriteRequest .OpType .INDEX ) {
764
795
IndexRequest indexRequest = new IndexRequest ("index" , "test" , id ).source (source , xContentType );
765
796
if (erroneous ) {
766
- indexRequest .version (12L );
797
+ indexRequest .setIfSeqNo (12L );
798
+ indexRequest .setIfPrimaryTerm (12L );
767
799
}
768
800
bulkRequest .add (indexRequest );
769
801
@@ -1075,7 +1107,8 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure)
1075
1107
if (opType == DocWriteRequest .OpType .INDEX ) {
1076
1108
IndexRequest indexRequest = new IndexRequest ("index" , "test" , id ).source (xContentType , "id" , i );
1077
1109
if (erroneous ) {
1078
- indexRequest .version (12L );
1110
+ indexRequest .setIfSeqNo (12L );
1111
+ indexRequest .setIfPrimaryTerm (12L );
1079
1112
}
1080
1113
processor .add (indexRequest );
1081
1114
0 commit comments