本文整理汇总了Java中org.springframework.data.elasticsearch.ElasticsearchException类的典型用法代码示例。如果您正苦于以下问题:Java ElasticsearchException类的具体用法?Java ElasticsearchException怎么用?Java ElasticsearchException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ElasticsearchException类属于org.springframework.data.elasticsearch包,在下文中一共展示了ElasticsearchException类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bulkIndex
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
@Override
public void bulkIndex(List<IndexQuery> queries) {
Bulk.Builder bulk = new Bulk.Builder();
for (IndexQuery query : queries) {
bulk.addAction(prepareIndex(query));
}
BulkResult bulkResult = new BulkResult(execute(bulk.build()));
if (!bulkResult.isSucceeded()) {
Map<String, String> failedDocuments = new HashMap<>();
for (BulkResult.BulkResultItem item : bulkResult.getFailedItems()) {
failedDocuments.put(item.id, item.error);
}
throw new ElasticsearchException(
"Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
+ failedDocuments + "]", failedDocuments
);
}
}
示例2: bulkUpdate
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
@Override
public void bulkUpdate(List<UpdateQuery> queries) {
Bulk.Builder bulk = new Bulk.Builder();
for (UpdateQuery query : queries) {
bulk.addAction(prepareUpdate(query));
}
BulkResult bulkResult = new BulkResult(execute(bulk.build()));
if (!bulkResult.isSucceeded()) {
Map<String, String> failedDocuments = new HashMap<>();
for (BulkResult.BulkResultItem item : bulkResult.getFailedItems()) {
failedDocuments.put(item.id, item.error);
}
throw new ElasticsearchException(
"Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
+ failedDocuments + "]", failedDocuments
);
}
}
示例3: putMapping
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
@Override
public <T> boolean putMapping(Class<T> clazz) {
if (clazz.isAnnotationPresent(Mapping.class)) {
String mappingPath = clazz.getAnnotation(Mapping.class).mappingPath();
if (isNotBlank(mappingPath)) {
String mappings = readFileFromClasspath(mappingPath);
if (isNotBlank(mappings)) {
return putMapping(clazz, mappings);
}
} else {
logger.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
}
}
ElasticsearchPersistentEntity<Object> persistentEntity = getPersistentEntityFor(clazz);
String mapping;
try {
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
if (idProperty == null) {
throw new IllegalArgumentException(String.format("No Id property for %s found", clazz.getSimpleName()));
}
mapping = buildMapping(
clazz,
persistentEntity.getIndexType(),
idProperty.getFieldName(),
persistentEntity.getParentType()
).string();
} catch (Exception e) {
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
}
return putMapping(clazz, mapping);
}
示例4: getMapping
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
@Override
public Map getMapping(String indexName, String type) {
Assert.notNull(indexName, "No index defined for putMapping()");
Assert.notNull(type, "No type defined for putMapping()");
Map mappings = null;
try {
GetMapping.Builder getMappingBuilder = new GetMapping.Builder();
getMappingBuilder.addIndex(indexName).addType(type);
JestResult result = execute(getMappingBuilder.build());
if (!result.getJsonObject().has(indexName)) {
logger.info("Index {} did not exist when retrieving mappings for type {}.", indexName, type);
} else {
JsonObject index = result.getJsonObject().get(indexName).getAsJsonObject();
if (index != null) {
JsonObject mappingElem = index.get("mappings").getAsJsonObject();
if (!mappingElem.has(type)) {
logger.info("Type {} did not exist in index {} when retrieving mappings.", type, indexName);
} else {
mappings = resultsMapper.getEntityMapper().mapToObject(mappingElem.get(type).toString(), Map.class);
}
}
}
} catch (Exception e) {
throw new ElasticsearchException("Error while getting mapping for indexName : " + indexName + " type : " + type + " " + e.getMessage());
}
return mappings;
}
示例5: execute
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
private <T extends JestResult> T execute(Action<T> action, boolean acceptNotFound) {
try {
// Execute action
T result = client.execute(action);
// Check result and map error
errorMapper.mapError(action, result, acceptNotFound);
return result;
} catch (IOException e) {
throw new ElasticsearchException("failed to execute action", e);
}
}
示例6: prepareUpdate
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
private Update prepareUpdate(UpdateQuery query) {
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() : getPersistentEntityFor(query.getClazz()).getIndexName();
String type = isNotBlank(query.getType()) ? query.getType() : getPersistentEntityFor(query.getClazz()).getIndexType();
Assert.notNull(indexName, "No index defined for Query");
Assert.notNull(type, "No type define for Query");
Assert.notNull(query.getId(), "No Id define for Query");
Assert.notNull(query.getUpdateRequest(), "No IndexRequest define for Query");
Map<String, Object> payLoadMap = new HashMap<>();
if (query.getUpdateRequest().script() == null) {
// doc
if (query.DoUpsert()) {
payLoadMap.put("doc_as_upsert", Boolean.TRUE);
payLoadMap.put("doc", query.getUpdateRequest().doc().sourceAsMap());
} else {
payLoadMap.put("doc", query.getUpdateRequest().doc().sourceAsMap());
}
} else {
// or script
/*
.setScript(query.getUpdateRequest().script(), query.getUpdateRequest().scriptType())
.setScriptParams(query.getUpdateRequest().scriptParams())
.setScriptLang(query.getUpdateRequest().scriptLang());
*/
}
try {
String payload = resultsMapper.getEntityMapper().mapToString(payLoadMap);
Update.Builder updateBuilder = new Update.Builder(payload).index(indexName).type(type).id(query.getId());
return updateBuilder.build();
} catch (IOException e) {
throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e);
}
}
示例7: mapEntity
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
private <T> T mapEntity(String source, Class<T> clazz) {
if (isBlank(source)) {
return null;
}
try {
return entityMapper.mapToObject(source, clazz);
} catch (IOException e) {
throw new ElasticsearchException("failed to map source [ " + source + "] to class " + clazz.getSimpleName(), e);
}
}
示例8: shouldThrowElasticsearchExceptionWhenNoDocumentSpecified
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
@Test(expected = ElasticsearchException.class)
public void shouldThrowElasticsearchExceptionWhenNoDocumentSpecified() {
// given
IndexQuery indexQuery = new IndexQuery();
indexQuery.setId("2333343434");
indexQuery.setIndexName(INDEX_NAME);
indexQuery.setType(TYPE_NAME);
//when
elasticsearchTemplate.index(indexQuery);
}
示例9: prepareIndex
import org.springframework.data.elasticsearch.ElasticsearchException; //导入依赖的package包/类
private Index prepareIndex(IndexQuery query) {
try {
String indexName = isBlank(query.getIndexName()) ? retrieveIndexNameFromPersistentEntity(query.getObject()
.getClass())[0] : query.getIndexName();
String type = isBlank(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0]
: query.getType();
Index.Builder indexBuilder;
if (query.getObject() != null) {
String entityId = null;
if (isDocument(query.getObject().getClass())) {
entityId = getPersistentEntityId(query.getObject());
}
indexBuilder = new Index.Builder(resultsMapper.getEntityMapper().mapToString(query.getObject()));
// If we have a query id and a document id, do not ask ES to generate one.
if (entityId != null) {
indexBuilder.index(indexName).type(type).id(entityId);
} else {
indexBuilder.index(indexName).type(type);
}
} else if (query.getSource() != null) {
indexBuilder = new Index.Builder(query.getSource()).index(indexName).type(type);
} else {
throw new ElasticsearchException("object or source is null, failed to index the document [id: " + query.getId() + "]");
}
if (query.getVersion() != null) {
indexBuilder.setParameter(Parameters.VERSION, query.getVersion());
indexBuilder.setParameter(Parameters.VERSION_TYPE, EXTERNAL.name().toLowerCase());
}
if (query.getId() != null) {
indexBuilder.id(query.getId());
}
if (query.getParentId() != null) {
indexBuilder.setParameter(Parameters.PARENT, query.getParentId());
}
return indexBuilder.build();
} catch (IOException e) {
throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e);
}
}