• elasticsearc进行全文索引高亮显示


    首先使用composer安装扩展

    composer require elasticsearch/elasticsearch
    composer require nunomaduro/collision

    开启你的 elasticsearch  与 kibana

    
    
    

    HTML代码  使用vue进行数据渲染

    <!doctype html>
    <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>Document</title>
        <script src="https://unpkg.com/vue@next"></script>
        <!-- import CSS -->
        <link rel="stylesheet" href="https://unpkg.com/element-plus/lib/theme-chalk/index.css">
        <!-- import JavaScript -->
        <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>

    控制器代码

    <?php
    
    namespace appadmincontroller;
    
    use ElasticsearchClientBuilder;
    use NunoMaduroCollisionHighlighter;
    use thinkController;
    use thinkRequest;
    
    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]);
        }
    
    }






    <?php
    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;
    }
    }

     
  • 相关阅读:
    最后之作-Last Order(bushi
    Hydra's plan
    CSP-S2019游记
    题解 P1407
    初赛排序知识
    题解 洛谷P3469
    二分图网络流知识总结
    题解 SGU294 He's Circles
    noip复习——逆元
    noip复习——线性筛(欧拉筛)
  • 原文地址:https://www.cnblogs.com/cyxng/p/14540269.html
Copyright © 2020-2023  润新知