@Override public void deleteIndexes(String name, String... otherNames) { GetMappingsResponse mappings = client.nativeClient().admin().indices().prepareGetMappings("_all").get(); Set<String> existingIndices = Sets.newHashSet(mappings.mappings().keysIt()); Stream.concat(Stream.of(name), Arrays.stream(otherNames)) .distinct() .filter(existingIndices::contains) .forEach(this::deleteIndex); }
private void addMappingToIndex(String indexName, String mappingType, String mappingFilename) throws IOException { GetMappingsResponse getMappingsResponse = elasticSearchClient.admin() .indices() .prepareGetMappings(indexName) .addTypes(mappingType) .execute() .actionGet(); if (getMappingsResponse.mappings().isEmpty()) { logger.info("Adding the workflow type mappings"); InputStream stream = ElasticSearchDAOV5.class.getResourceAsStream(mappingFilename); byte[] bytes = IOUtils.toByteArray(stream); String source = new String(bytes); try { elasticSearchClient.admin() .indices() .preparePutMapping(indexName) .setType(mappingType) .setSource(source) .execute() .actionGet(); } catch (Exception e) { logger.error("Failed to init index mappings", e); } } }
private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings() { return es.client().nativeClient().admin().indices().prepareGetMappings().get().mappings(); }
private Iterator<String> loadExistingIndices() { return es.client().nativeClient().admin().indices().prepareGetMappings().get().mappings().keysIt(); }
@Test @ExcludeOnTravis public void testMapping() throws Exception { configurationSource.save("foo", "bar"); refresh(); final GetMappingsResponse mappings = client.admin().indices().prepareGetMappings("stagemonitor-configuration").setTypes("configuration").get(); assertEquals(1, mappings.getMappings().size()); assertEquals("{\"configuration\":{" + "\"_all\":{\"enabled\":false}," + "\"properties\":{\"configuration\":{\"properties\":{" + "\"key\":{\"type\":\"keyword\"}," + "\"value\":{\"type\":\"keyword\"}}}}" + "}" + "}", mappings.getMappings().get("stagemonitor-configuration").get("configuration").source().toString()); } }
@Override public void deleteIndexes(String name, String... otherNames) { GetMappingsResponse mappings = client.nativeClient().admin().indices().prepareGetMappings("_all").get(); Set<String> existingIndices = Sets.newHashSet(mappings.mappings().keysIt()); Stream.concat(Stream.of(name), Arrays.stream(otherNames)) .distinct() .filter(existingIndices::contains) .forEach(this::deleteIndex); }
public Set<String> listAllFields() throws Exception{ GetMappingsResponse response = client.admin().indices().prepareGetMappings(this.indexName). execute().actionGet(); MappingMetaData mappingMetaData = response.getMappings().get(this.indexName).get(this.documentType); Map map = (Map)mappingMetaData.getSourceAsMap().get("properties"); Set<String> fields = new HashSet<>(); for (Object field: map.keySet()){ fields.add(field.toString()); } return fields; }
/** * Check if a type already exists * @param client Elasticsearch client * @param index Index name * @param type Type name * @return true if type already exists * @throws Exception if the elasticsearch call is failing */ @Deprecated public static boolean isTypeExist(Client client, String index, String type) throws Exception { return !client.admin().indices().prepareGetMappings(index).setTypes(type).get().getMappings().isEmpty(); }
/** * Check if a type already exists * @param client Elasticsearch client * @param index Index name * @param type Type name * @return true if type already exists * @throws Exception if the elasticsearch call is failing */ @Deprecated public static boolean isTypeExist(Client client, String index, String type) throws Exception { return !client.admin().indices().prepareGetMappings(index).setTypes(type).get().getMappings().isEmpty(); }
public static DeleteByQueryResponse clearIndex(Client client, String indexName){ DeleteByQueryResponse indexDeleteByQueryResponses = client.prepareDeleteByQuery(indexName).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(); GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings(indexName).execute().actionGet(); ArrayList<String> mappings = new ArrayList(); getMappingsResponse.getMappings().forEach(map -> { map.value.forEach(map2 -> mappings.add(map2.value.type())); }); if(mappings.size() > 0) { DeleteMappingResponse deleteMappingResponse = client.admin().indices().prepareDeleteMapping(indexName).setType(mappings.toArray(new String[mappings.size()])).execute().actionGet(); } return indexDeleteByQueryResponses; }
public static boolean mappingAlreadyExist(Client client, IndexName indexName, TypeName typeName) { return Iterators.toStream(client.admin() .indices() .prepareGetMappings(indexName.getValue()) .execute() .actionGet() .getMappings() .valuesIt()) .anyMatch(mapping -> mapping.keys().contains(typeName.getValue())); }
@Override public boolean mappingExists(String indexName, String type) { GetMappingsResponse mappings = client.admin().indices().prepareGetMappings(indexName).execute().actionGet(); if (mappings == null || mappings.getMappings().isEmpty()) { return false; } // The real index might have another name if indexName is an alias so we check the mapping of the first item. return mappings.getMappings().values().iterator().next().value.containsKey(type); }
public Map<String, Object> getMappings() throws IOException { ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings = client.admin().indices().prepareGetMappings( indexName).setTypes(documentType).execute().actionGet().getMappings(); ImmutableOpenMap<String, MappingMetaData> typeMappings = indexMappings.get(indexName); MappingMetaData mappings = typeMappings.get(documentType); return mappings.sourceAsMap(); }
/** * Waits for the given mapping type to exists on the master node. */ public void assertMappingOnMaster(final String index, final String type, final String... fieldNames) throws Exception { GetMappingsResponse response = client().admin().indices().prepareGetMappings(index).setTypes(type).get(); ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get(index); assertThat(mappings, notNullValue()); MappingMetaData mappingMetaData = mappings.get(type); assertThat(mappingMetaData, notNullValue()); Map<String, Object> mappingSource = mappingMetaData.getSourceAsMap(); assertFalse(mappingSource.isEmpty()); assertTrue(mappingSource.containsKey("properties")); for (String fieldName : fieldNames) { Map<String, Object> mappingProperties = (Map<String, Object>) mappingSource.get("properties"); if (fieldName.indexOf('.') != -1) { fieldName = fieldName.replace(".", ".properties."); } assertThat("field " + fieldName + " doesn't exists in mapping " + mappingMetaData.source().string(), XContentMapValues.extractValue(fieldName, mappingProperties), notNullValue()); } }
public Optional<MappingMetaData> getMapping(String index, String type) { checkNotEmpty("index", index); checkNotEmpty("type", type); try { GetMappingsResponse mappingResponse = this.getClient().admin().indices().prepareGetMappings(sanitizeIndex(index)).addTypes(sanitizeIndex(type)).get(); return Optional.ofNullable(mappingResponse.getMappings().getOrDefault(sanitizeIndex(index), ImmutableOpenMap.of()).getOrDefault(sanitizeIndex(type), null)); } catch (IndexNotFoundException ex) { if (logger.isDebugEnabled()) { logger.error(MessageFormat.format("Index not found trying to get mapping for {0}:{1}", index, type), ex); } return Optional.empty(); } }
public Optional<MappingMetaData> getMapping(String index, String type) { checkNotEmpty("index", index); checkNotEmpty("type", type); try { GetMappingsResponse mappingResponse = this.getClient().admin().indices().prepareGetMappings(sanitizeIndex(index)).addTypes(sanitizeIndex(type)).get(); return Optional.ofNullable(mappingResponse.getMappings().getOrDefault(sanitizeIndex(index), ImmutableOpenMap.of()).getOrDefault(sanitizeIndex(type), null)); } catch (IndexNotFoundException ex) { if (logger.isDebugEnabled()) { logger.error(MessageFormat.format("Index not found trying to get mapping for {0}:{1}", index, type), ex); } return Optional.empty(); } }
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) { try { indexRefreshTracker.refresh(client, indexName); GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get(); for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) { ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value); for (ObjectCursor<String> typeName : typeMappings.keys()) { MappingMetaData typeMapping = typeMappings.get(typeName.value); Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping); if (properties == null) { continue; } for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) { String rawPropertyName = propertyEntry.getKey().replace(FIELDNAME_DOT_REPLACEMENT, "."); loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName); } } } } catch (IOException ex) { throw new VertexiumException("Could not load type mappings", ex); } }
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) { try { GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get(); for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) { ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value); for (ObjectCursor<String> typeName : typeMappings.keys()) { MappingMetaData typeMapping = typeMappings.get(typeName.value); Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping); if (properties == null) { continue; } for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) { String rawPropertyName = propertyEntry.getKey(); loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName); } } } } catch (IOException ex) { throw new VertexiumException("Could not load type mappings", ex); } }
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) { try { GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get(); for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) { ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value); for (ObjectCursor<String> typeName : typeMappings.keys()) { MappingMetaData typeMapping = typeMappings.get(typeName.value); Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping); if (properties == null) { continue; } for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) { String rawPropertyName = propertyEntry.getKey(); loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName); } } } } catch (IOException ex) { throw new VertexiumException("Could not load type mappings", ex); } }
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) { try { indexRefreshTracker.refresh(client, indexName); GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get(); for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) { ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value); for (ObjectCursor<String> typeName : typeMappings.keys()) { MappingMetaData typeMapping = typeMappings.get(typeName.value); Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping); if (properties == null) { continue; } for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) { String rawPropertyName = propertyEntry.getKey().replace(FIELDNAME_DOT_REPLACEMENT, "."); loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName); } } } } catch (IOException ex) { throw new VertexiumException("Could not load type mappings", ex); } }