ElasticsearchNodeClient implements ElasticsearchClient { @Override public Map<String, IndexMapping> getIndexMappings(String indexExpression) { try { ClusterState state = clusterService.state(); String[] concreteIndices = resolveIndexExpression(state, new String[] {indexExpression}); return populateIndexMappings( state.metadata().findMappings(concreteIndices, ALL_TYPES, ALL_FIELDS)); } catch (IOException e) { throw new IllegalStateException( "Failed to read mapping in cluster state for index pattern [" + indexExpression + "]", e); } }  @Override Map<String, IndexMapping> getIndexMappings(String indexExpression); @Override ElasticsearchResponse search(ElasticsearchRequest request); @Override void cleanup(ElasticsearchRequest request); @Override void schedule(Runnable task); static final String[] ALL_TYPES; static final Function<String, Predicate<String>> ALL_FIELDS; }
@Test public void getIndexMappings() throws IOException { URL url = Resources.getResource(TEST_MAPPING_FILE); String mappings = Resources.toString(url, Charsets.UTF_8); String indexName = "test"; ElasticsearchNodeClient client = mockClient(indexName, mappings); 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 public void getIndexMappingsWithEmptyMapping() { String indexName = "test"; ElasticsearchNodeClient client = mockClient(indexName, ""); Map<String, IndexMapping> indexMappings = client.getIndexMappings(indexName); assertEquals(1, indexMappings.size()); IndexMapping indexMapping = indexMappings.values().iterator().next(); assertEquals(0, indexMapping.size()); }
@Test public void getIndexMappingsWithIOException() { String indexName = "test"; ClusterService clusterService = mockClusterService(indexName, new IOException()); ElasticsearchNodeClient client = new ElasticsearchNodeClient(clusterService, nodeClient); assertThrows(IllegalStateException.class, () -> client.getIndexMappings(indexName)); }
@Test public void getIndexMappingsWithNonExistIndex() { ElasticsearchNodeClient client = new ElasticsearchNodeClient(mockClusterService("test"), nodeClient); assertThrows(IndexNotFoundException.class, () -> client.getIndexMappings("non_exist_index")); }