|
27 | 27 | import org.elasticsearch.action.index.IndexResponse;
|
28 | 28 | import org.elasticsearch.action.search.ClearScrollRequest;
|
29 | 29 | import org.elasticsearch.action.search.ClearScrollResponse;
|
| 30 | +import org.elasticsearch.action.search.MultiSearchRequest; |
| 31 | +import org.elasticsearch.action.search.MultiSearchResponse; |
30 | 32 | import org.elasticsearch.action.search.SearchRequest;
|
31 | 33 | import org.elasticsearch.action.search.SearchResponse;
|
32 | 34 | import org.elasticsearch.action.search.SearchScrollRequest;
|
|
85 | 87 | import static org.hamcrest.Matchers.greaterThan;
|
86 | 88 |
|
87 | 89 | /**
|
88 |
| - * This class is used to generate the Java High Level REST Client Search API documentation. |
89 |
| - * <p> |
90 |
| - * You need to wrap your code between two tags like: |
91 |
| - * // tag::example |
92 |
| - * // end::example |
93 |
| - * <p> |
94 |
| - * Where example is your tag name. |
95 |
| - * <p> |
96 |
| - * Then in the documentation, you can extract what is between tag and end tags with |
97 |
| - * ["source","java",subs="attributes,callouts,macros"] |
98 |
| - * -------------------------------------------------- |
99 |
| - * include-tagged::{doc-tests}/SearchDocumentationIT.java[example] |
100 |
| - * -------------------------------------------------- |
101 |
| - * <p> |
102 |
| - * The column width of the code block is 84. If the code contains a line longer |
103 |
| - * than 84, the line will be cut and a horizontal scroll bar will be displayed. |
104 |
| - * (the code indentation of the tag is not included in the width) |
| 90 | + * Documentation for search APIs in the high level java client. |
| 91 | + * Code wrapped in {@code tag} and {@code end} tags is included in the docs. |
105 | 92 | */
|
106 | 93 | public class SearchDocumentationIT extends ESRestHighLevelClientTestCase {
|
107 | 94 |
|
108 | 95 | @SuppressWarnings({"unused", "unchecked"})
|
109 | 96 | public void testSearch() throws Exception {
|
| 97 | + indexSearchTestData(); |
110 | 98 | RestHighLevelClient client = highLevelClient();
|
111 |
| - { |
112 |
| - BulkRequest request = new BulkRequest(); |
113 |
| - request.add(new IndexRequest("posts", "doc", "1") |
114 |
| - .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?", "user", |
115 |
| - Arrays.asList("kimchy", "luca"), "innerObject", Collections.singletonMap("key", "value"))); |
116 |
| - request.add(new IndexRequest("posts", "doc", "2") |
117 |
| - .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch", "user", |
118 |
| - Arrays.asList("kimchy", "christoph"), "innerObject", Collections.singletonMap("key", "value"))); |
119 |
| - request.add(new IndexRequest("posts", "doc", "3") |
120 |
| - .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch", "user", |
121 |
| - Arrays.asList("kimchy", "tanguy"), "innerObject", Collections.singletonMap("key", "value"))); |
122 |
| - request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
123 |
| - BulkResponse bulkResponse = client.bulk(request); |
124 |
| - assertSame(RestStatus.OK, bulkResponse.status()); |
125 |
| - assertFalse(bulkResponse.hasFailures()); |
126 |
| - } |
127 | 99 | {
|
128 | 100 | // tag::search-request-basic
|
129 | 101 | SearchRequest searchRequest = new SearchRequest(); // <1>
|
@@ -715,4 +687,90 @@ public void onFailure(Exception e) {
|
715 | 687 | assertTrue(succeeded);
|
716 | 688 | }
|
717 | 689 | }
|
| 690 | + |
| 691 | + public void testMultiSearch() throws Exception { |
| 692 | + indexSearchTestData(); |
| 693 | + RestHighLevelClient client = highLevelClient(); |
| 694 | + { |
| 695 | + // tag::multi-search-request-basic |
| 696 | + MultiSearchRequest request = new MultiSearchRequest(); // <1> |
| 697 | + SearchRequest firstSearchRequest = new SearchRequest(); // <2> |
| 698 | + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
| 699 | + searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy")); |
| 700 | + firstSearchRequest.source(searchSourceBuilder); |
| 701 | + request.add(firstSearchRequest); // <3> |
| 702 | + SearchRequest secondSearchRequest = new SearchRequest(); // <4> |
| 703 | + searchSourceBuilder = new SearchSourceBuilder(); |
| 704 | + searchSourceBuilder.query(QueryBuilders.matchQuery("user", "luca")); |
| 705 | + secondSearchRequest.source(searchSourceBuilder); |
| 706 | + request.add(secondSearchRequest); |
| 707 | + // end::multi-search-request-basic |
| 708 | + // tag::multi-search-execute |
| 709 | + MultiSearchResponse response = client.multiSearch(request); |
| 710 | + // end::multi-search-execute |
| 711 | + // tag::multi-search-response |
| 712 | + MultiSearchResponse.Item firstResponse = response.getResponses()[0]; // <1> |
| 713 | + assertNull(firstResponse.getFailure()); // <2> |
| 714 | + SearchResponse searchResponse = firstResponse.getResponse(); // <3> |
| 715 | + assertEquals(3, searchResponse.getHits().getTotalHits()); |
| 716 | + MultiSearchResponse.Item secondResponse = response.getResponses()[1]; // <4> |
| 717 | + assertNull(secondResponse.getFailure()); |
| 718 | + searchResponse = secondResponse.getResponse(); |
| 719 | + assertEquals(1, searchResponse.getHits().getTotalHits()); |
| 720 | + // end::multi-search-response |
| 721 | + |
| 722 | + // tag::multi-search-execute-listener |
| 723 | + ActionListener<MultiSearchResponse> listener = new ActionListener<MultiSearchResponse>() { |
| 724 | + @Override |
| 725 | + public void onResponse(MultiSearchResponse response) { |
| 726 | + // <1> |
| 727 | + } |
| 728 | + |
| 729 | + @Override |
| 730 | + public void onFailure(Exception e) { |
| 731 | + // <2> |
| 732 | + } |
| 733 | + }; |
| 734 | + // end::multi-search-execute-listener |
| 735 | + |
| 736 | + // Replace the empty listener by a blocking listener in test |
| 737 | + final CountDownLatch latch = new CountDownLatch(1); |
| 738 | + listener = new LatchedActionListener<>(listener, latch); |
| 739 | + |
| 740 | + // tag::multi-search-execute-async |
| 741 | + client.multiSearchAsync(request, listener); // <1> |
| 742 | + // end::multi-search-execute-async |
| 743 | + |
| 744 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 745 | + } |
| 746 | + { |
| 747 | + // tag::multi-search-request-index |
| 748 | + MultiSearchRequest request = new MultiSearchRequest(); |
| 749 | + request.add(new SearchRequest("posts") // <1> |
| 750 | + .types("doc")); // <2> |
| 751 | + // end::multi-search-request-index |
| 752 | + MultiSearchResponse response = client.multiSearch(request); |
| 753 | + MultiSearchResponse.Item firstResponse = response.getResponses()[0]; |
| 754 | + assertNull(firstResponse.getFailure()); |
| 755 | + SearchResponse searchResponse = firstResponse.getResponse(); |
| 756 | + assertEquals(3, searchResponse.getHits().getTotalHits()); |
| 757 | + } |
| 758 | + } |
| 759 | + |
| 760 | + private void indexSearchTestData() throws IOException { |
| 761 | + BulkRequest request = new BulkRequest(); |
| 762 | + request.add(new IndexRequest("posts", "doc", "1") |
| 763 | + .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?", "user", |
| 764 | + Arrays.asList("kimchy", "luca"), "innerObject", Collections.singletonMap("key", "value"))); |
| 765 | + request.add(new IndexRequest("posts", "doc", "2") |
| 766 | + .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch", "user", |
| 767 | + Arrays.asList("kimchy", "christoph"), "innerObject", Collections.singletonMap("key", "value"))); |
| 768 | + request.add(new IndexRequest("posts", "doc", "3") |
| 769 | + .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch", "user", |
| 770 | + Arrays.asList("kimchy", "tanguy"), "innerObject", Collections.singletonMap("key", "value"))); |
| 771 | + request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 772 | + BulkResponse bulkResponse = highLevelClient().bulk(request); |
| 773 | + assertSame(RestStatus.OK, bulkResponse.status()); |
| 774 | + assertFalse(bulkResponse.hasFailures()); |
| 775 | + } |
718 | 776 | }
|
0 commit comments