Elasticsearch 5.x Delete by Query API (conditionally deleted)

Source: Internet
Author: User

In the 2.X version, this delete by query function was removed because the official thought it would cause some errors to install the plugin itself if needed.

Bin/plugin Install Delete-by-query

When you need to use

DELETE/Index name/type/_query{"query"needed to be emptied: {"match_all " : {}}}

Then in the 5.x version of the API is again the official back to the self-feature. That's what the new feature says.

Delete-by-query and update-by-query back to the core, formerly plug-ins, can now be used directly, but also built on the Reindex mechanism. (es1.x version is direct support, extracted as plug-in in es2.x, 5. x continue to return to direct support)

I checked the Official handbook and found that the use was different.

Official address: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-delete-by-query.html

That is, replacing the previous _query for _delete_by_query can achieve the goal.

Delete via header plugin

Use the head plugin to delete the specified data http: // serverhost:9200/index/type/_delete_by_query   Post  {   "query" : {      "match": {         Attribute field: Property value to delete}      }   }

Java Code Implementation:

String index ="wareic"; Boolquerybuilder QueryBuilder=querybuilders.boolquery (); Querybuilder.must (Querybuilders.termquery ("GroupID","42023")); Bulkindexbyscrollresponse Response=DeleteByQueryAction.INSTANCE.newRequestBuilder (client). Filter (QueryBuilder). Source (in DEX).Get(); Longdeleted =response.getdeleted (); System. out. println (deleted);

Another way to achieve this:

String index ="wareic"; String type="Product"; Bulkrequestbuilder bulkrequest=Client.preparebulk (); Searchrequestbuilder Searchrequestbuilder=Client.preparesearch (Index). settypes (type);//page OutSearchrequestbuilder.setfrom (0). SetSize ( +); Boolquerybuilder QueryBuilder=querybuilders.boolquery (); Querybuilder.must (Querybuilders.termquery ("GroupID","3445")); Searchrequestbuilder.setquery (QueryBuilder); SearchResponse Response= Searchrequestbuilder.execute ().Get(); for(Searchhit hit:response.getHits ()) {String ID=Hit.getid ();  Bulkrequest.add (Client.preparedelete (index, type, ID). request ()); Bulkresponse Bulkresponse= Bulkrequest.Get(); if(Bulkresponse.hasfailures ()) { for(Bulkitemresponse item:bulkResponse.getItems ()) {System. out. println (Item.getfailuremessage ()); }  } Else{System. out. println ("Delete OK"); }  

Article Reference address: https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.2/java-docs-delete-by-query.html

Elasticsearch 5.x Delete by Query API (conditionally deleted)

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.