Elasticsearch5.0 Java Api (ii)--Update index

Source: Internet
Author: User

Elasticsearch provides a variety of ways to update your index, here's a brief introduction to five of them

1  Packagecom.juyun.test;2 3 Importjava.io.IOException;4 Importjava.net.InetAddress;5 Importjava.util.concurrent.ExecutionException;6 7 Importorg.elasticsearch.action.index.IndexRequest;8 Importorg.elasticsearch.action.update.UpdateRequest;9 Importorg.elasticsearch.client.Client;Ten Importorg.elasticsearch.common.settings.Settings; One Importorg.elasticsearch.common.transport.InetSocketTransportAddress; A ImportOrg.elasticsearch.script.Script; - ImportOrg.elasticsearch.script.ScriptService; - Importorg.elasticsearch.transport.client.PreBuiltTransportClient; the Import StaticOrg.elasticsearch.common.xcontent.xcontentfactory.*; -  -  Public classElasticsearchupdate { -      +     Private Staticclient client; -  +     /** A * Multiple ways to update the index library at      * @paramargs -      */ -      Public Static voidMain (string[] args) { -  -         Try { -             //set the cluster name inSettings Settings = Settings.builder (). Put ("Cluster.name", "Elasticsearch"). Build (); -             //Create client toClient =Newprebuilttransportclient (Settings) +. addtransportaddress (NewInetsockettransportaddress (Inetaddress.getbyname ("172.16.0.210"), 9300)); -            the}Catch(Exception e) { * e.printstacktrace (); $         }Panax Notoginseng upMethod5 (); -          the         //Close Client + client.close (); A     } the      +     //method One: Create a updaterequest and send it to the client. -     //Use this method to make an error when the original document does not exist $      Public Static voidupMethod1 () { $         Try { -Updaterequest urequest =Newupdaterequest (); -Urequest.index ("Flow"); theUrequest.type ("Data"); -Urequest.id ("avvl93fjl55uxupfsv9x");WuyiUrequest.doc (Jsonbuilder (). StartObject (). Field ("Inbyte", 200.0). EndObject ()); the client.update (urequest). get (); -}Catch(IOException e) { Wu e.printstacktrace (); -}Catch(interruptedexception e) { About e.printstacktrace (); $}Catch(executionexception e) { - e.printstacktrace (); -         } -  A     } +  the     //method Two: Prepareupdate () update the index with a script -     //need to elasticsearch.yml, add a line: Script.engine.groovy.inline.update:on, and then restart ES $      Public Static voidupMethod2 () { theClient.prepareupdate ("Flow", "Data", "2") the. Setscript (NewScript ("ctx._source.inbyte = \" 3.14\ "", ScriptService.ScriptType.INLINE,NULL,NULL)) the . get (); the     } -  in     //Method Three: prepareupdate () update index with Doc the      Public Static voidupMethod3 () { the         Try { AboutClient.prepareupdate ("Flow", "Data", "avvl93jtl55uxupfsv9y") the. Setdoc (Jsonbuilder (). StartObject (). Field ("Inbyte", 3.14). EndObject ()). get (); the}Catch(IOException e) { the e.printstacktrace (); +         } -  the     }Bayi  the     //method Four: Add a new field the      Public Static voidupMethod4 () { -         Try { -Updaterequest updaterequest =NewUpdaterequest ("Flow", "Data", "avvl93jtl55uxupfsv9y") the. doc (Jsonbuilder (). StartObject () field ("Package", "100"). EndObject ()); the client.update (updaterequest). get (); the}Catch(IOException e) { the e.printstacktrace (); -}Catch(interruptedexception e) { the e.printstacktrace (); the}Catch(executionexception e) { the e.printstacktrace ();94         } the     } the  the     //method Five: Upsert Create a new index if the document does not exist98     //if present, the index is updated according to Updaterequest and if it does not exist, the new Indexrequest index is created. About      Public Static voidUpMethod5 () { -         Try {101Indexrequest indexrequest =NewIndexrequest ("Flow", "Data", "5"). Source (Jsonbuilder (). StartObject ()102. Field ("Time", "20170508"). Field ("Package", "121"). EndObject ());103 104Updaterequest URequest2 =NewUpdaterequest ("Flow", "Data", "5"). doc ( theJsonbuilder (). StartObject (). Field ("Time", "20170508"). Field ("Package", "12114"). EndObject ())106 . Upsert (indexrequest);107 client.update (UREQUEST2). get ();108}Catch(IOException e) {109 e.printstacktrace (); the}Catch(interruptedexception e) {111 e.printstacktrace (); the}Catch(executionexception e) {113 e.printstacktrace (); the         } the  the     }117 118}
elasticsearchupdate

Elasticsearch5.0 Java Api (ii)--Update index

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.