全文检索 高亮显示 数据库同步添加

<?php

namespace appadmincontroller;

use appadminmodelEsones;
use appcommonlibES;
use thinkController;
use thinkRequest;

class Esone extends Controller
{
    /**
     * 显示资源列表
     *
     * @return 	hinkResponse
     */
    //创建索引
    public function index()
    {
        //视图渲染
        return view('index');



    }

       //创建一个新的索引
    public function es(){
        $es=new ES('esl');
    }


    //添加数据
    public function addData(Request $request){
//        print_r($request->param());
        //添加数据到es
        $data=input();
        //添加数据
//        print_r($data);
        $data=Esones::create($data,true)->toArray();
//        print_r($data['name']);
        $es = new ES('es');
        $params = [
            'index' => "esl",
            'id' => $data['id'],
            'type' => "article",
            "body" => [
                'id'=>$data['id'],
                "name" => $data['name'],
                'age' => $data['age']
            ]
        ];
       $a= $es->add_doc($params);
       //重定向
       return redirect(url('lists'));



    }

    //搜索所有的数据
    public function lists(){
        //获取数据展示  全部数据 从es中获取
        $es = new ES('es');
        $data=$es->get_all('esl','article');
        //取值
        $data  = array_column($data,"hits");
        $data  = array_column($data[0],"_source");
//        print_r($data);

        return view('list',compact('data'));


    }


    //全文检索
    public function select(Request $request){
        $where=$request->param('name');
        $es = new ES('es');
        $index_name = "esl";
        $type_name = "article";
        $body = [
            'query' => [
                'bool' => [
                    'should' => [
                        [
                            'match' => [
//                                搜索的字段名
                                'name' => [
                                    //搜索的关键字
                                    'query' => $where,
                                    'boost' => 4, // 权重大
                                ]
                            ],

                        ],
                        [
                            'match' => [
                                'age' => [
                                    'query' => $where,
                                    'boost' => 3, // 权重大
                                ]
                            ],

                        ],

                    ],
                ]

            ]];
        $response= $es->search_doc($index_name, $type_name, $body);
        $data  = array_column($response,"hits");
        $data  = array_column($data[0],"_source");
        foreach ($data as $key => &$val){
            $val['name']=str_replace($where,"<span style='color: red'>$where</span>",$val['name']);
        }
        //可以将数据发送到视图
        return view('list',compact('data'));



    }



}

添加页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" action="{:url('addData')}">
    姓名:<input type="text" name="name">
    年龄:<input type="text" name="age">
    <input type="submit" value="提交">
</form>

</body>
</html>

列表展示

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap 实例 - 基本的表格</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form action="{:url('select')}" method="post">

    <input type="text" name="name">
    <input type="submit" value="搜索">
    <button><a href="{:url('lists')}">重置</a></button>
</form>


<table class="table">
    <caption>基本的表格布局</caption>
    <thead>
    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
    </thead>
    <tbody>
    {foreach $data as $v}
    <tr>

        <td>{$v['id']}</td>
        <td>{$v['name']}</td>
        <td>{$v['age']}</td>
        {/foreach}
    </tr>

    </tbody>
</table>

</body>
</html>

vue的使用方法

原文地址:https://www.cnblogs.com/cyxng/p/14657059.html