码迷,mamicode.com
首页 > 其他好文 > 详细

elasticsearc进行全文索引高亮显示

时间:2021-03-17 14:12:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:java   space   return   今天   dmi   ppi   extend   esc   lib   

首先使用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]);
    }

}

 

elasticsearc进行全文索引高亮显示

标签:java   space   return   今天   dmi   ppi   extend   esc   lib   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!