ElasticSearch resthighlevelclient Tutorial (c) Delete && query Delete

Source: Internet
Author: User
Tags prepare
Preface

The necessity of deleting documents as an important part of the ES operation is beyond doubt. According to the official website document API, there are two ways to delete: One is directly based on Index,type,id directly deleted, and the second is the query delete, which is called the Delete by query API.

The first method of deletion is because the ID is uniquely identified, so if the document exists it can definitely specify the deletion.

The second way to delete the query, the process is equivalent to the first query to meet the criteria of the document, and then deleted according to the document ID. Therefore, you must pay attention to the query criteria to determine the query result range. Otherwise, many documents will be deleted by mistake.

When using the resthighlevelclient operation, there is no problem with the first API, while the second provides deletebyqueryrequest, but there is no corresponding method to execute the request. (if present, still look feel free.) You can only query and then delete the two-step walk. Although the two requests made by the client are certainly not fast with delete by query, it is only possible to use this method to make the national salvation.

Another way is to use restclient, flexible stitching JSON statements, send HTTP requests.

Message Source: https://discuss.elastic.co/t/delete-by-query-with-new-java-rest-api/107578 body Preparation Data

/put http://{{host}}:{{port}}/delete_demo
{"
    mappings": {"
        demo": {
            "Properties": {
                "content": {
                    "type": "Text",
                    "fields": {
                        "keyword": {
                            "type": "Keyword"
                        }
                    }
        }}
    }
}
/post http://{{host}}:{{port}}/_bulk
{"index": {"_index": "Delete_demo", "_type": "Demo"}}
{"Content": " Test1 "}
{" index ": {" _index ":" Delete_demo "," _type ":" Demo "}}
{" Content ":" Test1 "}
{" index ": {" _index ":" Delete_demo "," _type ":" Demo "}}
{" Content ":" Test1 Add "}
{" index ": {" _index ":" Delete_demo "," _type ":" Demo "}}
{" Content ":" Test2 "}

Note: When you bulk operation, you have to return the line after each row of data, followed by a blank line after the last line.

{"Took": 7, "errors": false, "items": [{"index": {"_index": "Delete_de
                Mo "," _type ":" Demo "," _id ":" Awexgsdw00f4t28wapen "," _version ": 1, "Result": "Created", "_shards": {"Total": 2, "successfu
            L ": 1," Failed ": 0}," created ": true," status ": 201  }}, {"index": {"_index": "Delete_demo", "_type": "Demo", "_id": "Awexgsdw00f4t28wapeo", "_version": 1, "result": "Created" , "_shards": {"Total": 2, "successful": 1, "f
        Ailed ": 0}," created ": true," status ": 201}}, {"Index": {"_index": "Delete_demo", "_type": "Demo", "_id": "Awexgsdw00f4t28wapep "," _version ": 1," result ":" Created "," _shards ": {" T Otal ": 2," successful ": 1," Failed ": 0}," created  ": True," status ": 201}}, {" index ": {" _index ": "Delete_demo", "_type": "Demo", "_id": "Awexgsdw00f4t28wapeq", "_version"
                    : 1, "result": "Created", "_shards": {"Total": 2, "Successful": 1, "Failed": 0}, "created": True, "sta Tus ": 201}}]}
ID Method Delete API Format
/delete Http://{{host}}:{{port}}/delete_demo/demo/awexgsdw00f4t28wapen
Java Client
public class Elkdaotest extends basetest{

    @Autowired
    private resthighlevelclient rhlclient;

    Private String index;

    Private String type;

    Private String ID;

    @Before public
    Void Prepare () {
        index = "Delete_demo";
        Type = "Demo";
        id = "AWEXGSDW00F4T28WAPEO";
    }

    @Test public
    Void Delete () {
        deleterequest deleterequest = new Deleterequest (index,type,id);
        Deleteresponse response = null;
        try {
            response = Rhlclient.delete (deleterequest),
        } catch (IOException e) {
            //TODO auto-generated catch B Lock
            E.printstacktrace ();
        }
        SYSTEM.OUT.PRINTLN (response);
    }
}

The same deletion succeeds.

The use of rhlclient can refer to the previous blog post Elasticsearch Rest High level Client Tutorial (a) General operations. Delete by Query API Mode

First, restore the previous data back to four documents.

