Elasticsearch在thinkphp5中的使用增删改查(模糊查询、批量查询)

Elasticsearch在thinkphp5中的使用(模糊查询)

需要安装elasticsearch、elastic-header-master、kibana、analysis-ik四个包,如下图所示:(需要的包直接去github上搜索就可以,至于安装教程可以在csdn上搜,看好是按照在win还是linux上的,最主要的是按照会遇到很多问题,有钱首次安装会生成一个账号、密码、和code,记得保存在电脑上,首次启用kibaba时需要输入才可运行,记得安装好jdk>=1.8的版本,前提条件,所有安装的包都要对应相同的版本,否则在plugings中的ik重启就会失败)

Elasticsearch在thinkphp5中的使用增删改查(模糊查询、批量查询)_第1张图片
注意:千万别去官网下载最新的,因为最新的那个下载后没有对应的ik中文,配置的话,我以后再写。也可以看别人的,我下载的是7.17.2的包,大于8的可能或多或少的出现问题。只针对于thinkphp5的Elasticsearch的增删改查。
Elasticsearch在thinkphp5中的使用增删改查(模糊查询、批量查询)_第2张图片

一、创建索引

public function index()
    {
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        $params = [
            'index' => 'movies',//类似于库名
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'title' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ]
                    ]
                ]
            ]
        ];
        //执行创建
        $r = $es->indices()->create($params);
        dump($r);
    }

二、索引中新增数据

public function add_movies_data()
    {
        $data = Movies::select();//查询数据
        $res = (new Collection($data))->toArray();
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        foreach ($res as $k=>$v){
            $params = [
                'index' =>'movies',//索引
                'type'  =>'_doc',//表(额外需要注意的,这里是固定的写法)
                'id'    =>  $v['id'],//主键
                'body'     =>$v//数据
            ];
 
            $r = $es->index($params);
        }
        echo 'success';
 
    }

三、实现在Elasticsearch中数据的搜索(使得搜索的关键字高亮)

public function search_movies()
    {
        $word = input('word');//接收关键字
        $page = input('page',1);//接收当前页(如果没接收到,默认是1)
        $size = 5;//每页显示条数
        $limit = ($page-1)*$size;//偏移量
        $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();//创建es实例
        //设置查询的条件
        $params = [
            'index' => 'movies',//索引(类似于库)
            //'type' => '_doc',
            'body' => [
                //查询内容
                'query' => [
                    'match' => [//匹配
                        'title' => $word//匹配字段
                    ]
                ],
                'highlight' => [//高亮
                    'pre_tags' => [""],//样式自己写
                    'post_tags' => [""],
                    'fields' => [
                        "title" => new \stdClass()
                    ]
                ]
            ]
        ];
        //分页限制
        $params["size"] = $size;//每页显示条数
        $params["from"] = $limit;//偏移量
        $results = $client->search($params);//es搜索
        foreach ($results['hits']['hits'] as $k=>$v){
            $results['hits']['hits'][$k]['_source']['title'] = $v['highlight']['title'][0];
        }
 
        $data = array_column($results['hits']['hits'],'_source');
 
        $arr['data'] = $data;//数据
        $arr['page'] = $page;//当前页
        $arr['total'] = $results['hits']['total']['value'];//总条数
        $arr['last_page'] = ceil($results['hits']['total']['value']/$size);//总页数
 
        print_r($arr);//剩下的就是前端展示的事情了
        
    }

前端页面展示:
Elasticsearch在thinkphp5中的使用增删改查(模糊查询、批量查询)_第3张图片

