private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> getMappings(TransportClient client, GetMappingsRequest request) { try { return retry() .maxAttempts(maxAttempts) .exponentialBackoff(maxRetryTime) .run("getMappings", () -> client.admin() .indices() .getMappings(request) .actionGet(requestTimeout.toMillis()) .getMappings()); } catch (Exception e) { throw new RuntimeException(e); } }
private boolean doesMappingExist(final String index, final String mappingName) { GetMappingsRequest request = new GetMappingsRequest() .indices(index); try { GetMappingsResponse response = elasticSearchClient.admin() .indices() .getMappings(request) .get(); return response.getMappings() .get(index) .containsKey(mappingName); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }
getMappingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getMappingsRequest.masterNodeTimeout())); getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local())); return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) { @Override public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {
@Override public Map<String, Object> getAllMapping(String indexName) throws Exception { Assert.notNull(indexName, "No index defined for putMapping()"); Map<String, Object> map = new HashMap<>(); try { ImmutableOpenMap<String, MappingMetaData> immutableOpenMap = getIndicesAdminClient().getMappings(new GetMappingsRequest().indices(indexName)) .actionGet().getMappings().get(indexName); Iterator<ObjectObjectCursor<String, MappingMetaData>> iterator = immutableOpenMap.iterator(); while (iterator.hasNext()) { ObjectObjectCursor<String, MappingMetaData> objectObjectCursor = iterator.next(); map.put(objectObjectCursor.key, objectObjectCursor.value.getSourceAsMap()); } } catch (Exception e) { throw new Exception("Error while getting mapping for indexName : " + indexName + e.getMessage()); } return map; }
@Override public Map<String, Object> getMapping(String indexName, String type) throws Exception { Assert.notNull(indexName, "No index defined for putMapping()"); Assert.notNull(type, "No type defined for putMapping()"); Map mappings = null; try { mappings = getIndicesAdminClient().getMappings(new GetMappingsRequest().indices(indexName).types(type)) .actionGet().getMappings().get(indexName).get(type).getSourceAsMap(); } catch (Exception e) { throw new Exception("Error while getting mapping for indexName : " + indexName + " type : " + type + " " + e.getMessage()); } return mappings; }
@Override public List<String> getDynamicFieldNames() throws SearchEngineException { List<String> fieldNames = new LinkedList<>(); try { GetMappingsRequest req = new GetMappingsRequestBuilder(client, GetMappingsAction.INSTANCE, configuration.getIndexName()) .setTypes(configuration.getDocType()) .request(); GetMappingsResponse response = client.admin().indices().getMappings(req).actionGet(); MappingMetaData metaData = response.getMappings() .get(configuration.getIndexName()) .get(configuration.getDocType()); Map<String, Object> sourceMap = metaData.getSourceAsMap(); Object annotationField = ((Map)sourceMap.get("properties")).get(configuration.getAnnotationField()); Map<String, Object> annotationProperties = (Map<String, Object>)((Map)annotationField).get("properties"); if (annotationProperties != null) { for (String field : annotationProperties.keySet()) { if (field.matches(DYNAMIC_LABEL_FIELD_REGEX)) { fieldNames.add(field); } } } } catch (IOException e) { LOGGER.error("Caught IOException retrieving field source: {}", e.getMessage()); throw new SearchEngineException(e); } return fieldNames; }
@Override public void putMapping(TypeDescriptor typeDescriptor, JsonNode mapping) throws ClientException { checkClient(); // Check message type mapping GetMappingsRequest mappingsRequest = new GetMappingsRequest().indices(typeDescriptor.getIndex()); GetMappingsResponse mappingsResponse = esClientProvider.getClient().admin().indices().getMappings(mappingsRequest).actionGet(getQueryTimeout()); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappingsResponse.getMappings(); ImmutableOpenMap<String, MappingMetaData> map = mappings.get(typeDescriptor.getIndex()); MappingMetaData metadata = map.get(typeDescriptor.getType()); if (metadata == null) { logger.debug("Put mapping: '{}'", mapping); esClientProvider.getClient().admin().indices().preparePutMapping(typeDescriptor.getIndex()).setType(typeDescriptor.getType()).setSource(mapping.toString(), XContentType.JSON) .execute().actionGet(getQueryTimeout()); logger.trace("Put mapping - mapping {} created! ", typeDescriptor.getType()); } else { logger.trace("Put mapping - mapping {} already exists! ", typeDescriptor.getType()); } }
@Override public void putMapping(TypeDescriptor typeDescriptor, JsonNode mapping) throws ClientException { checkClient(); // Check message type mapping GetMappingsRequest mappingsRequest = new GetMappingsRequest().indices(typeDescriptor.getIndex()); GetMappingsResponse mappingsResponse = esClientProvider.getClient().admin().indices().getMappings(mappingsRequest).actionGet(getQueryTimeout()); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappingsResponse.getMappings(); ImmutableOpenMap<String, MappingMetaData> map = mappings.get(typeDescriptor.getIndex()); MappingMetaData metadata = map.get(typeDescriptor.getType()); if (metadata == null) { logger.debug("Put mapping: '{}'", mapping); esClientProvider.getClient().admin().indices().preparePutMapping(typeDescriptor.getIndex()).setType(typeDescriptor.getType()).setSource(mapping.toString(), XContentType.JSON) .execute().actionGet(getQueryTimeout()); logger.trace("Put mapping - mapping {} created! ", typeDescriptor.getType()); } else { logger.trace("Put mapping - mapping {} already exists! ", typeDescriptor.getType()); } }
@Override public boolean isMappingExists(TypeDescriptor typeDescriptor) throws ClientException { checkClient(); GetMappingsRequest mappingsRequest = new GetMappingsRequest().indices(typeDescriptor.getIndex()); GetMappingsResponse mappingsResponse = esClientProvider.getClient().admin().indices().getMappings(mappingsRequest).actionGet(getQueryTimeout()); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappingsResponse.getMappings(); ImmutableOpenMap<String, MappingMetaData> map = mappings.get(typeDescriptor.getIndex()); MappingMetaData metadata = map.get(typeDescriptor.getType()); return metadata != null; }
@Override public boolean isMappingExists(TypeDescriptor typeDescriptor) throws ClientException { checkClient(); GetMappingsRequest mappingsRequest = new GetMappingsRequest().indices(typeDescriptor.getIndex()); GetMappingsResponse mappingsResponse = esClientProvider.getClient().admin().indices().getMappings(mappingsRequest).actionGet(getQueryTimeout()); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappingsResponse.getMappings(); ImmutableOpenMap<String, MappingMetaData> map = mappings.get(typeDescriptor.getIndex()); MappingMetaData metadata = map.get(typeDescriptor.getType()); return metadata != null; }
getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions())); getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local())); client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) { @Override public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder builder) throws Exception {
getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions())); getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local())); return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) { @Override public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {
getMappingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getMappingsRequest.masterNodeTimeout())); getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local())); return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) { @Override public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {
getMappingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getMappingsRequest.masterNodeTimeout())); getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local())); return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) { @Override public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {