Java Code Examples for org.elasticsearch.action.delete.DeleteRequest#id()

The following examples show how to use org.elasticsearch.action.delete.DeleteRequest#id() . 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: 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 2
Source File: ElasticSearchManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception {
    String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
    IndexRequest indexRequest = new IndexRequest("people");
    indexRequest.source(jsonObject, XContentType.JSON);

    IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
    String id = response.getId();

    GetRequest getRequest = new GetRequest("people");
    getRequest.id(id);

    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    System.out.println(getResponse.getSourceAsString());

    DeleteRequest deleteRequest = new DeleteRequest("people");
    deleteRequest.id(id);

    DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);

    assertEquals(Result.DELETED, deleteResponse.getResult());
}
 
Example 3
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 4
Source File: DeleteQueryParser.java    From elasticsearch-sql with MIT License 5 votes vote down vote up
private void parseDelete(ElasticDslContext dslContext) {
    dslContext.getParseResult().setSqlOperation(SqlOperation.DELETE);
    ElasticsearchParser.DeleteOperationContext deleteOperationContext = dslContext.getSqlContext().deleteOperation();
    DeleteRequest deleteRequest = new DeleteRequest(deleteOperationContext.tableRef(0).indexName.getText());
    deleteRequest.id(StringManager.removeStringSymbol(deleteOperationContext.identifyClause().id.getText()));
    if (deleteOperationContext.routingClause() != null) {
        deleteRequest.routing(StringManager.removeStringSymbol(deleteOperationContext.routingClause().STRING(0).getText()));
    }
    dslContext.getParseResult().setDeleteRequest(deleteRequest);
}
 
Example 5
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 6
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 7
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 8
Source File: EsTask.java    From tunnel with Apache License 2.0 4 votes vote down vote up
private DocWriteRequest toRequest(Invocation invocation) {
    DocWriteRequest req = null;

    EsConfig esConfig = invocation.getParameter(Constants.CONFIG_NAME);

    Map<String, String> values = invocation.getEvent().getDataList()
            .stream()
            .collect(Collectors.toMap(CellData::getName, CellData::getValue));

    // column_name,column_name
    String id = esConfig.getEsId()
            .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 (invocation.getEvent().getWalType()) {
        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 9
Source File: EsTask.java    From tunnel with Apache License 2.0 4 votes vote down vote up
private DocWriteRequest toRequest(Invocation invocation) {
    DocWriteRequest req = null;

    EsConfig esConfig = invocation.getParameter(Constants.CONFIG_NAME);

    Map<String, String> values = invocation.getEvent().getDataList()
            .stream()
            .collect(Collectors.toMap(CellData::getName, CellData::getValue));

    // column_name,column_name
    String id = esConfig.getEsId()
            .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 (invocation.getEvent().getWalType()) {
        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;
}