《02ElasticSearch环境搭建与Java对接.docx》由会员分享,可在线阅读,更多相关《02ElasticSearch环境搭建与Java对接.docx(7页珍藏版)》请在第一文库网上搜索。
1、1安装es1.1下载e1asticsearch6.5.0地址:https:/www.e1astic.co/cn/down1oads/e1asticsearchGhnpmweiMiUQrMm”一,e,、63St1CProductsC1oUdS(McCuscomrs1amXaeeQDOwnPadsPastRe1easesOwrAnFHtmBcsejrcS4)E1asticsearch6.5.0nowiNovember14.2018选择down1oad-pastre1eases-e1asticsearch6.5.01.2解压完毕后可打开confige1asticsearch.ym1修改配置文件#C1
2、uster#*Useadescriptivenameforyourc1uster:c1uster.name:bipC1ustername为集群名称,利用c1ient连接e1asticsearch时需要用到该名称。# Node*# Useadescriptivenameforthenode:*node.name:node-1001node.name为节点名称,可以不错修改,使用默认的节点名称即可1 Network*SetthebindaddresstoaspecificIP(IPv4orIPv6):network.,host:.tSetacustomporeforHTTP:http.port:2
3、work.host为es的访问ip,设置为0.0.0.0即为所有ip均可访问http.port为es的端口,不用修改,使用默认的9200即可。1.3运行bin/e1asticsearch.bat即可启动,当看到下面的信息时,即安装成功。“nae:node-1”,*c1uster-nae*:my-app1ication,C1ustejuuid:,WC9-1fhOTH-6DJVDU9Sj7QNFrversion“munber”:6.5.0”,“bui1f1avor:defau1t,bui1c1type*:zip”,bui1d_hash*:816e6f6”,bui1c1date*:201-11-09
4、T18:58:36.352602Z*,*,bui1d-snapshot*:fa1se,“1ucene_version:7.5.0,mnijur1wire_compatibi1ity_versinx,:5.6.0,a,Jidnimuii1index_copatibi1ity_version*:*5.0.Cr1“tag1ine:YouKnow,forSearch*2安装head2.1下载e1asticsearchTead并解压。2.2安装node,js使用node-v查看自己node,js的版本,使用npm-v查看npm的版本,执行npminsta11-ggrunt-CIi命令,安装grunt。注
5、:第一次安装可能报错,如果报错,再安装一遍。2.3修改配置文件e1asticsearch安装目录下conf/e1asticsearch.ym1,在文末力口入http,cors.enab1ed:truehttp,cors.a11ow-origin:*node,master:truenode,data:true注意:参数值之前定要加空格。2. 4修改服务器地址进入Site文件目录,修改app.js文件,搜索“app-baSeJJr1”,更改I后地址为e1asticsearch的IP+9200端口。1oca1host为e1asticsearch的IPappApp=ui.AbstractWidget.
6、extend(defau1ts:ba9e_uri:nu111,In1t:tunc11on(parent)ti.super():this.prefsservices.Preferences.instance;this.boseurithis.config.baseuri11this.prefs.get(napp-ba3c-urit,)卜U/;1.32.,1.m;m,if(this.base_uri.charAt(this.base_uri.1ength)?,*)(XHRrequestfai1sifceUR1isnotendingwithawwth1s.base_ur1*t;2.5启动head并查
7、看e1asticsearch-head目录下,按Shift+鼠标右键,选择“在此处打开PoWerShe11”窗口,并输入npmrunstarto如果显示如下信息,即为启动成功。如果启动失败,就在e1asticsearch-head目录下,按Sh曲+鼠标右键,选择“在此处打开POWerSheI1”窗口,并输入npmruninsta11,安装完需要从组建,然后再输入npmrunstart%SD:Specia1SoftIaree1asticsearch-6.5.Oe1asticsearch-head-masternpmrunstarte1asticsearch-headO.O.OstartD:Spe
8、cia1Softfaree1asticsearch-6.5.Oe1asticsearch-head-mastergruntserverQunnintt“connect:SerVer”(COnneCt)taskFaitingforever.Startedconnectwebserveronhttp:/1oca1host:9100出现上述信息即启动成功,此时可访问head的web界面。-CIogstqiooE1asticsearch卜kwtmy-app1icatioB5HS史兄BTMK*M(*M*US,一,一,co11ection3e:412M(41.2M)doo:6(6)AUnasstoned*
9、辔#s回国回国图3.安装ik分词3.1 在github下载ik6.5.0(选择相应版本)3. 2用maven编译源码(InVnc1eanpackage)编译完成后在target/rea1eases下生成e1asticsearch-ana1ysis-ik-6.5.O.zip,解压至es安装目录的PIUginS下(可以更名)。薄e1asticsearch-ana1ysis-ik-6.5.0.zipI.BBtt(D:)setupfi1ese1asticsearch-6.5.0p1ugins具Co帮助(H)利库中共享新建文件突名称“修改日期ana1ysis-ik2023/6/1810:09备注:这里不
10、需要修改任何配置文件即可完成。如果是老版本2.x,则需要在配置文件中加入index.ana1ysis.ana1yzer.ik.type:,ikuo3.3重启es,重启成功即分词安装成功。4.在idea中导入es的java操作的c1ientoorg.e1asticsearch.c1iente1asticsearch-rest-high-1eve1-c1ient6.6.0索引创建初始化批量配苴属性,符合其中一些条件,即会姓爱批量操作pub1icstaticvoid()throwsEceptionbutProcessor=8u1kProcessor.frM4ep(c1int/newBu1kProce
11、ssor.1istener()Overridepub1icvoidbeforeBu1k(1ongidBu1kRequestreq)System.out.print1n(mid:-id+,req:-*req);i-,Overridepub1icvoidafterBu1k(1ongid,Bu1kRequestreq,Throwab1ecause)System.out.print1n(*idsrid+req:+req+*cause:+cause.getHessage();/:,.iTi4:i.但一号七三)ICYerriCEpub1icvoidafterBUIk(IOngid,Bu1kRequestr
12、eq,Bu1kResponserep)System.out.print1n(id:+id+rq:*req+,rp:*rep);=二上7HV二二,可甘一;与心)SetBu1kActionsC300)i访尚打1理门.setBu1kSize(newByteSizeVa1ue(1,ByteSizeUnit.MB),吟.setConcurretRequests(1),a亓专连”程.setF1ushinterva1(TimeVa1ue.timeV1ueSeconds(59)/3,卡bui1d()j建8u1APeuessoF批量操作,执行索引创建/,关闭es的butkft东pub1icstaticvoidc
13、1oseESBu1k()trybu1Processor.awaitC1ose(rmeout:3,TimeUit.MINUTES);/::,::fi,1:,catch(Ecption)e.printStackTrace();)Pub1icstaticid()A/使用游标语RaO”。嘴其他的数据库一员就可以潮嘲-状以季班R加蜕循1istrecord1ist=I1OadAeUOrdt1Pw,m:,ZB.QMH)wi1(Pecord1ist.iterator().hasNet()HapmaptoJp(record1ist.iterator().next();/?/居世目e。P组关bu1kPr0cessor.add(nowIndexRequest(index:*indcxt*,type:typc*id:source(ap);)o*ES8uM);,W闭批步算1找到查询逻辑代码,使用EIasticSearch的查询去替换。*es查询多条件查询Vpub1ic1istMap(Mapparams)throwsUnknownHostException1istMapresu1t=newArray1ist();/第一个查询SearchRequestBui1dersrb1=c1ient.prepareSearch(paratns.get(in