Building Elasticsearch's mapping through Java's domain class

Source: Internet
Author: User

Create your own elasticsearch mapping by giving a class of Java

Order's Domain Class

 Public class Order {    public  String system_id;      Public String finger_id;      Public String merchant_id;      Public Double Tx_money;      Public String pay_date;      Public String payment_status;      Public Date settle_date;      Public Long Goods_count;}

Mapping startup class for building Elasticsearch

 Public classstartmapping {Private StaticLog log = Logfactory.getlog (startmapping.class); Private StaticTransportclient client =NULL; Private Staticindicesadminclient adminclient; Private StaticTransportclient getclient ()throwsunknownhostexception {Settings Settings= Settings.settingsbuilder (). Put ("Cluster.name", Constant.cluster). Build (); Transportclient Client=Transportclient.builder (). settings (Settings). build (); Client.addtransportaddress (Newinetsockettransportaddress (Inetaddress.getbyname (constant.eshost), constant.esport)); returnclient; }        Static{        Try{Client=getclient (); } Catch(unknownhostexception e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {//elasticsearch-mapping Builddomapping (); }        /*** Determine if an index in Elasticsearch exists*/    Private Static Booleanexists (String index) {adminclient=client.admin (). indices (); Indicesexistsrequest Request=Newindicesexistsrequest (index); Indicesexistsresponse Response=adminclient.exists (Request). Actionget (); if(Response.isexists ()) {return true; }          return false; }          Private Static voiddomapping () {if(Exists (Constant.index)) {System.out.println (Constant.index+ "Index already exists! "); Log.info (Constant.index+ "Index already exists! "); return; } String Template= "{\ n" + "\t\" "+constant.index+" \ ": {\ n" + "\t\t\" properties\ ": {                                \ n "+" \t\t{#}\n "+" \t\t\t\t}\n " + "\t\t}\n" + "}"; String fieldstring=getclassmapping (); Template= Template.replace ("{#}", fieldstring);        Client.admin (). Indices (). Preparecreate (Constant.index). addmapping (Constant.index, template). get ();        System.out.println (template); System.out.println ("Create Elasticsearch mapping complete!!!" "); Log.info ("Create Elasticsearch mapping complete!!!" "); }        /*** fields in Elasticsearch from the field mapping of the class *@return     */     Public StaticString getclassmapping () {StringBuilder fieldstring=NewStringBuilder (); Field[] Fields= Order.class. Getdeclaredfields ();  for(inti = 0; i < fields.length; i++) {fieldstring.append ("\t\t\t\t\" "+ fields[i].getname (). toLowerCase () +" \ ": {\ n"); Fieldstring.append ("\t\t\t\t\t\t\" type\ ": \" "+ Getelasticsearchmappingtype (Fields[i].gettype (). Getsimplename ()) +" \ n "); if(i = = Fields.length-1) {fieldstring.append ("\t\t\t\t\t}\n"); } Else{fieldstring.append ("\t\t\t\t\t},\n"); }        }        returnfieldstring.tostring (); }    Private Staticstring Getelasticsearchmappingtype (String varType) {string es= "String"; Switch(varType) { Case"Date": Es= "Date\" \ n "+" \t\t\t\t\t\t,\ "format\": \ "Yyyy-mm-dd hh:mm:ss\" \ n "+" \t\t\t\t\t\t,\ "null_value\": \ "1900-01-01 00:00:01\ "";  Break;  Case"Double": Es= "Double\" \ n "+" \t\t\t\t\t\t,\ "null_value\": \ "nan\" ";  Break;  Case"Long": Es= "Long\" ";  Break; default: Es= "String\" ";  Break; }        returnes; }      }

About Elasticsearch mapping fields manually added
Curl-xput http://192.168.46.165:9200/t_order/_mapping/t_order?pretty-d ' {"Properties": {"order_ip": {"type": " String "}}} '

Building Elasticsearch's mapping through Java's domain class

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.