Elasticsearch-1.2.1 client connection demo

Source: Internet
Author: User

1. Download the zip package of the elasticsearch-1.2.1, decompress it, and double-clickElasticsearch. bat(For Windows) Start the server (Listening to port 9200 by default)

Access http: // localhost: 9200. If the following JSON data is displayed, the startup is successful.

 

2. Sample Code for client connection:

 

Package COM. jiaoyiping. othersimple;/*** created with intellij idea. * User: Jiao Yiping * Date: 14-7-8 * Time: * to change this template use file | Settings | file templates. */import Java. util. arraylist; import Java. util. list; import Org. elasticsearch. action. index. indexrequestbuilder; import Org. elasticsearch. action. index. indexresponse; import Org. elasticsearch. action. search. searchresponse; import Org. elasticsearch. client. client; import Org. elasticsearch. client. transport. transportclient; import Org. elasticsearch. common. transport. inetsockettransportaddress; import Org. elasticsearch. index. query. querybuilder; import Org. elasticsearch. index. query. querybuilders; import Org. elasticsearch. search. searchhit; import Org. elasticsearch. search. searchhits; import Org. slf4j. logger; import Org. slf4j. loggerfactory; public class elasticsearchhandler {logger = loggerfactory. getlogger (elasticsearchhandler. class); Private Client client; Public elasticsearchhandler () {// use the local machine as the node this ("127.0.0.1");} public elasticsearchhandler (string IPaddress) {client = new transportclient (). addtransportaddress (New inetsockettransportaddress (IPaddress, 9300);}/*** @ Param jsondata JSON-format data set ** @ return */Public void createindexresponse (string indexname, string type, list <string> jsondata) {// note the following when creating an index database. setrefresh (true) must be set here, otherwise the data indexrequestbuilder requestbuilder = client cannot be found during the first index creation. prepareindex (indexname, type ). setrefresh (true); For (INT I = 0; I <jsondata. size (); I ++) {requestbuilder.setsource(jsondata.get( I )cmd.exe cute (). actionget () ;}/ *** create Index * Param client * @ Param jsondata * @ return */Public indexresponse createindexresponse (string indexname, string type, string jsondata) {indexresponse response = client. prepareindex (indexname, type ). setsource (jsondata ). execute (). actionget (); return response ;} /*** execute the search * @ Param querybuilder * @ Param indexname * @ Param type * @ return */public list <medicine> searcher (querybuilder, string indexname, string type) {list <medicine> List = new arraylist <medicine> (); searchresponse = client. preparesearch (indexname ). settypes (type ). setquery (querybuilder ). execute (). actionget (); searchhits hits = searchresponse. gethits (); system. out. println ("number of records queried =" + hits. gettotalhits (); searchhit [] searchhists = hits. gethits (); If (searchhists. length> 0) {for (searchhit hit: searchhists) {INTEGER id = (integer) hit. getsource (). get ("ID"); string name = (string) hit. getsource (). get ("name"); string function = (string) hit. getsource (). get ("funciton"); list. add (new medicine (ID, name, function) ;}} client. close (); // close the connection (if it is not closed here, it will report: the remote host forcibly closes a connection) return list;} public static void main (string [] ARGs) {elasticsearchhandler eshandler = new elasticsearchhandler (); List <string> jsondata = DataFactory. getinitjsondata (); string indexname = "indexdemo"; string type = "typedemo"; // Add to index // eshandler. createindexresponse (indexname, type, jsondata); // query condition querybuilder = querybuilders. querystring ("Name: Name"); List <medicine> result = eshandler. searcher (querybuilder, indexname, type); For (INT I = 0; I <result. size (); I ++) {fig = result. get (I); system. out. println ("(" + medicine. GETID () + ") Name:" + medicine. getname () + "\ t" + medicine. getfunction ());}}}

 

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.