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
Delete the index named Twitter, with all documents of type Tweet,user field containing kimchy:
DELETE /twitter/tweet/_query?q=user:kimchy
The Java API has not been found for the moment.
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;public class Elasticsearchcreate {private static String ServerIP ="127.0.0.1";//ElasticSearch server IPprivate static int serverport =9300;//PortPrivate 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","One"). 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 the index library named Test}//delete index library public static void Deleteindex (String indexname) {try {if (!isindexexists (IndexName)) {System. 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 = Transport Client. 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);Indicesexistsresponse inexistsresponse = Client. Admin(). Indices(). Exists(inexistsrequest). Actionget();if (inexistsresponse. Isexists()) {flag = True;} else {flag = False;}} catch (Unknownhostexception e) {E. Printstacktrace();} return Flag;}}
ElasticSearch Java Api-delete Index