接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码

Posted yszr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码相关的知识,希望对你有一定的参考价值。

-- ----------------------------
-- 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 appindexcontroller;
use thinkDb;
use thinkConfig;
//引入es搜索类
//require ‘./vendor/autoload.php‘;
use ElasticsearchClientBuilder;

use thinkController;
use thinkRequest;

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/[email protected]/dist/css/bootstrap.min.css">
                <!-- 最新的 Bootstrap 核心 javascript 文件 -->
                <script src="https://cdn.jsdelivr.net/npm/[email protected]/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‘;
      }
    



}

 



















以上是关于接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码的主要内容,如果未能解决你的问题,请参考以下文章

接上篇--.NET高级技术之正则表达式

你写的什么垃圾代码让Vsync命令不能及时处理呢?

Android:如何通过c++来调用java程序:接上篇

Cython 扩展模块类方法不可见/命名 提及时出错

接上篇 checkbox数据的带入

Discord.js 机器人在提及时做出响应