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 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]);
    }

}






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

 

 

posted @ 2021-03-15 22:02  Conqueror·  阅读(43)  评论(0编辑  收藏  举报