Delete can either delete the entire index library, delete the document under the Index library based on the document ID, or delete all eligible data by querying the query criteria.
First, delete the entire index library
The following example removes the IndexName index:
DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName) .execute().actionGet();
You can determine whether the deletion succeeded based on the method of the Deleteindexresponse object isAcknowledged()
, and the return value is a Boolean type.
An exception occurs if the IndexName of the descendant does not exist. You can first determine whether an index exists:
IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);IndicesExistsResponse inExistsResponse = client.admin().indices() .exists(inExistsRequest).actionGet();
Depending on the Boolean return value of the Isexists () method of the Indicesexistsresponse object, the index library is determined to exist.
Second, delete by ID
The following example is a document that deletes an index named blog with a type of Article,id 1:
DeleteResponse dResponse = client.prepareDelete("blog", "article", "1").execute().actionGet();
by Deleteresponse the Isfound () method of the object, you can get the success of the deletion, and the return value is a Boolean type.
Third, delete through query
elasticsearch-2.3 and older APIs are not the same, install plugins:
delete-by-query
When a cluster has multiple nodes, each node needs to be installed and restarted.
If you want to remove a plug-in, you can execute the following command:
delete-by-query
Delete the index named Twitter, with all documents of type Tweet,user field containing kimchy:
DELETE /twitter/tweet/_query?q=user:kimchy
Java API Reference Elasticsearch Java API (vi) –deletebyquery.
Iv. Java Demo
Package CN. com. bropen. es; import static Org. elasticsearch. Index. query. Querybuilders. termquery; Import Java. Net. InetAddress; Import Java. Net. Unknownhostexception; Import org. elasticsearch. Action. admin. Indices. Create. Createindexrequest; Import org. elasticsearch. Action. admin. Indices. Create. Createindexresponse; Import org. elasticsearch. Action. admin. Indices. Delete. Deleteindexresponse; Import org. elasticsearch. Action. admin. Indices. exists. Indices. Indicesexistsrequest; Import org. elasticsearch. Action. admin. Indices. exists. Indices. Indicesexistsresponse; Import org. elasticsearch. Action. Delete. Deleteresponse; Import org. elasticsearch. Client. Client; Import org. elasticsearch. Client. Transport. Transportclient; Import org. elasticsearch. Common. Transport. Inetsockettransportaddress; Import org. elasticsearch. Index. query. QueryBuilder;p Ublic class Elasticsearchcreate {private static String ServerIP ="127.0.0.1";//ElasticSearch server IP private static int serverport =9300;//Port Private Client Client; public static void Main (string[] args) {try {client client = Transportclient. Builder (). Build (). addtransportaddress (New inetsockettransportaddress (inetaddress. Getbyname ("127.0.0.1"),9300)); Deleteresponse dresponse = Client. Preparedelete ("Blog","Article","11"). Execute (). Actionget (); if (dresponse. Isfound ()) {System. Out. println ("Delete succeeded"); } else {System. Out. println ("Delete Failed"); } QueryBuilder QB1 = Termquery ("Title","Hibernate"); } catch (Unknownhostexception e) {E. Printstacktrace (); } deleteindex ("Test");//delete index library named Test}//delete index library public static void Deleteindex (String indexname) {try {if (!isindexexists (IndexName)) {Syste M. Out. println (IndexName +"Not EXISTS"); } else {Client client = Transportclient. Builder (). Build (). addtransportaddress (New inetsockettransportaddress (inetaddress. Getbyname (ServerIP), ServerPort)); Deleteindexresponse dresponse = Client. Admin (). Indices (). Preparedelete (IndexName). Execute (). Actionget (); if (dresponse. isacknowledged ()) {System. Out. println ("Delete Index" +indexname+"successfully!"); }else{System. Out. println ("Fail to delete index" +indexname); }}} catch (Unknownhostexception e) {E. Printstacktrace (); }}//CREATE index library public static void CreateIndex (String indexname) {try {client client = Transportclient. Builder (). Build (). addtransportaddress (New inetsockettransportaddress (inetaddress. Getbyname (ServerIP), ServerPort)); Create an index library if (isindexexists ("IndexName")) {System. Out. println ("Index" + IndexName +"Already exits!"); } else {createindexrequest cindexrequest = new Createindexrequest ("IndexName"); Createindexresponse cindexresponse = Client. Admin (). Indices (). Create (Cindexrequest). Actionget (); if (cindexresponse. isacknowledged ()) {System. Out. println (The CREATE INDEX successfully! "); } else {System. Out. println ("Fail to create index!"); }}} catch (Unknownhostexception e) {E. Printstacktrace (); }}//Determine if the index has an incoming parameter for the index library name public static Boolean isindexexists (String indexname) {Boolean flag = False; try {Client client = Transportclient. Builder (). Build ().addtransportaddress (New inetsockettransportaddress (InetAddress.getbyname (ServerIP), serverport)) ; Indicesexistsrequest inexistsrequest = new Indicesexistsrequest (indexname) .admin () .indices () .exists (inexistsrequest) .actionget () . Isexists ()) {flag = True.printstacktrace () ;} return flag< Span class= "hljs-comment" >; }}
ElasticSearch Java api-Delete index