4848import org .elasticsearch .client .core .MultiTermVectorsResponse ;
4949import org .elasticsearch .client .core .TermVectorsRequest ;
5050import org .elasticsearch .client .core .TermVectorsResponse ;
51+ import org .elasticsearch .client .indices .CreateIndexRequest ;
5152import org .elasticsearch .common .Strings ;
5253import org .elasticsearch .common .bytes .BytesReference ;
5354import org .elasticsearch .common .settings .Settings ;
9596public class CrudIT extends ESRestHighLevelClientTestCase {
9697
9798 public void testDelete () throws IOException {
99+ highLevelClient ().indices ().create (
100+ new CreateIndexRequest ("index" ).settings (Collections .singletonMap ("index.number_of_shards" , "1" )),
101+ RequestOptions .DEFAULT );
98102 {
99103 // Testing deletion
100104 String docId = "id" ;
101- highLevelClient ().index (
105+ IndexResponse indexResponse = highLevelClient ().index (
102106 new IndexRequest ("index" , "type" , docId ).source (Collections .singletonMap ("foo" , "bar" )), RequestOptions .DEFAULT );
103107 DeleteRequest deleteRequest = new DeleteRequest ("index" , "type" , docId );
104108 if (randomBoolean ()) {
105- deleteRequest .version (1L );
109+ deleteRequest .setIfSeqNo (indexResponse .getSeqNo ());
110+ deleteRequest .setIfPrimaryTerm (indexResponse .getPrimaryTerm ());
111+ } else {
112+ deleteRequest .version (indexResponse .getVersion ());
106113 }
107114 DeleteResponse deleteResponse = execute (deleteRequest , highLevelClient ()::delete , highLevelClient ()::deleteAsync ,
108115 highLevelClient ()::delete , highLevelClient ()::deleteAsync );
@@ -127,13 +134,26 @@ public void testDelete() throws IOException {
127134 String docId = "version_conflict" ;
128135 highLevelClient ().index (
129136 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+
131145 ElasticsearchException exception = expectThrows (ElasticsearchException .class ,
132146 () -> execute (deleteRequest , highLevelClient ()::delete , highLevelClient ()::deleteAsync ,
133147 highLevelClient ()::delete , highLevelClient ()::deleteAsync ));
134148 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+ }
137157 assertEquals ("index" , exception .getMetadata ("es.index" ).get (0 ));
138158 }
139159 {
@@ -453,18 +473,29 @@ public void testIndex() throws IOException {
453473 assertEquals ("type" , indexResponse .getType ());
454474 assertEquals ("id" , indexResponse .getId ());
455475 assertEquals (2L , indexResponse .getVersion ());
476+ final boolean seqNosForConflict = randomBoolean ();
456477
457478 ElasticsearchStatusException exception = expectThrows (ElasticsearchStatusException .class , () -> {
458479 IndexRequest wrongRequest = new IndexRequest ("index" , "type" , "id" );
459480 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+ }
461486
462487 execute (wrongRequest , highLevelClient ()::index , highLevelClient ()::indexAsync ,
463488 highLevelClient ()::index , highLevelClient ()::indexAsync );
464489 });
465490 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+ }
468499 assertEquals ("index" , exception .getMetadata ("es.index" ).get (0 ));
469500 }
470501 {
@@ -763,7 +794,8 @@ public void testBulk() throws IOException {
763794 if (opType == DocWriteRequest .OpType .INDEX ) {
764795 IndexRequest indexRequest = new IndexRequest ("index" , "test" , id ).source (source , xContentType );
765796 if (erroneous ) {
766- indexRequest .version (12L );
797+ indexRequest .setIfSeqNo (12L );
798+ indexRequest .setIfPrimaryTerm (12L );
767799 }
768800 bulkRequest .add (indexRequest );
769801
@@ -1075,7 +1107,8 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure)
10751107 if (opType == DocWriteRequest .OpType .INDEX ) {
10761108 IndexRequest indexRequest = new IndexRequest ("index" , "test" , id ).source (xContentType , "id" , i );
10771109 if (erroneous ) {
1078- indexRequest .version (12L );
1110+ indexRequest .setIfSeqNo (12L );
1111+ indexRequest .setIfPrimaryTerm (12L );
10791112 }
10801113 processor .add (indexRequest );
10811114
0 commit comments