ElasticSearch Java api-Delete Index

Source: Internet
Author: User

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();
    • 1
    • 2
    • 3
    • 4

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();
    • 1
    • 2

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
    • 1

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

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.