use of org.elasticsearch.action.DocWriteResponse.Result in project fess by codelibs.
the class FessEsClient method store.
public boolean store(final String index, final String type, final Object obj) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
@SuppressWarnings("unchecked") final Map<String, Object> source = obj instanceof Map ? (Map<String, Object>) obj : BeanUtil.copyBeanToNewMap(obj);
final String id = (String) source.remove(fessConfig.getIndexFieldId());
final Long version = (Long) source.remove(fessConfig.getIndexFieldVersion());
IndexResponse response;
try {
if (id == null) {
// create
response = client.prepareIndex(index, type).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.CREATE).execute().actionGet(fessConfig.getIndexIndexTimeout());
} else {
// create or update
final IndexRequestBuilder builder = client.prepareIndex(index, type, id).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.INDEX);
if (version != null && version.longValue() > 0) {
builder.setVersion(version);
}
response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
}
final Result result = response.getResult();
return result == Result.CREATED || result == Result.UPDATED;
} catch (final ElasticsearchException e) {
throw new FessEsClientException("Failed to store: " + obj, e);
}
}
Aggregations