以上是html部分,下面是php部分
public function getsearch(){//这个方法是表单点击搜索时action提交的方法 $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 .= ' '; $str .= '
id | title | content |
'; foreach ($list as $k => $v) { $str .= ' ' . $v['_source']['id'] . ' | ' . $v['_source']['title'] . ' | ' . $v['_source']['content'] . ' |
'; } $str .='
'; return $str; } public function search() {//这部分方法是ajax 在搜索框输入文字时进行提示的方法 /*$client = ClientBuilder::create()->setHosts($hosts)->build();*/ //实例化es类;在项目中引入自动加载文件,并且实例化一个客户端: $client = ClientBuilder::create()->build(); $keys = Request::instance()->param('keyword');//tp5方法,获取get post数据自动辨别 $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;}
最终效果