use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method reindexEvent.
private boolean reindexEvent(final ReindexResponse.Failure resp, final String indexName, final SearchHit sh, final EventInfo ei, final BulkProcessor bulkProcessor) {
try {
/* If it's not recurring or a stand-alone instance index it */
final BwEvent ev = ei.getEvent();
if ((uidsMap != null) && (ev.getLocationUid() == null)) {
final String locuid = uidsMap.get(ev.getUid());
if (locuid != null) {
uidsSet++;
ev.setLocationUid(locuid);
}
}
if (!restoreEvProps(resp, ei)) {
return false;
}
if (!ev.testRecurring() && (ev.getRecurrenceId() == null)) {
final EsDocInfo doc = makeDoc(resp, ei, ItemKind.master, ev.getDtstart(), ev.getDtend(), // ev.getRecurrenceId(),
null, null);
if (doc == null) {
return false;
}
final IndexRequest request = new IndexRequest(indexName, sh.type(), doc.getId());
request.source(doc.getSource());
bulkProcessor.add(request);
return true;
}
if (ev.getRecurrenceId() != null) {
errorReturn(resp, "Not implemented - index of single override");
return false;
}
if (!addOverrides(resp, idxpars.getUserIndexName(), ei)) {
return false;
}
final int maxYears;
final int maxInstances;
final DateLimits dl = new DateLimits();
if (ev.getPublick()) {
maxYears = unauthpars.getMaxYears();
maxInstances = unauthpars.getMaxInstances();
} else {
maxYears = authpars.getMaxYears();
maxInstances = authpars.getMaxInstances();
}
final RecurPeriods rp = RecurUtil.getPeriods(ev, maxYears, maxInstances);
if (rp.instances.isEmpty()) {
errorReturn(resp, "No instances for an alleged recurring event.");
return false;
}
final String stzid = ev.getDtstart().getTzid();
int instanceCt = maxInstances;
final boolean dateOnly = ev.getDtstart().getDateType();
/* First build a table of overrides so we can skip these later
*/
final Map<String, String> overrides = new HashMap<>();
if (!Util.isEmpty(ei.getOverrides())) {
for (final EventInfo oei : ei.getOverrides()) {
final BwEvent ov = oei.getEvent();
overrides.put(ov.getRecurrenceId(), ov.getRecurrenceId());
final String dtstart;
if (ov.getDtstart().getDateType()) {
dtstart = ov.getRecurrenceId().substring(0, 8);
} else {
dtstart = ov.getRecurrenceId();
}
final BwDateTime rstart = BwDateTime.makeBwDateTime(ov.getDtstart().getDateType(), dtstart, stzid);
final BwDateTime rend = rstart.addDuration(BwDuration.makeDuration(ov.getDuration()));
final EsDocInfo doc = makeDoc(resp, oei, ItemKind.override, rstart, rend, ov.getRecurrenceId(), dl);
if (doc == null) {
return false;
}
final IndexRequest request = new IndexRequest(indexName, sh.type(), doc.getId());
request.source(doc.getSource());
bulkProcessor.add(request);
instanceCt--;
}
}
for (final Period p : rp.instances) {
String dtval = p.getStart().toString();
if (dateOnly) {
dtval = dtval.substring(0, 8);
}
final BwDateTime rstart = BwDateTime.makeBwDateTime(dateOnly, dtval, stzid);
if (overrides.get(rstart.getDate()) != null) {
// Overrides indexed separately - skip this instance.
continue;
}
final String recurrenceId = rstart.getDate();
dtval = p.getEnd().toString();
if (dateOnly) {
dtval = dtval.substring(0, 8);
}
final BwDateTime rend = BwDateTime.makeBwDateTime(dateOnly, dtval, stzid);
final EsDocInfo doc = makeDoc(resp, ei, entity, rstart, rend, recurrenceId, dl);
if (doc == null) {
return false;
}
final IndexRequest request = new IndexRequest(indexName, sh.type(), doc.getId());
request.source(doc.getSource());
bulkProcessor.add(request);
instanceCt--;
if (instanceCt == 0) {
// That's all you're getting from me
break;
}
}
// </editor-fold>
// <editor-fold desc="Emit the master event with a date range covering the entire period.">
final BwDateTime dtstart = BwDateTime.makeBwDateTime(dateOnly, dl.minStart, stzid);
final BwDateTime dtend = BwDateTime.makeBwDateTime(dateOnly, dl.maxEnd, stzid);
final EsDocInfo doc = makeDoc(resp, ei, ItemKind.master, dtstart, dtend, null, null);
if (doc == null) {
return false;
}
final IndexRequest request = new IndexRequest(indexName, sh.type(), doc.getId());
request.source(doc.getSource());
bulkProcessor.add(request);
// </editor-fold>
return true;
} catch (final Throwable t) {
errorReturn(resp, t);
return false;
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project elasticsearch-indexing-proxy by codelibs.
the class RequestUtils method createBulkRequest.
public static BulkRequestBuilder createBulkRequest(final Client client, final StreamInput streamInput, final String index) throws IOException {
final BulkRequestBuilder builder = client.prepareBulk();
final BulkRequest request = builder.request();
request.readFrom(streamInput);
if (index != null) {
request.requests().stream().forEach(req -> {
if (req instanceof DeleteRequest) {
((DeleteRequest) req).index(index);
} else if (req instanceof DeleteByQueryRequest) {
((DeleteByQueryRequest) req).indices(index);
} else if (req instanceof IndexRequest) {
((IndexRequest) req).index(index);
} else if (req instanceof UpdateRequest) {
((UpdateRequest) req).index(index);
} else if (req instanceof UpdateByQueryRequest) {
((UpdateByQueryRequest) req).indices(index);
} else {
throw new ElasticsearchException("Unsupported request in bulk: " + req);
}
});
}
return builder;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project Zpider by zeroized.
the class ElasticClient method indexDoc.
public String indexDoc(Map<String, ?> doc) throws IOException {
IndexRequest indexRequest = new IndexRequest(index, type);
indexRequest.source(doc);
IndexResponse indexResponse = highLevelClient.index(indexRequest);
return indexResponse.getId();
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project Zpider by zeroized.
the class ElasticClient method bulkIndex.
public List<String> bulkIndex(List<Map<String, ?>> docs) throws IOException {
BulkRequest bulkRequest = new BulkRequest();
for (Map<String, ?> doc : docs) {
IndexRequest indexRequest = new IndexRequest(index, type);
indexRequest.source(doc);
bulkRequest.add(indexRequest);
}
BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest);
return Arrays.stream(bulkResponse.getItems()).map(BulkItemResponse::getId).collect(Collectors.toList());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project main by JohnPeng739.
the class ElasticAccessorRest method save.
/**
* {@inheritDoc}
*
* @see ElasticAccessor#save(Base)
*/
@Override
public <T extends Base> T save(T t) throws UserInterfaceDalErrorException {
try {
Class<T> clazz = (Class<T>) t.getClass();
boolean isNew;
if (StringUtils.isBlank(t.getId())) {
// 新数据
t.setId(DigestUtils.uuid());
t.setCreatedTime(System.currentTimeMillis());
isNew = true;
} else {
T check = getById(t.getId(), clazz);
if (check != null) {
// 修改数据
t.setCreatedTime(check.getCreatedTime());
if (check instanceof BaseDict) {
((BaseDict) t).setCode(((BaseDict) check).getCode());
}
isNew = false;
} else {
isNew = true;
}
}
t.setUpdatedTime(System.currentTimeMillis());
t.setOperator(sessionDataStore.getCurrentUserCode());
if (isNew) {
IndexRequest request = new IndexRequest(index, t.getClass().getName(), t.getId());
request.source(JSON.toJSONString(t), XContentType.JSON);
client.index(request);
} else {
UpdateRequest request = new UpdateRequest(index, t.getClass().getName(), t.getId());
request.doc(JSON.toJSONString(t), XContentType.JSON);
client.update(request);
}
return getById(t.getId(), (Class<T>) t.getClass());
} catch (IOException ex) {
if (logger.isErrorEnabled()) {
logger.error("Save the data into elastic fail.", ex);
}
throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
}
}
Aggregations