/post http://{{host}}:{{port}}/delete_demo/demo/_delete_by_query
{"
    query": {"
        match": {
            "content ":" Test1 "}}
}
{
    "took": +,
    "timed_out": false,
    "Total": 3,
    "deleted": 3,
    "batches": 1,
    "Version_ Conflicts ": 0,
    " noops ": 0,
    " retries ": {
        " bulk ": 0,
        " search ": 0
    },
    " Throttled_millis ": 0,
    "Requests_per_second":-1,
    "Throttled_until_millis": 0,
    "failures": []
}
/get Http://{{host}}:{{port}}/delete_demo/demo/_search
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "Failed": 0
    },
    "hits": {
        "total": 1,
        "Max_score": 1,
        "hits": [
            {
                "_index": " Delete_demo ",
                " _type ":" Demo ",
                " _id ":" Awexkdse00f4t28waaff ",
                " _score ": 1,
                " _source ": {
                    " Content ":" Test2 "}}
        ]
    }
}

The result shows that three documents were deleted, namely Test1,test1,test1 Add, leaving only test2. It is clear that the results of the query have been deleted.

If you use term, it is also deleted by query matching.

/post http://{{host}}:{{port}}/delete_demo/demo/_delete_by_query
{"
    query": {"term
        ": {
            " Content.keyword ":" Test1 "}}
}
{
    "took": 6,
    "Timed_out": false,
    "Total": 2,
    "deleted": 2,
    "Batches": 1,
    "version_conflicts ": 0,
    " noops ": 0,
    " retries ": {
        " bulk ": 0,
        " search ": 0
    },
    " Throttled_millis ": 0,
    " Requests_per_second ":-1,
    " Throttled_until_millis ": 0,
    " failures ": []
}

Proof that Delete by query is the process of querying and then deleting. Java Client

Using Resthighlevelclient

public class Elkdaotest extends Basetest {@Autowired private resthighlevelclient rhlclient;

Private String index;

Private String type;

Private String DeleteText;
    @Before public void Prepare () {index = "Delete_demo";
    Type = "Demo";
DeleteText = "Test1";
        } @Test public void Delete () {try {searchsourcebuilder sourcebuilder = new Searchsourcebuilder ();
        Sourcebuilder.timeout (New TimeValue (2, timeunit.seconds));
        Termquerybuilder termQueryBuilder1 = querybuilders.termquery ("Content.keyword", DeleteText);
        Sourcebuilder.query (TermQueryBuilder1);
        SearchRequest searchrequest = new SearchRequest (index);
        Searchrequest.types (type);
        Searchrequest.source (Sourcebuilder);
        SearchResponse response = Rhlclient.search (searchrequest);
        Searchhits hits = Response.gethits ();
        list<string> docids = new arraylist<> (hits.gethits (). length); for (Searchhit hit:hits) {Docids.add (Hit.getid ());
        } bulkrequest bulkrequest = new Bulkrequest ();
            for (String id:docids) {deleterequest deleterequest = new Deleterequest (index, type, id);
        Bulkrequest.add (deleterequest);
    } rhlclient.bulk (Bulkrequest);
    } catch (IOException e) {e.printstacktrace (); }

}
}

Recover the data and execute the above code, the query only test1 add and test2 two documents, delete the query successfully. The specific query is no longer posted.

Using Restclient

As mentioned in the previous series, Rhlclient is the encapsulation of restclient, while some of Rhlclient's features are still being perfected and not implemented in Java. It would be nice to use Restclient to invoke the ES service directly in the form of HTTP.

public class Elkdaotest extends Basetest {@Autowired private restclient restclient;

Private String index;

Private String type;

Private String DeleteText;
    @Before public void Prepare () {index = "Delete_demo";
    Type = "Demo";
DeleteText = "Test1";
    } @Test public void Delete () {String endPoint = "/" + index + "/" + type + "/_delete_by_query";
    String Source = genereatequerystring ();
    httpentity entity = new Nstringentity (source, Contenttype.application_json); try {restclient.performrequest ("POST", Endpoint,collections.<string, String> emptymap (), EN
    tity);
    } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
    }} public String genereatequerystring () {indexrequest indexrequest = new Indexrequest ();
    Xcontentbuilder Builder; try {builder = Jsonxcontent.contentbuilder (). StartObject (). StartObject ("que
              Ry ")          . StartObject ("term"). Field ("Content.keyword", DeleteText). E
        Ndobject (). EndObject (). EndObject ();
    Indexrequest.source (builder);
    } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
    The String Source = Indexrequest.source (). utf8tostring ();
return source; }
}

After running, also deleted the Test1 two documents, the function realizes. The advantage is that there is no need to initiate two HTTP connections to save time. Summary

As far as the delete operation is concerned, Resthighlevelclient is not perfect enough, so it is necessary to contact Restclient's flexibility to achieve the functionality we want. Series Articles:

ElasticSearch Rest High Level Client Tutorial (i) General operations

ElasticSearch resthighlevelclient Tutorial (ii) Operation index

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.