Java API Operations Elasticsearch

Source: Internet
Author: User

Elasticsearch's maven dependency
<Dependency>            <groupId>Org.elasticsearch</groupId>            <Artifactid>Elasticsearch</Artifactid>            <version>2.1.1</version>            <Scope>Test</Scope></Dependency>
 PackageCom.cheguo.merchant.app.toolkit;ImportOrg.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;ImportOrg.elasticsearch.action.search.SearchResponse;Importorg.elasticsearch.action.update.UpdateRequest;Importorg.elasticsearch.client.Client;Importorg.elasticsearch.client.transport.TransportClient;Importorg.elasticsearch.common.transport.InetSocketTransportAddress;ImportOrg.elasticsearch.common.xcontent.XContentBuilder;Importorg.elasticsearch.common.xcontent.XContentFactory;ImportOrg.elasticsearch.index.query.MatchQueryBuilder;ImportOrg.elasticsearch.index.query.QueryBuilder;Importorg.elasticsearch.index.query.QueryBuilders;ImportOrg.elasticsearch.search.SearchHit;Importorg.elasticsearch.search.SearchHits;Importjava.io.IOException;Importjava.net.InetAddress;Importjava.net.UnknownHostException;Importjava.util.Date;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;Import StaticOrg.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;/*** Created with IntelliJ idea. * *@author: Yangxianyu * DATE:2017/12/14 * Time: PM 3:13 * Description:*/ Public classTestelk { Public Static voidMain (string[] args) {Try{Client Client=Transportclient.builder (). Build (). Addtransportaddress (NewInetsockettransportaddress (Inetaddress.getbyname ("10.10.13.7"), 9300));            Get (client); //put (client); //setmapping (client);Client.close (); } Catch(unknownhostexception e) {e.printstacktrace (); }    }     Public Static voidGet (Client client) {String Query= "Audi"; string[] Str=NewString[3]; str[0] = "Car_name"; str[1] = "Cars"; if(isnumeric (query)) {str[2] = "Sale_price"; } QueryBuilder QueryBuilder=querybuilders.multimatchquery (QUERY,STR)//avoid the background of the word to bring the Austrian or the search out. operator (MatchQueryBuilder.Operator.AND); SearchResponse Response= Client.preparesearch ("Carsrc"). Settypes ("Carsource")). SetSize (3)//show number of bars per page. Setfrom (3*0)//start the query from that one. Setquery (QueryBuilder). Execute (). Actionget (); Searchhits searchhits=response.gethits (); if(Searchhits.totalhits () >0){             for(Searchhit searchhit:searchhits) {System.out.println ("Total>>>>" +searchhit.gettype () + "source>>>" +searchhit.getsourceasstring ()); }        }Else{System.out.println ("Total 0"); }    }     Public Static voidput (client client) {Updaterequest urequest=Newupdaterequest (); Try {            //urequest = client.prepareupdate ("Carsrc", "Carsource", "243"). Setdoc (Model2xcontent ()). get ();Urequest.index ("Carsrc"); Urequest.type ("Carsource"); Urequest.id ("243"); Urequest.doc (Jsonbuilder (). StartObject (). Field ("car_source_id", "232"). Field ("Car_source_code", "" "). Field ("Sales_region", "" "). Field ("Car_code", "" "). Field ("car_id", "78732"). Field ("Car_name", "Wenzhou"). Field ("Cars", ""). Field ("MSRP", "" "). Field ("Sale_price", "" "). Field ("OVER_MSRP", "" "). Field ("Car_color", "" "). Field ("Car_trim_color", "" "). Field ("Remark", "" "). Field ("Source_type", "" "). Field ("Source_kind", "" "). Field ("Source_status", "" "). Field ("Product_date", "" "). Field ("Procedure", "" "). Field ("Car_vin", "" "). Field ("Area_code", "" "). Field ("Area_name", "" "). Field ("Status", "" "). Field ("company_id", "" "). Field ("Company_Name", "" "). Field ("order_user_id", "" "). Field ("pay_user_id", "" "). Field ("Transport", "" "). Field ("Plate_limit", "" "). Field ("Validate", "" "). Field ("Valid_status", "" "). Field ("Buy_deposit", "" "). Field ("Sale_deposit", "" "). Field ("pay_order_id", "" "). Field ("Score", "" "). Field ("Company_type", "" "). Field ("Come_from", "" "). Field ("Brand_logo", "" "). Field ("Pic_flag", "" "). Field ("Cost_price", "" "). Field ("Mini_price", "" "). Field ("Reason", "" "). Field ("Real_price", "" "). Field ("Number", "" "). Field ("Bill", ""). Field ("Source_flag", "2"). Field ("Gmt_create",NewDate ()). Field ("Gmt_modify",NewDate ()). Field ("Price_type", "" "). Field ("Price_value", "" "). EndObject ());        Client.update (urequest). get (); } Catch(Exception e) {e.printstacktrace (); }        //. Prepareindex ("Carsrc", "Carsource", "1"). //SetSource (Model2json ()). get ();//if (urequest.) {//System.out.println ("add Success");//        }    }     Public Static voidsetmapping (client client) {Try{putmappingresponse response=client.admin (). Indices (). prepareputmapping ("Carsrc"). SetType ("Carsource"). SetSource (Xcontentfactory.jsonbuilder (). StartObject () . StartObject ("Car_name"). StartObject ("Cars"). Field ("Analyzer", "whitespace"). Field ("Type", "string"). EndObject (). EndObject (). Endobjec        T ()). Execute (). Actionget (); } Catch(IOException e) {e.printstacktrace (); }    }     Public Staticstring Model2json () {string Jsondata= ""; Try{xcontentbuilder Xcontentbuilder=Jsonbuilder (); Xcontentbuilder.startobject (). Field ("car_id", "78732"). Field ("car_source_id", "243"). Field ("Sales_region", "Wenzhou"). EndObject (); } Catch(IOException e) {e.printstacktrace (); }        returnJsondata; }     Public StaticXcontentbuilder model2xcontent () {Xcontentbuilder Xcontentbuilder=NULL; Try{Xcontentbuilder=Jsonbuilder (); Xcontentbuilder.startobject (). Field ("",""). Field ("",""). EndObject (); } Catch(IOException e) {e.printstacktrace (); }        returnXcontentbuilder; }     Public Static Booleanisnumeric (String str) {pattern pattern= Pattern.compile ("-?[ 0-9]+.? [0-9]+]); Matcher Isnum=Pattern.matcher (str); if(!isnum.matches ()) {            return false; }        return true; }}

Java API Operations Elasticsearch

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.