|
28 | 28 | import co.elastic.clients.elasticsearch.indices.CreateResponse; |
29 | 29 | import co.elastic.clients.elasticsearch.indices.IndexState; |
30 | 30 | import co.elastic.clients.json.jsonb.JsonbJsonpMapper; |
| 31 | +import jakarta.json.JsonValue; |
31 | 32 | import org.apache.http.HttpHost; |
32 | 33 | import org.elasticsearch.client.RequestOptions; |
33 | 34 | import org.elasticsearch.client.RestClient; |
@@ -102,50 +103,46 @@ public void testDataIngestion() throws Exception { |
102 | 103 | BooleanResponse existsResponse = client.indices().exists(b -> b.index("test")); |
103 | 104 | assertTrue(existsResponse.value()); |
104 | 105 |
|
105 | | - // Would be nice to have index tagged as "preferred field" to generate this: |
106 | | - // BooleanResponse existsResponse = client.indices().exists("test"); |
107 | | - |
108 | | - |
109 | 106 | // Ingest some data |
110 | 107 | AppData appData = new AppData(); |
111 | 108 | appData.setIntValue(1337); |
112 | 109 | appData.setMsg("foo"); |
113 | 110 |
|
114 | | - String docId = client.create(b -> b |
| 111 | + String docId = client.index(b -> b |
115 | 112 | .index("test") |
116 | | - .type("_doc") // needed for now because of how paths are generated |
117 | 113 | .id("myId") |
118 | 114 | .value(appData) |
| 115 | + .refresh(JsonValue.TRUE) // Make it visible for search |
119 | 116 | )._id(); |
120 | 117 |
|
121 | 118 | // Query by id |
122 | 119 | AppData esData = client.get(b -> b |
123 | 120 | .index("test") |
124 | | - .type("_doc") |
125 | 121 | .id(docId) |
126 | 122 | , AppData.class |
127 | 123 | )._source(); |
128 | 124 |
|
129 | 125 | assertEquals(1337, esData.getIntValue()); |
130 | 126 | assertEquals("foo", esData.getMsg()); |
131 | 127 |
|
132 | | - // Search, adding some options |
| 128 | + // Search, adding some request options |
133 | 129 | RequestOptions options = RequestOptions.DEFAULT.toBuilder() |
134 | 130 | .addHeader("x-super-header", "bar") |
135 | 131 | .build(); |
136 | 132 |
|
137 | | - SearchResponse<String> search = client |
| 133 | + SearchResponse<AppData> search = client |
138 | 134 | .withRequestOptions(options) |
139 | 135 | .search(b -> b |
140 | | - .index("test")//, "foo", "bar") |
141 | | - .allowNoIndices(true) |
142 | | - .explain(true), |
143 | | - String.class |
| 136 | + .index("test") |
| 137 | + , AppData.class |
144 | 138 | ); |
145 | 139 |
|
146 | | - System.out.println(search.hits().total().asJsonObject().getInt("value") + " hits"); |
147 | | - |
| 140 | + int hits = search.hits().total().asJsonObject().getInt("value"); // union types not handled yet |
| 141 | + assertEquals(1, hits); |
148 | 142 |
|
| 143 | + esData = search.hits().hits().get(0)._source(); |
| 144 | + assertEquals(1337, esData.getIntValue()); |
| 145 | + assertEquals("foo", esData.getMsg()); |
149 | 146 | } |
150 | 147 |
|
151 | 148 | public static class AppData { |
|
0 commit comments