Java Code Examples for org.elasticsearch.action.delete.DeleteRequest

The following examples show how to use org.elasticsearch.action.delete.DeleteRequest. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeString(index);
    timeout.writeTo(out);
    out.writeLong(ingestId);
    shardId.writeTo(out);
    out.writeVInt(actionRequests.size());
    for (ActionRequest<?> actionRequest : actionRequests) {
        if (actionRequest == null) {
            out.writeBoolean(false);
            continue;
        }
        out.writeBoolean(true);
        if (actionRequest instanceof IndexRequest) {
            out.writeBoolean(true);
        } else if (actionRequest instanceof DeleteRequest) {
            out.writeBoolean(false);
        } else {
            throw new ElasticsearchException("action request not supported: " + actionRequest.getClass().getName());
        }
        actionRequest.writeTo(out);
    }
}
 
Example 2
Source Project: conductor   Source File: ElasticSearchDAOV6.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void removeWorkflow(String workflowId) {
    try {
        long startTime = Instant.now().toEpochMilli();
        DeleteRequest request = new DeleteRequest(workflowIndexName, WORKFLOW_DOC_TYPE, workflowId);
        DeleteResponse response = elasticSearchClient.delete(request).actionGet();
        if (response.getResult() == DocWriteResponse.Result.DELETED) {
            LOGGER.error("Index removal failed - document not found by id: {}", workflowId);
        }
        long endTime = Instant.now().toEpochMilli();
        LOGGER.debug("Time taken {} for removing workflow: {}", endTime - startTime, workflowId);
        Monitors.recordESIndexTime("remove_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Throwable e) {
        LOGGER.error("Failed to remove workflow {} from index", workflowId, e);
        Monitors.error(CLASS_NAME, "remove");
    }
}
 
Example 3
Source Project: elasticsearch-helper   Source File: HttpBulkNodeClient.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public HttpBulkNodeClient delete(String index, String type, String id) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    try {
        if (metric != null) {
            metric.getCurrentIngest().inc(index, type, id);
        }
        bulkProcessor.add(new DeleteRequest(index).type(type).id(id));
    } catch (Exception e) {
        throwable = e;
        closed = true;
        logger.error("bulk add of delete failed: " + e.getMessage(), e);
    }
    return this;
}
 
Example 4
@Override
public IngestTransportClient delete(String index, String type, String id) {
    if (closed) {
        if (throwable != null) {
            throw new ElasticsearchException("client is closed, possible reason: ", throwable);
        } else {
            throw new ElasticsearchException("client is closed");
        }
    }
    try {
        metric.getCurrentIngest().inc(index, type, id);
        ingestProcessor.add(new DeleteRequest(index).type(type).id(id));
    } catch (Exception e) {
        logger.error("add of delete request failed: " + e.getMessage(), e);
        throwable = e;
        closed = true;
    }
    return this;
}
 
Example 5
Source Project: conductor   Source File: ElasticSearchDAOV5.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void removeWorkflow(String workflowId) {
    try {
        long startTime = Instant.now().toEpochMilli();
        DeleteRequest request = new DeleteRequest(indexName, WORKFLOW_DOC_TYPE, workflowId);
        DeleteResponse response = elasticSearchClient.delete(request).actionGet();
        if (response.getResult() == DocWriteResponse.Result.DELETED) {
            logger.error("Index removal failed - document not found by id: {}", workflowId);
        }
        long endTime = Instant.now().toEpochMilli();
        logger.debug("Time taken {} for removing workflow: {}", endTime - startTime, workflowId);
        Monitors.recordESIndexTime("remove_workflow", WORKFLOW_DOC_TYPE, endTime - startTime);
        Monitors.recordWorkerQueueSize("indexQueue", ((ThreadPoolExecutor) executorService).getQueue().size());
    } catch (Exception e) {
        logger.error("Failed to remove workflow {} from index", workflowId, e);
        Monitors.error(className, "remove");
    }
}
 
Example 6
Source Project: elasticsearch-helper   Source File: BulkNodeClient.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public BulkNodeClient delete(String index, String type, String id) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    try {
        if (metric != null) {
            metric.getCurrentIngest().inc(index, type, id);
        }
        bulkProcessor.add(new DeleteRequest(index).type(type).id(id));
    } catch (Exception e) {
        throwable = e;
        closed = true;
        logger.error("bulk add of delete failed: " + e.getMessage(), e);
    }
    return this;
}
 