elasticsearch索引展示:
Elasticsearch在thinkphp5中的使用增删改查(模糊查询、批量查询)_第4张图片
我将代码复制到此处:
Controller中的代码:

 public function first()
    {
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        $params = [
            'index' => 'movies01',//类似于库名
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'title' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ],
                        'goods_name' => [//goods_name 文档名称 相当于mysql中的字段
                            'type' => 'text',//text文本
                            'analyzer' => 'ik_max_word',//analyzer 指定分词器为ik
                            'search_analyzer'=>'ik_max_word',//指定搜索时的分词器
                        ],
                        'goods_logo' => [
                            'type' => 'text'
                        ],
                        'goods_price' => [
                            'type' => 'text',//text文本
                        ],
                        'goods_desc' => [
                            'type' => 'text',//text文本
                            'analyzer' => 'ik_max_word',//analyzer 指定分词器为ik
                            'search_analyzer'=>'ik_max_word'//指定搜索时的分词器
                        ],
                        'goods_remark' => [
                            'type' => 'text',//text文本
                            'analyzer' => 'ik_max_word',//analyzer 指定分词器为ik
                            'search_analyzer'=>'ik_max_word'//指定搜索时的分词器
                        ],
                        "news_source"=>[
                            "type"=>"text",
                            "fielddata"=>true]
                    ]
                ]
            ]
        ];
        //执行创建
        $r = $es->indices()->create($params);
        dump($r);
    }


    public function add_movies_data()
    {
        $data = Goods::select();//查询数据
        $res = (new Collection($data))->toArray();
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        foreach ($res as $k=>$v){
            $params = [
                'index' =>'movies01',//索引
                'type'  =>'_doc',//表(额外需要注意的,这里是固定的写法)
                'id'    =>  $v['id'],//主键
                'body'     =>$v//数据
            ];
            $r = $es->index($params);
        }
        echo 'success';

    }
    public function search_movies($word)
    {
        header("Access-Control-Allow-Origin:*");

        header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");

        header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");

//        $word = input('word');//接收关键字
        $page = input('page',1);//接收当前页(如果没接收到,默认是1)
        $size = 5;//每页显示条数
        $limit = ($page-1)*$size;//偏移量
        $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();//创建es实例
        //设置查询的条件
        $params = [
            'index' => 'movies01',//索引(类似于库)
            //'type' => '_doc',
            'body' => [
                //查询内容
                'query' => [
                    'match' => [//匹配
                        'title' => $word//匹配字段
                    ]
                ],
                'highlight' => [//高亮
                    'pre_tags' => [""],//样式自己写
                    'post_tags' => [""],
                    'fields' => [
                        "title" => new \stdClass()
                    ]
                ]
            ]
        ];
        //分页限制
        $params["size"] = $size;//每页显示条数
        $params["from"] = $limit;//偏移量
        $results = $client->search($params)->asArray();//es搜索
        foreach ($results['hits']['hits'] as $k=>$v){
            $results['hits']['hits'][$k]['_source']['title'] = $v['highlight']['title'][0];
        }

        $data = array_column($results['hits']['hits'],'_source');
        $arr['data'] = $data;//数据
        $arr['page'] = $page;//当前页
        $arr['total'] = $results['hits']['total']['value'];//总条数
        $arr['last_page'] = ceil($results['hits']['total']['value']/$size);//总页数
//        print_r($arr);//剩下的就是前端展示的事情了
        return json($arr);
//        return view('index/index',compact('arr',$arr));
    }

数据库数据展示:

Elasticsearch在thinkphp5中的使用增删改查(模糊查询、批量查询)_第5张图片
前端代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>xx信息查询</title>
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<form>
    <table id="table-search" width="80%" align="center" border="1">
        <tr>
            <td>输入关键字</td>
            <td><input type="text" maxlength="50" name="word" id="word"></td>
        </tr>
            <td>
                <input type="button" value="搜索" id="btSearch" class="ui right floated positive button btn-search"/>
            </td>
        </tr>
    </table>
</form>
<br/>
<table id="table-result" width="80%" align="center" border="1">
    <tr>
        <th>货物名称</th>
        <th>货物图标</th>
        <th>货物价格</th>
        <th>货物备注</th>
        <th>分类</th>
    </tr>
    <tbody id="tbody-result">
    </tbody>
</table>
</body>

<script>
    $(function () {
        $('#btSearch').click(function () {
            var word = $('#word').val();
            var tbody = window.document.getElementById("tbody-result");
            $.ajax({
                type: "post",
                dataType: "json",
                url: "http://www.el.com/index.php/index/index/search_movies?word="+word,
                success: function (msg) {
                    console.log(msg);
                    if (msg) {
                        var str = "";
                        var data = msg.data;
                        for (i in data) {
                            str += "" +
                                "" + data[i].goods_name + "" +
                                "" + data[i].goods_logo + "" +
                                "" + data[i].goods_price + "" +
                                "" + data[i].goods_remark + "" +
                                "" + data[i].title + "" +
                                "";
                        }
                        document.getElementById('tbody-result').innerHTML = str;
                    }
                },
                error: function () {
                    alert("查询失败")
                }
            });
        });
    });
</script>
</html>

你可能感兴趣的:(thinkphp5,elasticsearch,后台接口,php,tp,thinkphp5,elasticsearch,elsatic)