-- ----------------------------
-- Table structure for articles
-- ----------------------------
DROP TABLE IF EXISTS `articles`;
CREATE TABLE `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL COMMENT '标题',
`content` text COMMENT '内容',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of articles
-- ----------------------------
INSERT INTO `articles` VALUES ('1', 'Laravel 测试1', 'Laravel 测试文章内容1');
INSERT INTO `articles` VALUES ('2', 'Laravel 测试2', 'Laravel 测试文章内容2');
INSERT INTO `articles` VALUES ('3', 'Laravel 测试3', 'Laravel 测试文章内容3');
INSERT INTO `articles` VALUES ('4', 'Laravel 测试4', 'Laravel 测试文章内容4');
INSERT INTO `articles` VALUES ('5', 'Laravel 测试5', 'Laravel 测试文章内容5');
INSERT INTO `articles` VALUES ('6', 'Laravel 测试6', 'Laravel 测试文章内容6');
INSERT INTO `articles` VALUES ('7', 'Laravel 测试7', 'Laravel 测试文章内容7');
后台代码
<?php
namespace app\index\controller;
use think\Db;
use think\Config;
//引入es搜索类
//require './vendor/autoload.php';
use Elasticsearch\ClientBuilder; use think\Controller;
use think\Request; class Index extends Controller
{
public function index()
{ return $this->fetch(); } public function getsearch(){ $client = ClientBuilder::create()->build();
$keys = Request::instance()->param('keyword');
$keys = $keys ? $keys : '测试';
$params = [
'index' => 'article_index',
'type' => 'article_type',
'body' => [
'query' => [
'match' => [
'content' => $keys
]
]
]
];
$response = $client->search($params);
$str = '';
$list = $response['hits']['hits'];
//pp($list);die;
$str .= '<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>'; $str .= '<table class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>content</th>
</tr>
</thead>
<tbody>'; foreach ($list as $k => $v) {
$str .= '<tr><td>' . $v['_source']['id'] . '</td><td>' . $v['_source']['title'] . '</td><td>' . $v['_source']['content'] . '</td></tr>';
}
$str .='</tbody></table>';
return $str; } public function search() { /*$client = ClientBuilder::create()->setHosts($hosts)->build();*/
//实例化es类;在项目中引入自动加载文件,并且实例化一个客户端:
$client = ClientBuilder::create()->build(); $keys = Request::instance()->param('keyword');
$keys = $keys ? $keys : '6';
$params = [
'index' => 'article_index',
'type' => 'article_type',
'body' => [
'query' => [
'match' => [
'content' => $keys
]
]
]
];
$response = $client->search($params);
return json($response['hits']['hits']);
//pp($response['hits']['hits']);
die; try {
//将文档加入索引
//echo ClientBuilder::$aaa;
// $data = db::name('articles')->select();
//查询出多条数据添加索引
/*foreach ($data as $k => $v) { $params = [
'index' => 'article_index',//数据库名
'type' => 'article_type',//表名
'id' => 'article_' . $v['id'],//主键
'body' => [
'id' => $v['id'],
'title' => $v['title'],
'content' => $v['content'],
],
];
$response = $client->index($params); }
*/
//从索引中获取文档
/* $getparams = [
'index' => 'article_index',
'type' => 'article_type',
'id' => 'article_1'
];
$res = $client->get($getparams);*/ //从索引中删除文档
/*$delparams = [
'index' => 'article_index',
'type' => 'article_type',
'id' => 'article_1'
];
$res = $client->delete($delparams);
*/
//删除索引
/*$params = [
'index' => 'articles_index'
];
$res = $client->indices()->delete($params);
print_r($res);*/ //搜索
/*$serparams = [
'index' => 'article_index',
'type' => 'article_type',
]; $serparams['body']['query']['match']['content'] = $_POST['keyword'];
$resech = $client->search($serparams);*/ //pp($resech); // pp($data);
} catch (Exception $e) {
echo $e->getMessage();
}
} public function savesearchlog() {
return '111111';
} }