tp6+ElasticSearch+Kibana+高亮 多条件搜索接口

    /**
     *  添加索引
     * @return string
     */
    public static function createDatabase()
    {
        //本地调用实例化
        $client  = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        //创建索引
        $params = [
            'index' => 'goods',//数据库名
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,//创建后可以更改
                    'number_of_replicas' => 1//创建后不可以更改
                ],
                'mappings' => [
                    '_doc' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties' => [
                            //搜索的字段名
                            'name' => [
                                'type' => 'text',
                                'analyzer' => 'ik_max_word',//ik分词器
                                'search_analyzer' => 'ik_max_word'
                            ],  //搜索的字段名
                            'type_name' => [
                                'type' => 'text',
                                'analyzer' => 'ik_max_word',//ik分词器
                                'search_analyzer' => 'ik_max_word'
                            ]
                        ]
                    ]
                ]
            ]
        ];

        //将索引添加到kibana中
        $response = $client->indices()->create($params);

        //判断结果
        if($response){

            //执行成功
            return "索引添加成功";

        }else{

            //执行失败
            return "索引添加失败";
        }
    }

    //同步数据
    public static function createData()
    {
      //本地调用实例化
        $hosts = [
            '127.0.0.1:9200'
        ];
        //请求连接
        $client = ClientBuilder::create()->setHosts($hosts)->build();
        //查询数据表所有的数据并转化数组格式
//        $date = Goods::all()->toArray();
        $date = \app\admin\model\Goods::with('type')->select()->toArray();
        //循环
        foreach ($date as $v)
        {
            $data = [
                'index' => 'goods',//更改索引名(数据库名) 其余不变
                'type' => '_doc',
                'id' => $v['id'],
                'body' => $v,
            ];
            $response = $client->index($data);
        }
        //判断 是否添加数据成功
        if($response){

            return "添加数据成功";
        }else{

            return "添加数据失败";
        }
    }

 

//高亮搜索
    public static function read(Request $request)
    {
//接值
        $word = $request->get('title');
        if (!$word) {
            return ['code' => 500, 'msg' => '未检测到值', 'data' => ''];
        }
        //请求连接
        $client = ClientBuilder::create()->build();
        //搜索的值和库中的值进行对比
        $params = [
            'index' => 'goods',//数据库名
            'type' => '_doc',
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [
                                'match' => [
                                    //要搜索的字段↓
                                    'name' => $word,//要搜索的内容
                                ]
                            ],
                            [
                                'match' => [
                                    //要搜索的字段↓
                                    'type_name' => $word,//要搜索的内容
                                ]
                            ]
                        ]
                    ]
                ],
                'highlight' => [
                    'pre_tags' => ["<em style='color: #ff0000'>"],//样式
                    'post_tags' => ["</em>"],
                    'fields' => [
                        //要搜索的字段↓
                        "name" => new \stdClass(),
                        "type_name" => new \stdClass()
                    ]
                ]
            ],
        ];

        //执行搜索
        $results = $client->search($params);
//        print_r($results);die();
        //循环取出值
        foreach ($results['hits']['hits'] as &$v) {
            if (isset($v['highlight']['name'])) {
                $v['_source']['name'] = $v['highlight']['name'];
//                return 1;
            } elseif (isset($v['highlight']['type_name'])) {
                $v['_source']['type_name'] = $v['highlight']['type_name'];
//                return 2;
            }else{
                return "没数据";
            }
        }
        //替换
        $response = array_column($results['hits']['hits'], '_source');
        print_r($response);
        die();
        //返回
        return ['code' => 200, 'msg' => '请求成功', 'data' => $response];
    }

 

posted @ 2022-08-13 09:05  清淤  阅读(181)  评论(0)    收藏  举报