Search in sources :

Example 56 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch-indexing-proxy by codelibs.

the class ProxyActionFilter method getExecutor.

@SuppressWarnings("unchecked")
private <Request extends ActionRequest, Response extends ActionResponse> Supplier<Response> getExecutor(final Task task, final String action, final Request request) {
    if (BulkAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final BulkRequest req = (BulkRequest) request;
        for (final DocWriteRequest<?> subReq : req.requests()) {
            if (indexingProxyService.isTargetIndex(subReq.index())) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.requests().size()) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.requests().size());
        }
        return () -> {
            final List<BulkItemResponse> responseList = new ArrayList<>(req.requests().size());
            for (int i = 0; i < req.requests().size(); i++) {
                final DocWriteRequest<?> dwr = req.requests().get(i);
                if (dwr instanceof IndexRequest) {
                    final IndexRequest r = (IndexRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final IndexResponse response = new IndexResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else if (dwr instanceof UpdateRequest) {
                    final UpdateRequest r = (UpdateRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final UpdateResponse response = new UpdateResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), Result.CREATED);
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else if (dwr instanceof DeleteRequest) {
                    final DeleteRequest r = (DeleteRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final DeleteResponse response = new DeleteResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
                    response.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else {
                    responseList.add(new BulkItemResponse(i, dwr.opType(), new BulkItemResponse.Failure(dwr.index(), dwr.type(), dwr.id(), new ElasticsearchException("Unknown request: " + dwr))));
                }
            }
            return (Response) new BulkResponse(responseList.toArray(new BulkItemResponse[responseList.size()]), (System.nanoTime() - startTime) / 1000000);
        };
    } else if (DeleteAction.NAME.equals(action)) {
        final DeleteRequest req = (DeleteRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            final DeleteResponse res = new DeleteResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
            res.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
            return (Response) res;
        };
    } else if (DeleteByQueryAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final DeleteByQueryRequest req = (DeleteByQueryRequest) request;
        for (final String index : req.indices()) {
            if (indexingProxyService.isTargetIndex(index)) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.indices().length) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
        }
        return () -> {
            return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
        };
    } else if (IndexAction.NAME.equals(action)) {
        final IndexRequest req = (IndexRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            return (Response) new IndexResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
        };
    } else if (UpdateAction.NAME.equals(action)) {
        final UpdateRequest req = (UpdateRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            return (Response) new UpdateResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), Result.CREATED);
        };
    } else if (UpdateByQueryAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final UpdateByQueryRequest req = (UpdateByQueryRequest) request;
        for (final String index : req.indices()) {
            if (indexingProxyService.isTargetIndex(index)) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.indices().length) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
        }
        return () -> {
            return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
        };
    }
    return null;
}
Also used : Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) ShardId(org.elasticsearch.index.shard.ShardId) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) ArrayList(java.util.ArrayList) List(java.util.List) BulkByScrollTask(org.elasticsearch.index.reindex.BulkByScrollTask) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ActionResponse(org.elasticsearch.action.ActionResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 57 with IndexRequest

use of 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 58 with IndexRequest

use of 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 59 with IndexRequest

use of 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 60 with IndexRequest

use of 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)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)177 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)36 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)34 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)33 IOException (java.io.IOException)28 Test (org.junit.Test)27 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)26 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)16 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)15 Map (java.util.Map)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)13 GetRequest (org.elasticsearch.action.get.GetRequest)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)8