当前位置: 首页 > 后端技术 > PHP

PHP使用elasticsearchsearch安装和分词方式

时间:2023-03-29 20:50:44 PHP

1.背景为什么要用这个ESsearch?是因为看无云的漏洞案例库,不方便搜索。比如我要搜索一个SQL注入,mysql的匹配就像模糊匹配一样。搜索必须有SQL注入这四个字,连续找不方便。然后就想着做一个分词,这样查找起来就方便多了。我首先想到的是ES搜索。如何使用ES?2、安装ESSearch我们只需要一个JAVA环境,配置好Java环境变量即可。这些JAVA环境相信大家之前都配置过,这里就不多说了。现在只需要下载ES文件,不需要编译。只需下载它并将其放在目录中即可。下载地址:https://www.elastic.co/downlo...3.安装headhead是基于node开发的,所以需要安装nodenode下载地址:http://cdn.npm.taobao.org/dis。..在电脑任意目录下(不在elasticsearch目录下),执行命令,gitclonehttps://github.com/mobz/elasticsearch-head.gitcdelasticsearch-head/npminstall3.修改一些配置和修改两个A处:file:elasticsearch-headGruntfile.jsconnect:{server:{options:{port:9100,hostname:'*',base:'.',keepalive:true}}}添加配置,file:elasticsearch-5.6.0configelasticsearch.ymlhttp.cors.enabled:truehttp.cors.allow-origin:"*"4.输入npmrunstart启动5.访问head管理页面:http://localhost:9100/4.安装composer,我们需要安装composer,安装composer是为了什么?下载地址:https://getcomposer.org/Compo...下载完成后进入下一步安装。因为如果我们的PHP调用ES搜索接口,需要下载一个类库。1.下载composer.pharcurl-sShttps://getcomposer.org/installer|php在当前目录2.在当前目录创建composer.json文件{"require":{"elasticsearch/elasticsearch":"~2.0@beta"}}3.安装依赖phpcomposer.pharinstall5.安装分词插件也就是说,我们需要安装一个分词插件。在ES搜索中,Ik分词插件是中文分词最好的一款,安装也很方便。我们只需要从GitHub上下载对应版本的这个文件,解压到ES的plugin目录下,重启ES搜索服务即可。下载地址:https://github.com/medcl/elas...如何验证插件是否安装成功?我们可以通过下面的网址做一个分词测试。http://localhost:9200/你的图书馆名/_analyze?analyzer=ik_max_word&pretty=true&text=中华人民共和国我们可以在这个网址输入中华人民共和国;默认分词器会将中华人民共和国分为Chinese、Hua、people、people、republic和country。那么我们选择使用IK作为tokenizer之后,它就可以使用中华人民共和国作为一个词,China作为一个词。6、导入数据下面说一下如何将数据库中的数据导入到ES中。首先需要创建这样一个库,然后将数据以固定的格式插入到ES搜索中。下面是我的代码示例die("连接错误");mysql_select_db("wooyun",$conn);mysql_query("设置名称'UTF8'");return$conn;}//插入数据到ESsearchfunctioncreate_index($maxId,$client){//查询数据库中的数据$sql="SELECT*FROMbugswhereid>$maxIdlimit0,300";get_conn();@$result_bugs=mysql_query($sql);while(@$row=mysql_fetch_assoc(@$result_bugs)){$rtn[]=$row;}foreach($rtnas$val){$params=array();$params['body']=array('id'=>$val['id'],'wybug_id'=>$val['wybug_id'],'wybug_title'=>$val['wybug_title'],);$params['index']='wooyun';$params['类型']='标题';$client->index($params);}返回(计数($rtn)==300)?$val['id']:false;}set_time_limit(0);$client=Elasticsearch\ClientBuilder::create()->setHosts(['localhost'])->build();//删除所有数据$client->indices()->delete(['index'=>'wooyun']);$a=真;$maxId=0;while($a){$maxId=create_index($maxId,$client);如果(empty($maxId)){$a=false;}}7.查询数据setHosts(['localhost'])->build();//查询数据的组装$params=array();$params['index']='wooyun';$params['类型']='标题';$params['body']['query']['match']['wybug_title']=$keyword;$params['from']=$page;$params['大小']=$大小;//执行查询$rtn=$client->search($params)['hits'];//resultassembly装配数据$data['total']=$rtn['total'];$data['lists']=array_column($rtn['hits'],'_source');$data['lists']=formatData(array_column($data['lists'],'id'));返回$数据;}函数formartData($ids){$ids=implode($ids,',');$sql="select*frombugswhereidin($ids)";$data=mysql_query($sql);$rtn=[];while(@$row=mysql_fetch_assoc(@$data)){$rtn[]=$row;}返回$rtn;}$q0=isset($_GET['q'])?$_GET['q']:'SQL注册';$num="15";//每页显示15条$page=isset($_GET['page'])?intval($_GET['page']):1;$offset=($page-1)*$num;$esData=search($q0,$offset,$num);