Example 7
@Test
public void deleteMappingsByDetectorUUID_successful() throws Exception {
    val id = "adsvade8^szx";
    val detectorUuid = UUID.randomUUID();
    val searchIndex = "2";
    val lookUpTime = 100;

    val deleteResponse = mockDeleteResponse(id);
    val searchResponse = mockSearchResponse(searchIndex, lookUpTime, detectorUuid.toString());
    when(legacyElasticSearchClient.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(searchResponse);
    when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(new DeleteResponse());
    repoUnderTest.deleteMappingsByDetectorUUID(detectorUuid);
    verify(legacyElasticSearchClient, atLeastOnce()).delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT));
    assertEquals(id, deleteResponse.getId());
    assertEquals(elasticSearchProperties.getIndexName(), deleteResponse.getIndex());
    assertEquals("DELETED", deleteResponse.getResult().toString());
}
 
Example 8
public String bulkDelete(String index,String type,String... ids){
	try {
		if(xclient==null){
			init();
		}
		BulkRequest request = new BulkRequest();
		for (String id : ids) {
			request.add(new DeleteRequest(index, type, id));
		}
		BulkResponse result = xclient.bulk(request);
		return result.toString();
	}catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 9
Source Project: elasticsearch-pool   Source File: BulkApiMain.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    try{
        RestHighLevelClient client = HighLevelClient.getInstance();
        BulkRequest bulkRequest = new BulkRequest();
        for(int i=1;i<4;i++) {
            bulkRequest.add(new IndexRequest("jingma2_20180716", "testlog", String.valueOf(i)).source(buildIndexData()));
        }
        bulkRequest.add(new DeleteRequest("jingma2_20180716", "testlog", "1"));
        bulkRequest.add(new UpdateRequest("jingma2_20180716", "testlog", "2").doc(XContentType.JSON,"name","马靖2"));

        BulkResponse bulkResponse = client.bulk(bulkRequest);
        System.out.println(bulkResponse);

    }finally{
        HighLevelClient.close();
    }
}
 
Example 10
Source Project: elasticsearch-helper   Source File: BulkNodeClient.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public BulkNodeClient bulkDelete(DeleteRequest deleteRequest) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    try {
        if (metric != null) {
            metric.getCurrentIngest().inc(deleteRequest.index(), deleteRequest.type(), deleteRequest.id());
        }
        bulkProcessor.add(deleteRequest);
    } catch (Exception e) {
        throwable = e;
        closed = true;
        logger.error("bulk add of delete failed: " + e.getMessage(), e);
    }
    return this;
}
 
Example 11
Source Project: java-study   Source File: EsUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @return boolean
 * @Author pancm
 * @Description //删除数据
 * 根据ID进行单条删除
 * @Date 2019/3/21
 * @Param []
 **/
public static boolean deleteById(String index, String type, String id) throws IOException {
    if (index == null || type == null || id == null) {
        return true;
    }
    try {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.id(id);
        deleteRequest.index(index);
        deleteRequest.type(type);
        // 同步删除
        client.delete(deleteRequest, RequestOptions.DEFAULT);
    } finally {
        if (isAutoClose) {
            close();
        }
    }
    return true;
}
 
Example 12
Source Project: java-study   Source File: EsUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @return boolean
 * @Author pancm
 * @Description //批量删除数据
 * 根据ID进行批量删除
 * @Date 2019/3/21
 * @Param []
 **/
public static boolean deleteByIds(String index, String type, Set<String> ids) throws IOException {
    if (index == null || type == null || ids == null) {
        return true;
    }
    try {
        BulkRequest requestBulk = new BulkRequest();
        ids.forEach(id -> {
            DeleteRequest deleteRequest = new DeleteRequest(index, type, id);
            requestBulk.add(deleteRequest);
        });
        // 同步删除
        client.bulk(requestBulk, RequestOptions.DEFAULT);
    } finally {
        if (isAutoClose) {
            close();
        }
    }
    return false;
}
 
Example 13
Source Project: sql4es   Source File: ESUpdateState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Executes the list with requests as a bulk with maximum number of requests per bulk
 * @param requests
 * @param maxRequestsPerBulk
 * @return
 * @throws SQLException
 */
