org.elasticsearch.action.delete.DeleteRequest Java Examples

The following examples show how to use org.elasticsearch.action.delete.DeleteRequest. 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
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: IndicesTTLService.java    From Elasticsearch with 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 #3
Source File: ESDeleteTask.java    From Elasticsearch with 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 #4
Source File: IngestReplicaShardRequest.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: ElasticSearchService.java    From pybbs with GNU Affero General Public License v3.0 6 votes vote down vote up
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 #6
Source File: ElasticSearchDAOV6.java    From conductor with 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 #7
Source File: ESUpdateState.java    From sql4es with 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 #8
Source File: EsUtil.java    From java-study with 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 #9
Source File: BulkApiMain.java    From elasticsearch-pool with 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 File: HttpBulkNodeClient.java    From elasticsearch-helper with 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 #11
Source File: IngestTransportClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@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 #12
Source File: EsUtil.java    From java-study with 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 #13
Source File: ElasticsearchHighRestFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
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 #14
Source File: ElasticSearchDAOV5.java    From conductor with 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 #15
Source File: BulkNodeClient.java    From elasticsearch-helper with 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 #16
Source File: BulkNodeClient.java    From elasticsearch-helper with 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 #17
Source File: ConfService.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
private void callRemoveES(ConfigurationLogstash cl) throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest(INDEX_STORAGE, "doc", cl.getIdEs());
    DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest);
    if (deleteResponse != null) {
        deleteResponse.status();
        deleteResponse.toString();
        cl.setIdEs(null);
    }
}
 
Example #18
Source File: ElasticSearchService.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
/**
 * 删除文章数据
 */
public void deleteArticle(long articleId) throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest(BLOG_INDEX, ARTICLE_TYPE, String.valueOf(articleId));
    DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);

    logger.info("删除数据 | {}", JSON.toJSONString(deleteResponse));
}
 
Example #19
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeException.class)
public void deleteMappingsByDetectorUUID_fail() throws IOException {
    val detectorUuid = UUID.randomUUID();
    val searchIndex = "2";
    val lookUpTime = 100;
    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))).thenThrow(new IOException());
    repoUnderTest.deleteMappingsByDetectorUUID(detectorUuid);
}
 
Example #20
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteDetectorMapping_successful() throws Exception {
    val id = "adsvade8^szx";
    DeleteResponse deleteResponse = mockDeleteResponse(id);
    when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(new DeleteResponse());
    repoUnderTest.deleteDetectorMapping(id);
    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 #21
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeException.class)
public void deleteDetectorMapping_fail() throws IOException {
    val id = "adsvade8^szx";
    DeleteRequest deleteRequest = new DeleteRequest(elasticSearchProperties.getIndexName(),
            elasticSearchProperties.getDocType(), id);
    when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenThrow(new IOException());
    repoUnderTest.deleteDetectorMapping(id);
}
 
Example #22
Source File: IngestRequest.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    timeout.writeTo(out);
    out.writeLong(ingestId);
    out.writeVInt(requests.size());
    for (ActionRequest<?> request : requests) {
        if (request instanceof IndexRequest) {
            out.writeByte((byte) 0);
        } else if (request instanceof DeleteRequest) {
            out.writeByte((byte) 1);
        }
        request.writeTo(out);
    }
}
 
Example #23
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testScenarioAsBulkRequest() throws IOException, ExecutionException, InterruptedException {
    doBulk(new BulkRequest()
        .add(new IndexRequest(INDEX, DOC_TYPE, "2").source(
            jsonBuilder()
                .startObject()
                .field(FOO, BAR)
                .endObject()
        ))
        .add(new DeleteRequest(INDEX, DOC_TYPE, "2")));

    validateSpanContentAfterBulkRequest();
}
 
Example #24
Source File: BulkDelete.java    From elastic-crud with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(final SearchHit hit) {
  final DeleteRequest delete = client
    .prepareDelete()
    .setIndex(hit.getIndex())
    .setType(hit.getType())
    .setId(hit.getId())
    .request();

  request.get().add(delete);
}
 
Example #25
Source File: DeleteByQueryPerformer.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Mono<BulkResponse> deleteRetrievedIds(SearchResponse searchResponse, RoutingKey routingKey) {
    BulkRequest request = new BulkRequest();

    for (SearchHit hit : searchResponse.getHits()) {
        request.add(
            new DeleteRequest(aliasName.getValue())
                .type(NodeMappingFactory.DEFAULT_MAPPING_NAME)
                .id(hit.getId())
                .routing(routingKey.asString()));
    }

    return client.bulk(request, RequestOptions.DEFAULT);
}
 
Example #26
Source File: EsPublisher.java    From tunnel with 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 #27
Source File: Elasticsearch7BulkProcessorIndexer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void add(DeleteRequest... deleteRequests) {
	for (DeleteRequest deleteRequest : deleteRequests) {
		if (flushOnCheckpoint) {
			numPendingRequestsRef.getAndIncrement();
		}
		this.bulkProcessor.add(deleteRequest);
	}
}
 
Example #28
Source File: BookingDaoESImp.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
@Override
public void removeBookingBucket(BookingBucket bookingBucket) throws IOException
{
    DeleteRequest deleteRequest = new DeleteRequest(this.bookingBucketsIndex);
    deleteRequest.type(ES_TYPE);
    deleteRequest.id(bookingBucket.getId());
    this.esclient.delete(deleteRequest);
}
 
Example #29
Source File: BookingDaoESImp.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
@Override
public void unlockBooking() throws IOException
{
    DeleteRequest deleteRequest = new DeleteRequest(this.bookingsIndex);
    deleteRequest.type(ES_TYPE);
    deleteRequest.id(LOCK_BOOKING_ID);
    this.esclient.delete(deleteRequest);
}
 
Example #30
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private DeleteResponse doDelete(DeleteRequest deleteRequest) throws IOException, ExecutionException, InterruptedException {
    if (async) {
        ClientMethod<DeleteRequest, DeleteResponse> method =
            (request, listener) -> client.deleteAsync(request, listener);
        return invokeAsync(deleteRequest, method);
    }
    return client.delete(deleteRequest);
}