41
41
import org .elasticsearch .action .admin .indices .forcemerge .ForceMergeRequest ;
42
42
import org .elasticsearch .action .admin .indices .forcemerge .ForceMergeResponse ;
43
43
import org .elasticsearch .action .admin .indices .get .GetIndexRequest ;
44
+ import org .elasticsearch .action .admin .indices .mapping .get .GetMappingsRequest ;
45
+ import org .elasticsearch .action .admin .indices .mapping .get .GetMappingsResponse ;
44
46
import org .elasticsearch .action .admin .indices .mapping .put .PutMappingRequest ;
45
47
import org .elasticsearch .action .admin .indices .mapping .put .PutMappingResponse ;
46
48
import org .elasticsearch .action .admin .indices .open .OpenIndexRequest ;
64
66
import org .elasticsearch .client .ESRestHighLevelClientTestCase ;
65
67
import org .elasticsearch .client .RestHighLevelClient ;
66
68
import org .elasticsearch .client .SyncedFlushResponse ;
69
+ import org .elasticsearch .cluster .metadata .MappingMetaData ;
70
+ import org .elasticsearch .common .collect .ImmutableOpenMap ;
67
71
import org .elasticsearch .common .settings .Settings ;
68
72
import org .elasticsearch .common .unit .ByteSizeUnit ;
69
73
import org .elasticsearch .common .unit .ByteSizeValue ;
81
85
import java .util .concurrent .CountDownLatch ;
82
86
import java .util .concurrent .TimeUnit ;
83
87
88
+ import static org .hamcrest .Matchers .equalTo ;
89
+
84
90
/**
85
91
* This class is used to generate the Java Indices API documentation.
86
92
* You need to wrap your code between two tags like:
@@ -532,17 +538,17 @@ public void testPutMappingAsync() throws Exception {
532
538
533
539
// tag::put-mapping-execute-listener
534
540
ActionListener <PutMappingResponse > listener =
535
- new ActionListener <PutMappingResponse >() {
536
- @ Override
537
- public void onResponse (PutMappingResponse putMappingResponse ) {
538
- // <1>
539
- }
541
+ new ActionListener <PutMappingResponse >() {
542
+ @ Override
543
+ public void onResponse (PutMappingResponse putMappingResponse ) {
544
+ // <1>
545
+ }
540
546
541
- @ Override
542
- public void onFailure (Exception e ) {
543
- // <2>
544
- }
545
- };
547
+ @ Override
548
+ public void onFailure (Exception e ) {
549
+ // <2>
550
+ }
551
+ };
546
552
// end::put-mapping-execute-listener
547
553
548
554
// Replace the empty listener by a blocking listener in test
@@ -557,6 +563,133 @@ public void onFailure(Exception e) {
557
563
}
558
564
}
559
565
566
+ public void testGetMapping () throws IOException {
567
+ RestHighLevelClient client = highLevelClient ();
568
+
569
+ {
570
+ CreateIndexResponse createIndexResponse = client .indices ().create (new CreateIndexRequest ("twitter" ));
571
+ assertTrue (createIndexResponse .isAcknowledged ());
572
+ PutMappingRequest request = new PutMappingRequest ("twitter" );
573
+ request .type ("tweet" );
574
+ request .source (
575
+ "{\n " +
576
+ " \" properties\" : {\n " +
577
+ " \" message\" : {\n " +
578
+ " \" type\" : \" text\" \n " +
579
+ " }\n " +
580
+ " }\n " +
581
+ "}" , // <1>
582
+ XContentType .JSON );
583
+ PutMappingResponse putMappingResponse = client .indices ().putMapping (request );
584
+ assertTrue (putMappingResponse .isAcknowledged ());
585
+ }
586
+
587
+ {
588
+ // tag::get-mapping-request
589
+ GetMappingsRequest request = new GetMappingsRequest (); // <1>
590
+ request .indices ("twitter" ); // <2>
591
+ request .types ("tweet" ); // <3>
592
+ // end::get-mapping-request
593
+
594
+ // tag::get-mapping-request-masterTimeout
595
+ request .masterNodeTimeout (TimeValue .timeValueMinutes (1 )); // <1>
596
+ request .masterNodeTimeout ("1m" ); // <2>
597
+ // end::get-mapping-request-masterTimeout
598
+
599
+ // tag::get-mapping-request-indicesOptions
600
+ request .indicesOptions (IndicesOptions .lenientExpandOpen ()); // <1>
601
+ // end::get-mapping-request-indicesOptions
602
+
603
+ // tag::get-mapping-execute
604
+ GetMappingsResponse getMappingResponse = client .indices ().getMappings (request );
605
+ // end::get-mapping-execute
606
+
607
+ // tag::get-mapping-response
608
+ ImmutableOpenMap <String , ImmutableOpenMap <String , MappingMetaData >> allMappings = getMappingResponse .mappings (); // <1>
609
+ MappingMetaData typeMapping = allMappings .get ("twitter" ).get ("tweet" ); // <2>
610
+ Map <String , Object > tweetMapping = typeMapping .sourceAsMap (); // <3>
611
+ // end::get-mapping-response
612
+
613
+ Map <String , String > type = new HashMap <>();
614
+ type .put ("type" , "text" );
615
+ Map <String , Object > field = new HashMap <>();
616
+ field .put ("message" , type );
617
+ Map <String , Object > expected = new HashMap <>();
618
+ expected .put ("properties" , field );
619
+ assertThat (tweetMapping , equalTo (expected ));
620
+ }
621
+ }
622
+
623
+ public void testGetMappingAsync () throws Exception {
624
+ final RestHighLevelClient client = highLevelClient ();
625
+
626
+ {
627
+ CreateIndexResponse createIndexResponse = client .indices ().create (new CreateIndexRequest ("twitter" ));
628
+ assertTrue (createIndexResponse .isAcknowledged ());
629
+ PutMappingRequest request = new PutMappingRequest ("twitter" );
630
+ request .type ("tweet" );
631
+ request .source (
632
+ "{\n " +
633
+ " \" properties\" : {\n " +
634
+ " \" message\" : {\n " +
635
+ " \" type\" : \" text\" \n " +
636
+ " }\n " +
637
+ " }\n " +
638
+ "}" , // <1>
639
+ XContentType .JSON );
640
+ PutMappingResponse putMappingResponse = client .indices ().putMapping (request );
641
+ assertTrue (putMappingResponse .isAcknowledged ());
642
+ }
643
+
644
+ {
645
+ GetMappingsRequest request = new GetMappingsRequest ();
646
+ request .indices ("twitter" );
647
+ request .types ("tweet" );
648
+
649
+ // tag::get-mapping-execute-listener
650
+ ActionListener <GetMappingsResponse > listener =
651
+ new ActionListener <GetMappingsResponse >() {
652
+ @ Override
653
+ public void onResponse (GetMappingsResponse putMappingResponse ) {
654
+ // <1>
655
+ }
656
+
657
+ @ Override
658
+ public void onFailure (Exception e ) {
659
+ // <2>
660
+ }
661
+ };
662
+ // end::get-mapping-execute-listener
663
+
664
+ // Replace the empty listener by a blocking listener in test
665
+ final CountDownLatch latch = new CountDownLatch (1 );
666
+ final ActionListener <GetMappingsResponse > latchListener = new LatchedActionListener <>(listener , latch );
667
+ listener = ActionListener .wrap (r -> {
668
+ ImmutableOpenMap <String , ImmutableOpenMap <String , MappingMetaData >> allMappings = r .mappings ();
669
+ MappingMetaData typeMapping = allMappings .get ("twitter" ).get ("tweet" );
670
+ Map <String , Object > tweetMapping = typeMapping .sourceAsMap ();
671
+
672
+ Map <String , String > type = new HashMap <>();
673
+ type .put ("type" , "text" );
674
+ Map <String , Object > field = new HashMap <>();
675
+ field .put ("message" , type );
676
+ Map <String , Object > expected = new HashMap <>();
677
+ expected .put ("properties" , field );
678
+ assertThat (tweetMapping , equalTo (expected ));
679
+ latchListener .onResponse (r );
680
+ }, e -> {
681
+ latchListener .onFailure (e );
682
+ fail ("should not fail" );
683
+ });
684
+
685
+ // tag::get-mapping-execute-async
686
+ client .indices ().getMappingsAsync (request , listener ); // <1>
687
+ // end::get-mapping-execute-async
688
+
689
+ assertTrue (latch .await (30L , TimeUnit .SECONDS ));
690
+ }
691
+ }
692
+
560
693
public void testOpenIndex () throws Exception {
561
694
RestHighLevelClient client = highLevelClient ();
562
695
0 commit comments