private int execute(List<?> requests, int maxRequestsPerBulk) throws SQLException{
	int result = 0;
	BulkRequestBuilder bulkReq = client.prepareBulk();
	for(Object req : requests){
		if(req instanceof IndexRequest)	bulkReq.add((IndexRequest)req);
		else if(req instanceof UpdateRequest) bulkReq.add((UpdateRequest)req);
		else if(req instanceof DeleteRequest) bulkReq.add((DeleteRequest)req);
		else if(req instanceof IndexRequestBuilder) bulkReq.add((IndexRequestBuilder)req);
		else if(req instanceof UpdateRequestBuilder) bulkReq.add((UpdateRequestBuilder)req);
		else if(req instanceof DeleteRequestBuilder) bulkReq.add((DeleteRequestBuilder)req);
		else throw new SQLException("Type "+req.getClass()+" cannot be added to a bulk request");
		
		if(bulkReq.numberOfActions() > maxRequestsPerBulk){
			result += bulkReq.get().getItems().length;
			bulkReq = client.prepareBulk();
		}
	}
	
	if(bulkReq.numberOfActions() > 0){
		result += bulkReq.get().getItems().length;
	}
	return result;
}
 
Example 14
public void bulkDeleteDocument(String type, List<Integer> ids) {
    try {
        if (this.instance() == null) return;
        BulkRequest requests = new BulkRequest();
        int count = 0;
        for (Integer id : ids) {
            count++;
            DeleteRequest request = new DeleteRequest(name, type, String.valueOf(id));
            requests.add(request);
            if (count % 1000 == 0) {
                client.bulk(requests, RequestOptions.DEFAULT);
                requests.requests().clear();
                count = 0;
            }
        }
        if (requests.numberOfActions() > 0) client.bulk(requests, RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error(e.getMessage());
    }
}
 
Example 15
Source Project: Elasticsearch   Source File: ESDeleteTask.java    License: Apache License 2.0 6 votes vote down vote up
public ESDeleteTask(UUID jobId,
                    ESDeleteNode node,
                    TransportDeleteAction transport,
                    JobContextService jobContextService) {
    super(jobId, node.executionPhaseId(), node.docKeys().size(), jobContextService);
    List<DeleteRequest> requests = new ArrayList<>(node.docKeys().size());
    List<ActionListener> listeners = new ArrayList<>(node.docKeys().size());
    for (DocKeys.DocKey docKey : node.docKeys()) {
        DeleteRequest request = new DeleteRequest(
                ESGetTask.indexName(node.tableInfo(), docKey.partitionValues()),
                Constants.DEFAULT_MAPPING_TYPE, docKey.id());
        request.routing(docKey.routing());
        if (docKey.version().isPresent()) {
            request.version(docKey.version().get());
        }
        requests.add(request);
        SettableFuture<TaskResult> result = SettableFuture.create();
        results.add(result);
        listeners.add(new DeleteResponseListener(result));
    }

    createContext("delete", requests, listeners, transport, null);
}
 
Example 16
Source Project: Elasticsearch   Source File: IndicesTTLService.java    License: Apache License 2.0 6 votes vote down vote up
private void purgeShards(List<IndexShard> shardsToPurge) {
    for (IndexShard shardToPurge : shardsToPurge) {
        Query query = shardToPurge.indexService().mapperService().smartNameFieldType(TTLFieldMapper.NAME).rangeQuery(null, System.currentTimeMillis(), false, true);
        Engine.Searcher searcher = shardToPurge.acquireSearcher("indices_ttl");
        try {
            logger.debug("[{}][{}] purging shard", shardToPurge.routingEntry().index(), shardToPurge.routingEntry().id());
            ExpiredDocsCollector expiredDocsCollector = new ExpiredDocsCollector();
            searcher.searcher().search(query, expiredDocsCollector);
            List<DocToPurge> docsToPurge = expiredDocsCollector.getDocsToPurge();

            BulkRequest bulkRequest = new BulkRequest();
            for (DocToPurge docToPurge : docsToPurge) {

                bulkRequest.add(new DeleteRequest().index(shardToPurge.routingEntry().index()).type(docToPurge.type).id(docToPurge.id).version(docToPurge.version).routing(docToPurge.routing));
                bulkRequest = processBulkIfNeeded(bulkRequest, false);
            }
            processBulkIfNeeded(bulkRequest, true);
        } catch (Exception e) {
            logger.warn("failed to purge", e);
        } finally {
            searcher.close();
        }
    }
}
 
Example 17
Source Project: tunnel   Source File: EsPublisher.java    License: Apache License 2.0 5 votes vote down vote up
private DocWriteRequest eventToRequest(EsConfig esConfig, EventType eventType, Map<String, String> values) {

        DocWriteRequest req = null;

        // column_name,column_name
        String id = esConfig.getEsIdFieldNames()
                .stream()
                .map(esId -> String.valueOf(values.get(esId)))
                .reduce((s1, s2) -> s1 + esConfig.getSeparator() + s2)
                .orElse("");

        if (StringUtils.isBlank(id)) {
            return null;
        }
        String type = esConfig.getType();
        String index = esConfig.getIndex();


        switch (eventType) {
            case INSERT:
            case UPDATE:
                UpdateRequest ur = new UpdateRequest(index, type, id);
                ur.doc(values);
                ur.docAsUpsert(true);
                req = ur;
                break;
            case DELETE:
                DeleteRequest dr = new DeleteRequest(index, type, id);
                dr.id(id);
                req = dr;
                break;
            default:
                break;
        }
        return req;
    }
 
Example 18
Source Project: bdt   Source File: ElasticSearchUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a document by its id.
 *
 * @param indexName
 *
 * @param id
 */
public void deleteDocument(String indexName, String id) {
    DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
    try {

        client.delete(deleteRequest, RequestOptions.DEFAULT);

    } catch (IOException e) {
        throw new ElasticsearchException("Error deleting document");
    }
}
 
Example 19
@Override
public void deleteArticle(Object id) {

    DeleteRequest request = new DeleteRequest(index, type, id.toString());
    try {
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        if (LogKit.isDebugEnabled()) {
            LogKit.debug(response.toString());
        }
    } catch (Exception e) {
        LOG.error(e.toString(), e);
    }

}
 
Example 20
Source Project: Flink-CEPplus   Source File: RequestIndexer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add multiple {@link ActionRequest} to the indexer to prepare for sending requests to Elasticsearch.
 *
 * @param actionRequests The multiple {@link ActionRequest} to add.
 * @deprecated use the {@link DeleteRequest}, {@link IndexRequest} or {@link UpdateRequest}
 */
@Deprecated
default void add(ActionRequest... actionRequests) {
	for (ActionRequest actionRequest : actionRequests) {
		if (actionRequest instanceof IndexRequest) {
			add((IndexRequest) actionRequest);
		} else if (actionRequest instanceof DeleteRequest) {
			add((DeleteRequest) actionRequest);
		} else if (actionRequest instanceof UpdateRequest) {
			add((UpdateRequest) actionRequest);
		} else {
			throw new IllegalArgumentException("RequestIndexer only supports Index, Delete and Update requests");
		}
	}
}
 
Example 21
Source Project: elasticshell   Source File: DeleteRequestBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(DeleteRequest request, DeleteResponse response, XContentBuilder builder) throws IOException {
    return builder.startObject()
            .field(Fields.OK, true)
            .field(Fields.FOUND, !response.isNotFound())
            .field(Fields._INDEX, response.getIndex())
            .field(Fields._TYPE, response.getType())
            .field(Fields._ID, response.getId())
            .field(Fields._VERSION, response.getVersion())
            .endObject();
}
 
Example 22
Source Project: flink   Source File: BufferingNoOpRequestIndexer.java    License: Apache License 2.0 5 votes vote down vote up
void processBufferedRequests(RequestIndexer actualIndexer) {
	for (ActionRequest request : bufferedRequests) {
		if (request instanceof IndexRequest) {
			actualIndexer.add((IndexRequest) request);
		} else if (request instanceof DeleteRequest) {
			actualIndexer.add((DeleteRequest) request);
		} else if (request instanceof UpdateRequest) {
			actualIndexer.add((UpdateRequest) request);
		}
	}

	bufferedRequests.clear();
}
 
Example 23
@Override
public void add(DeleteRequest... deleteRequests) {
	for (DeleteRequest deleteRequest : deleteRequests) {
		if (flushOnCheckpoint) {
			numPendingRequestsRef.getAndIncrement();
		}
		this.bulkProcessor.add(deleteRequest);
	}
}
 
Example 24
Source Project: flink   Source File: BufferingNoOpRequestIndexer.java    License: Apache License 2.0 5 votes vote down vote up
void processBufferedRequests(RequestIndexer actualIndexer) {
	for (ActionRequest request : bufferedRequests) {
		if (request instanceof IndexRequest) {
			actualIndexer.add((IndexRequest) request);
		} else if (request instanceof DeleteRequest) {
			actualIndexer.add((DeleteRequest) request);
		} else if (request instanceof UpdateRequest) {
			actualIndexer.add((UpdateRequest) request);
		}
	}

	bufferedRequests.clear();
}
 
Example 25
Source Project: heroic   Source File: TransportConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void execute(
    @NotNull DeleteRequest request,
    @NotNull ActionListener<DeleteResponse> listener
) {
    client.delete(request, listener);
}
 
Example 26
Source Project: elasticsearch-helper   Source File: IngestRequest.java    License: Apache License 2.0 5 votes vote down vote up
public IngestRequest add(Iterable<ActionRequest<?>> requests) {
    for (ActionRequest<?> request : requests) {
        if (request instanceof IndexRequest) {
            add((IndexRequest) request);
        } else if (request instanceof DeleteRequest) {
            add((DeleteRequest) request);
        } else {
            throw new IllegalArgumentException("no support for request [" + request + "]");
        }
    }
    return this;
}
 
Example 27
Source Project: alchemy   Source File: Elasticsearch6SinkFunction.java    License: Apache License 2.0 5 votes vote down vote up
private void processDelete(Row row, RequestIndexer indexer) {
    final String key = createKey(row);
    final DeleteRequest deleteRequest = requestFactory.createDeleteRequest(
            getIndex(row),
            docType,
            key);
    indexer.add(deleteRequest);
}
 
Example 28
Source Project: AsuraFramework   Source File: EsDoc.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 批量del
 * @param indexName
 * @param indexType
 * @param
 * @return
 */
private  BulkResponse bulkDelete(String indexName, String indexType, List<String> ids){
    TransportClient client = esClientFactory.getClient();
    BulkResponse bulkResponse=null;
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for(int i=0;i<ids.size();i++){
        bulkRequest.add(new DeleteRequest(esClientFactory.getIndexs(indexName), indexType,ids.get(i) ));
    }
    bulkResponse = bulkRequest.get();
    // 处理错误信息
    handBulkResponseException(bulkResponse);
    return bulkResponse;
}
 
Example 29
Source Project: pinpoint   Source File: ElasticsearchExecutorInterceptor.java    License: Apache License 2.0 5 votes vote down vote up
private void recordeESattributes(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {

        if (recordESVersion) {
            if (target instanceof ClusterInfoAccessor) {
                //record elasticsearch version and cluster name.
                recorder.recordAttribute(ElasticsearchConstants.ARGS_VERSION_ANNOTATION_KEY, ((ClusterInfoAccessor) target)._$PINPOINT$_getClusterInfo());
            }
        }
        if (recordDsl) {

            if (args[0] instanceof SearchRequest) {
                SearchRequest request = (SearchRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(request.source().toString(), 256));
            } else if (args[0] instanceof GetRequest) {
//                GetRequest request = (GetRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            } else if (args[0] instanceof IndexRequest) {
//                IndexRequest request = (IndexRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            } else if (args[0] instanceof DeleteRequest) {
//                DeleteRequest request = (DeleteRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            } else if (args[0] instanceof UpdateRequest) {
//                UpdateRequest request = (UpdateRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            }
        }


    }
 
Example 30
Source Project: anomaly-detection   Source File: CheckpointDao.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the model checkpoint for the model.
 *
 * @param modelId id of the model
 * @param listener onReponse is called with null when the operation is completed
 */
public void deleteModelCheckpoint(String modelId, ActionListener<Void> listener) {
    clientUtil
        .<DeleteRequest, DeleteResponse>asyncRequest(
            new DeleteRequest(indexName, DOC_TYPE, modelId),
            client::delete,
            ActionListener.wrap(r -> listener.onResponse(null), listener::onFailure)
        );
}