为何会用到这个ES搜索?
是由于我在看乌云的漏洞案例库时候,搜索即为不方便。php
好比说说我要搜索一个 SQL注入node
那mysql匹配的时候是like模糊匹配,搜索必需要有SQL注入这四个字,连续的才能查找到那这样会不太方便。mysql
而后我就想着作一个分词,搜索起来会方便很多,第一个想到的就是ES搜索了。git
怎么去用ES呢?github
咱们只须要一个JAVA环境而且把Java的环境变量配置好,我相信这些JAVA环境你们以前都配置过,这里很少说。sql
那如今只须要下载ES的文件,也不须要编译,下载下来就好了,把他放到一个目录。
下载地址: https://www.elastic.co/downlo...数据库
head是基于node开发的,因此须要先安装node
node下载地址:http://cdn.npm.taobao.org/dis...npm
在电脑任意一个目录下(不要在elasticsearch目录里面),执行一下命令,json
git clone https://github.com/mobz/elasticsearch-head.git cd elasticsearch-head/ npm install
三、修改部分配置
修改两个地方:
文件:elasticsearch-headGruntfile.jscors
connect: { server: { options: { port: 9100, hostname: '*', base: '.', keepalive: true } } }
增长配置,文件:elasticsearch-5.6.0configelasticsearch.yml
http.cors.enabled: true http.cors.allow-origin: "*"
四、输入 npm run start 启动
五、访问head管理页面: http://localhost:9100/
就是咱们须要安装composer,安装composer干什么呢?
下载地址:https://getcomposer.org/Compo...
下载以后,直接下一步下一步就安装好了。
由于咱们PHP若是调用ES搜索的接口,咱们是须要去下载一个类库。
curl -sS https://getcomposer.org/installer | php
{ "require": { "elasticsearch/elasticsearch": "~2.0@beta" } }
php composer.phar install
就是说咱们须要安装一个分词插件。
在ES搜索当中Ik分词插件是中文分词最好用的一个,安装也极为方便。
咱们只须要到GitHub上把他对应版本的这个,文件下载下来,而后解压到ES的插件目录,而后从新启动一下ES搜索服务,就能够了。
下载地址:https://github.com/medcl/elas...
怎么去验证这个插件有没有安装成功呢?
咱们能够经过下面的URL,作分词测试。
http://localhost:9200/你的库名/_analyze?analyzer=ik_max_word&pretty=true&text=中华人民共和国
咱们能够在这个URL中输入,中华人民共和国; 默认的分词器他会把中华人民共和国分别以以 中、华、人、民、共、和、国。
那咱们选择用IK做为分词器后,它是能够把 中华人民共和国 做为一个词,把中华做为一个词。
如今说一下怎么把数据库中的数据导入到ES中,
首先须要创建这样一个库,
而后把数据按照固定的格式插入到ES搜索中。下面是个人一个代码示例
<?php require_once './vendor/autoload.php'; //链接MYSQL数据库 function get_conn() { @$conn = mysql_connect("localhost", "root", "") or die("error connecting"); mysql_select_db("wooyun", $conn); mysql_query("SET NAMES 'UTF8'"); return $conn; } //插入数据到ES搜索中 function create_index($maxId, $client) { //查询数据库中的数据 $sql = "SELECT * FROM bugs where id > $maxId limit 0,300"; get_conn(); @$result_bugs = mysql_query($sql); while (@$row = mysql_fetch_assoc(@$result_bugs)) { $rtn[] = $row; } foreach ($rtn as $val) { $params = array(); $params['body'] = array( 'id' => $val['id'], 'wybug_id' => $val['wybug_id'], 'wybug_title' => $val['wybug_title'], ); $params['index'] = 'wooyun'; $params['type'] = 'title'; $client->index($params); } return (count($rtn) == 300) ? $val['id'] : false; } set_time_limit(0); $client = Elasticsearch\ClientBuilder::create()->setHosts(['localhost'])->build(); //删除全部数据 $client->indices()->delete(['index' => 'wooyun']); $a = true; $maxId = 0; while ($a) { $maxId = create_index($maxId, $client); if (empty($maxId)) { $a = false; } }
<?php //引入mysql链接,和ES类库 require('conn.php'); require_once 'vendor/autoload.php'; function search($keyword, $page = 0, $size = 20) { //对象实例化 $client = Elasticsearch\ClientBuilder::create()->setHosts(['localhost'])->build(); //查询数据的拼装 $params = array(); $params['index'] = 'wooyun'; $params['type'] = 'title'; $params['body']['query']['match']['wybug_title'] = $keyword; $params['from'] = $page; $params['size'] = $size; //执行查询 $rtn = $client->search($params)['hits']; //结果组装组装数据 $data['total'] = $rtn['total']; $data['lists'] = array_column($rtn['hits'], '_source'); $data['lists'] = formartData(array_column($data['lists'], 'id')); return $data; } function formartData($ids) { $ids = implode($ids, ','); $sql = "select * from bugs where id in($ids)"; $data = mysql_query($sql); $rtn = []; while (@$row = mysql_fetch_assoc(@$data)) { $rtn[] = $row; } return $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);