ElasticsearchRestClient implements ElasticsearchClient { @Override public Map<String, IndexMapping> getIndexMappings(String indexExpression) { GetMappingsRequest request = new GetMappingsRequest().indices(indexExpression); try { GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT); return response.mappings().entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> new IndexMapping(e.getValue()))); } catch (IOException e) { throw new IllegalStateException("Failed to get index mappings for " + indexExpression, e); } }  @Override Map<String, IndexMapping> getIndexMappings(String indexExpression); @Override ElasticsearchResponse search(ElasticsearchRequest request); @Override void cleanup(ElasticsearchRequest request); @Override void schedule(Runnable task);  }
@Test void getIndexMappings() throws IOException { URL url = Resources.getResource(TEST_MAPPING_FILE); String mappings = Resources.toString(url, Charsets.UTF_8); String indexName = "test"; GetMappingsResponse response = mock(GetMappingsResponse.class); when(response.mappings()).thenReturn(mockFieldMappings(indexName, mappings)); when(restClient.indices().getMapping(any(GetMappingsRequest.class), any())) .thenReturn(response); Map<String, IndexMapping> indexMappings = client.getIndexMappings(indexName); assertEquals(1, indexMappings.size()); IndexMapping indexMapping = indexMappings.values().iterator().next(); assertEquals(20, indexMapping.size()); assertEquals("text", indexMapping.getFieldType("address")); assertEquals("integer", indexMapping.getFieldType("age")); assertEquals("double", indexMapping.getFieldType("balance")); assertEquals("keyword", indexMapping.getFieldType("city")); assertEquals("date", indexMapping.getFieldType("birthday")); assertEquals("geo_point", indexMapping.getFieldType("location")); assertEquals("some_new_es_type_outside_type_system", indexMapping.getFieldType("new_field")); assertEquals("text", indexMapping.getFieldType("field with spaces")); assertEquals("text_keyword", indexMapping.getFieldType("employer")); assertEquals("keyword", indexMapping.getFieldType("employer.raw")); assertEquals("nested", indexMapping.getFieldType("projects")); assertEquals("boolean", indexMapping.getFieldType("projects.active")); assertEquals("date", indexMapping.getFieldType("projects.release")); assertEquals("nested", indexMapping.getFieldType("projects.members")); assertEquals("text", indexMapping.getFieldType("projects.members.name")); assertEquals("object", indexMapping.getFieldType("manager")); assertEquals("text_keyword", indexMapping.getFieldType("manager.name")); assertEquals("keyword", indexMapping.getFieldType("manager.name.keyword")); assertEquals("keyword", indexMapping.getFieldType("manager.address")); assertEquals("long", indexMapping.getFieldType("manager.salary")); }
@Test void getIndexMappingsWithIOException() throws IOException { when(restClient.indices().getMapping(any(GetMappingsRequest.class), any())) .thenThrow(new IOException()); assertThrows(IllegalStateException.class, () -> client.getIndexMappings("test")); }