List of usage examples for org.springframework.data.elasticsearch ElasticsearchException ElasticsearchException
public ElasticsearchException(String message, Map<String, String> failedDocuments)
From source file:org.springframework.data.elasticsearch.core.DefaultResultMapper.java
private <T> void populateScriptFields(T result, SearchHit hit) { if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) { for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); if (scriptedField != null) { String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); SearchHitField searchHitField = hit.getFields().get(name); if (searchHitField != null) { field.setAccessible(true); try { field.set(result, searchHitField.getValue()); } catch (IllegalArgumentException e) { throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " + searchHitField.getValue(), e); } catch (IllegalAccessException e) { throw new ElasticsearchException("failed to access scripted field: " + name, e); }//w ww . j a v a 2 s.c o m } } } } }
From source file:org.springframework.data.elasticsearch.core.AbstractResultMapper.java
public <T> T mapEntity(String source, Class<T> clazz) { if (isBlank(source)) { return null; }//w ww .j a v a2s .c om try { return entityMapper.mapToObject(source, clazz); } catch (IOException e) { throw new ElasticsearchException( "failed to map source [ " + source + "] to class " + clazz.getSimpleName(), e); } }
From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java
@Override public <T> boolean putMapping(Class<T> clazz) { ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz); PutMappingRequestBuilder requestBuilder = client.admin().indices() .preparePutMapping(persistentEntity.getIndexName()).setType(persistentEntity.getIndexType()); try {//from w w w . j ava 2 s . c o m XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity.getIdProperty().getFieldName(), elasticsearchConverter.getMappingContext()); return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged(); } catch (Exception e) { throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e); } }
From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java
@Override public void bulkIndex(List<IndexQuery> queries) { BulkRequestBuilder bulkRequest = client.prepareBulk(); for (IndexQuery query : queries) { bulkRequest.add(prepareIndex(query)); }/*from w w w .j a va 2s . c o m*/ BulkResponse bulkResponse = bulkRequest.execute().actionGet(); if (bulkResponse.hasFailures()) { Map<String, String> failedDocuments = new HashMap<String, String>(); for (BulkItemResponse item : bulkResponse.getItems()) { if (item.isFailed()) failedDocuments.put(item.getId(), item.getFailureMessage()); } throw new ElasticsearchException( "Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages [" + failedDocuments + "]", failedDocuments); } }
From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java
private IndexRequestBuilder prepareIndex(IndexQuery query) { try {//from ww w. j ava2s .c om String indexName = isBlank(query.getIndexName()) ? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0] : query.getIndexName(); String type = isBlank(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0] : query.getType(); IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()) .setSource(objectMapper.writeValueAsString(query.getObject())); if (query.getParentId() != null) { indexRequestBuilder.setParent(query.getParentId()); } if (query.getVersion() != null) { indexRequestBuilder.setVersion(query.getVersion()); indexRequestBuilder.setVersionType(EXTERNAL); } return indexRequestBuilder; } catch (IOException e) { throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e); } }
From source file:org.springframework.data.elasticsearch.core.ElasticsearchTemplate.java
private <T> T mapResult(String source, Class<T> clazz) { if (isBlank(source)) { return null; }//from ww w .j a va 2 s . c o m try { return objectMapper.readValue(source, clazz); } catch (IOException e) { throw new ElasticsearchException( "failed to map source [ " + source + "] to class " + clazz.getSimpleName(), e); } }