@@ -461,6 +461,99 @@ public void testUpdateRequestWithScriptAndShouldUpsertDoc() throws Exception {
461
461
}
462
462
}
463
463
464
+ @ Test
465
+ public void testContextVariables () throws Exception {
466
+ createTestIndex ();
467
+
468
+ // Add child type for testing the _parent context variable
469
+ client ().admin ().indices ().preparePutMapping ("test" )
470
+ .setType ("subtype1" )
471
+ .setSource (XContentFactory .jsonBuilder ()
472
+ .startObject ()
473
+ .startObject ("subtype1" )
474
+ .startObject ("_parent" ).field ("type" , "type1" ).endObject ()
475
+ .startObject ("_timestamp" ).field ("enabled" , true ).field ("store" , "yes" ).endObject ()
476
+ .startObject ("_ttl" ).field ("enabled" , true ).field ("store" , "yes" ).endObject ()
477
+ .endObject ()
478
+ .endObject ())
479
+ .execute ().actionGet ();
480
+ ensureGreen ();
481
+
482
+ // Index some documents
483
+ long timestamp = System .currentTimeMillis ();
484
+ client ().prepareIndex ()
485
+ .setIndex ("test" )
486
+ .setType ("type1" )
487
+ .setId ("parentId1" )
488
+ .setTimestamp (String .valueOf (timestamp -1 ))
489
+ .setSource ("field1" , 0 , "content" , "bar" )
490
+ .execute ().actionGet ();
491
+
492
+ client ().prepareIndex ()
493
+ .setIndex ("test" )
494
+ .setType ("subtype1" )
495
+ .setId ("id1" )
496
+ .setParent ("parentId1" )
497
+ .setRouting ("routing1" )
498
+ .setTimestamp (String .valueOf (timestamp ))
499
+ .setTTL (111211211 )
500
+ .setSource ("field1" , 1 , "content" , "foo" )
501
+ .execute ().actionGet ();
502
+ long postIndexTs = System .currentTimeMillis ();
503
+
504
+ // Update the first object and note context variables values
505
+ Map <String , Object > scriptParams = new HashMap <>();
506
+ scriptParams .put ("delim" , "_" );
507
+ UpdateResponse updateResponse = client ().prepareUpdate ("test" , "subtype1" , "id1" )
508
+ .setRouting ("routing1" )
509
+ .setScript (
510
+ "assert ctx._index == \" test\" : \" index should be \\ \" test\\ \" \" \n " +
511
+ "assert ctx._type == \" subtype1\" : \" type should be \\ \" subtype1\\ \" \" \n " +
512
+ "assert ctx._id == \" id1\" : \" id should be \\ \" id1\\ \" \" \n " +
513
+ "assert ctx._version == 1 : \" version should be 1\" \n " +
514
+ "assert ctx._parent == \" parentId1\" : \" parent should be \\ \" parentId1\\ \" \" \n " +
515
+ "assert ctx._routing == \" routing1\" : \" routing should be \\ \" routing1\\ \" \" \n " +
516
+ "assert ctx._timestamp == " + timestamp + " : \" timestamp should be " + timestamp + "\" \n " +
517
+ "def now = new Date().getTime()\n " +
518
+ "assert (111211211 - ctx._ttl) <= (now - " + postIndexTs + ") : \" ttl is not within acceptable range\" \n " +
519
+ "ctx._source.content = ctx._source.content + delim + ctx._source.content;\n " +
520
+ "ctx._source.field1 += 1;\n " ,
521
+ ScriptService .ScriptType .INLINE )
522
+ .setScriptParams (scriptParams )
523
+ .execute ().actionGet ();
524
+
525
+ assertEquals (2 , updateResponse .getVersion ());
526
+
527
+ GetResponse getResponse = client ().prepareGet ("test" , "subtype1" , "id1" ).setRouting ("routing1" ).execute ().actionGet ();
528
+ assertEquals (2 , getResponse .getSourceAsMap ().get ("field1" ));
529
+ assertEquals ("foo_foo" , getResponse .getSourceAsMap ().get ("content" ));
530
+
531
+ // Idem with the second object
532
+ scriptParams = new HashMap <>();
533
+ scriptParams .put ("delim" , "_" );
534
+ updateResponse = client ().prepareUpdate ("test" , "type1" , "parentId1" )
535
+ .setScript (
536
+ "assert ctx._index == \" test\" : \" index should be \\ \" test\\ \" \" \n " +
537
+ "assert ctx._type == \" type1\" : \" type should be \\ \" type1\\ \" \" \n " +
538
+ "assert ctx._id == \" parentId1\" : \" id should be \\ \" parentId1\\ \" \" \n " +
539
+ "assert ctx._version == 1 : \" version should be 1\" \n " +
540
+ "assert ctx._parent == null : \" parent should be null\" \n " +
541
+ "assert ctx._routing == null : \" routing should be null\" \n " +
542
+ "assert ctx._timestamp == " + (timestamp - 1 ) + " : \" timestamp should be " + (timestamp - 1 ) + "\" \n " +
543
+ "assert ctx._ttl == null : \" ttl should be null\" \n " +
544
+ "ctx._source.content = ctx._source.content + delim + ctx._source.content;\n " +
545
+ "ctx._source.field1 += 1;\n " ,
546
+ ScriptService .ScriptType .INLINE )
547
+ .setScriptParams (scriptParams )
548
+ .execute ().actionGet ();
549
+
550
+ assertEquals (2 , updateResponse .getVersion ());
551
+
552
+ getResponse = client ().prepareGet ("test" , "type1" , "parentId1" ).execute ().actionGet ();
553
+ assertEquals (1 , getResponse .getSourceAsMap ().get ("field1" ));
554
+ assertEquals ("bar_bar" , getResponse .getSourceAsMap ().get ("content" ));
555
+ }
556
+
464
557
@ Test
465
558
@ Slow
466
559
public void testConcurrentUpdateWithRetryOnConflict () throws Exception {
0 commit comments