使用elasticsearch实现全文检索以及增删改查(二)

三.框架中的使用

简单的使用
前端vue


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Documenttitle>
    <script src="https://unpkg.com/vue@next">script>
    
    <link rel="stylesheet" href="https://unpkg.com/element-plus/lib/theme-chalk/index.css">
    
    <script src="https://unpkg.com/element-plus/lib/index.full.js">script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js">script>
    
    <script src="https://unpkg.com/axios/dist/axios.min.js">script>
    <style>
        em {
            color: red;
        }
    style>
head>
<body>
<div id="app">
    <input type="text" v-model="title" @blur="blur">请搜索
    <div v-for="item in data ">
        <div v-html="item.highlight.article[0]" >div>
    div>

div>

body>
<script>
    const App={
        data() {
            return {
                title:"",
                data:""
            }
        },
        methods:{
            blur(){
                axios.post('{:url("search")}', {
                    title:this.title
                })
                    .then(response=>(this.data=(response.data.msg.hits.hits)))
            }
        },
    }
    const app = Vue.createApp(App);
    app.use(ElementPlus);
    app.mount("#app")
script>
html>

控制器层代码



namespace app\admin\controller;

use Elasticsearch\ClientBuilder;
use NunoMaduro\Collision\Highlighter;
use think\Controller;
use think\Request;

class Esc extends Controller
{
    public $client;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        //连接
        $this->client=ClientBuilder::create()->setHosts(["127.0.0.1:9200"])->build();
    }

    //渲染视图
    public function show(){
       return view('index');
    }

    //添加索引
    public function createIndex(){
        $params = [
            'index' => 'mq_index',
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 1
                ],
                'mappings' => [
                    'properties' => [
                        'title' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word',
                            'search_analyzer' => 'ik_max_word'
                        ],
                        'article' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word',
                            'search_analyzer' => 'ik_max_word'
                        ]
                    ]
                ]
            ]
        ];
        $response = $this->client->indices()->create($params);
        print_r($response);
    }

// 添加数据
    public function createData(){
        $params = [
            'index' => 'mq_index',
            'body'  => ['title' => '今天不行',"article"=>"今天的天气确实不错"]
        ];
        $response = $this->client->index($params);
        print_r($response);
    }


//    搜索
    public function search(){
        $res=input("title");
        $params = [
            'index' => 'mq_index',
            'body'  => [
                'query' => [
                    'multi_match' => [
                        //搜索的内容
                        'query' => $res,
                        'fields' => ["title",'article']
                    ]
                ],
                'highlight' => [
                    'fields' => [
                        '*' => new Highlighter()
                    ]
                ]
            ]
        ];

        $response = $this->client->search($params);
        return json(["code"=>200,"msg"=>$response]);
    }

}



class Singleton{   
 private static $_instance;   
 private function __construct(){}   
 private function __clone(){}    
 public static function getInstance()    {  
        if(self::$_instance instanceof Singleton){//instanceof             self::$_instance = new Singleton();        }               return self::$_instance;   
         }
        }

你可能感兴趣的:(php,php)