Search in sources :

Example 56 with IndexRequest

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;
    }
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) BwDateTime(org.bedework.calfacade.BwDateTime) HashMap(java.util.HashMap) Period(net.fortuna.ical4j.model.Period) BwEvent(org.bedework.calfacade.BwEvent) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) EsDocInfo(org.bedework.util.elasticsearch.EsDocInfo) RecurPeriods(org.bedework.icalendar.RecurUtil.RecurPeriods)

Example 57 with IndexRequest

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;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest)

Example 58 with IndexRequest

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();
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 59 with IndexRequest

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());
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 60 with IndexRequest

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);
    }
}
Also used : BaseDict(org.mx.dal.entity.BaseDict) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) IOException(java.io.IOException) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)187 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)37 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)37 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)34 IOException (java.io.IOException)30 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)27 Test (org.junit.Test)27 ElasticsearchException (org.elasticsearch.ElasticsearchException)21 IndexResponse (org.elasticsearch.action.index.IndexResponse)20 HashMap (java.util.HashMap)17 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)17 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)17 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)17 Map (java.util.Map)15 GetRequest (org.elasticsearch.action.get.GetRequest)14 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)11 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)10 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)9