elasticsearc进行全文索引高亮显示

摘要:
--导入组件库--˃em{color:red;}请搜索constApp={data(){return{title:“”,data:“”},方法:{blur(){axios.post.then},}constpp=Vue。createApp;应用程序。使用应用程序。mount控制器代码˂?

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

 

免责声明:文章转载自《elasticsearc进行全文索引高亮显示》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇flask中使用ajax 处理前端POST请求 弹框展示git代码合并与冲突下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

BN层

论文名字:Batch Normalization: Accelerating Deep Network Training by  Reducing Internal Covariate Shift 论文地址:https://arxiv.org/abs/1502.03167 BN被广泛应用于深度学习的各个地方,由于在实习过程中需要修改网络,修改的网络在训练过...

CocoaPods的安装及使用

一、前言   2016年4月15日,为什么我会记得这个日子呢,因为这一天,是我接触iOS开始的一天。也是因为项目的需要,就投入到iOS的开发中了,在接到说要我去开发iOS,当时的心情既激动也忐忑,激动是觉得自己除了Android,又有iOS可以去学习了,可以开拓思维,也就多了一门技术。忐忑是因为说以前都没接触过,不知道能不能做好。带着这样的心情,开始了我的...

openshift 入门 部署 openshift-origin-server-v3.7.0

OpenShift是一个基于容器技术的云平台,这里的容器技术指的就是docker和kubernetes. Openshift 错误解决 错误信息 failed to run Kubelet: failed to create kubelet: misconfiguration: kubelet cgroup driver: "systemd" is dif...

如何调试 Android 上 HTTP(S) 流量

转自: http://greenrobot.me/devpost/how-to-debug-http-and-https-traffic-on-android/ 如何调试 Android 上 HTTP(S) 流量 前面的话 在Android开发中我们常常会和API 打交道,可能你不想,但是这是避不开的。大部分情况下,调试发送网络请求和接收响应的过程都是...

ipadmini从9.3.5降级8.4.1并完美越狱

ipadmini之前是iOS9.3.5实在是卡的用不了,于是打算降级,但是尝试了包括改版本描述等很多方法一直失败。今天突然成功降级8.4.1并且完美越狱,运行流畅了非常多。赶紧发个教程,回馈一下网友。 1、首先是越狱。方法如下: ipad上网址:https://lmapp.cc/yueyu/ ,安装火凤凰在线越狱,设置->通用里选择信任软件。安装成...

企业如何部署开源邮箱系统?

今日我们来一些实际的、靠谱的部署。如何让自己有一个属于自己的邮箱系统。 我们说一下软件选择: 方案1:底层收发用postfix+ dovecot ;页面用roundmil;数据库可以用mysql(或者跟我一样用本地操作系统用户) 方案2:直接用iredmail吧,下载一个软件即可。执行程序,根据提示一步步进行。 下面我们简单介绍一下方案2,对用户最简单的方...