栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

php操作elasticsearch

php操作elasticsearch

首先在composer.json 加上以下代码。

 "elasticsearch/elasticsearch": "~6.0"

使用composer 完成拓展的加载

composer update elasticsearch/elasticsearch

添加索引

public function createIndex()
    {
        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'new_home',
            //索引名
            'body' => [
                'settings' => [
                    //分片
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        //字段名称
                        'name' => [
                            //文字
                            'type' => 'text',
                            //ik分词器
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ],
                        'id' => [
                            //数值
                            'type' => 'integer'
                        ]
                    ]
                ]
            ]
        ];
        $response = $client->indices()->create($params);
        var_dump($response);
    }
elasticsearch添加数据
 public function addEs($id, $name)
    {
        $client = ClientBuilder::create()->build();
        $params = [
            //索引名
            'index' => 'new_home',
            'type' => '_doc',  
            //指定id
            'id' => $id,
            //内容
            'body' => [
                'name' => $name,
                'id' => $id
            ]
        ];
        $response = $client->index($params);
        print_r($response);
        echo "
"; }

elasticsearch搜索

public function searchEs(Request $request)
    {
        $name = $request['name'];
        if (empty($name)) {
            return showMsg(500, '查询条件不能为空');
        }
        $client = ClientBuilder::create()->build();
        $params = [
            //索引名
            'index' => 'new_home',
            'type' => '_doc',
            'body' => [
                'query' => [
                    'match' => [
                        //条件
                        'name' => $name
                    ]
                ],
                //高亮显示
                'highlight' => [
                    'pre_tags' => [""],
                    'post_tags' => [""],
                    'fields' => [
                        "name" => new stdClass()
                    ]
                ]
            ]
        ];
        //前台高亮显示
        
        $results = $client->search($params);
        return showMsg(200, '查询成功', $results['hits']['hits']);
    }

以下为全部代码

build();
        $params = [
            'index' => 'new_home',
            //索引名
            'body' => [
                'settings' => [
                    //分片
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        //字段名称
                        'name' => [
                            //文字
                            'type' => 'text',
                            //ik分词器
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ],
                        'id' => [
                            //数值
                            'type' => 'integer'
                        ]
                    ]
                ]
            ]
        ];
        $response = $client->indices()->create($params);
        var_dump($response);
    }

    
    public function addEs($id, $name)
    {
        $client = ClientBuilder::create()->build();
        $params = [
            //索引名
            'index' => 'new_home',
            'type' => '_doc',
            //指定id
            'id' => $id,
            //内容
            'body' => [
                'name' => $name,
                'id' => $id
            ]
        ];
        $response = $client->index($params);
        print_r($response);
        echo "
"; } public function mysql_es() { $date = AppModelsnewRoom::select('id', 'title')->get(); foreach ($date as $v) { $this->addEs($v['id'], $v['title']); } } public function searchEs(Request $request) { $name = $request['name']; if (empty($name)) { return showMsg(500, '查询条件不能为空'); } $client = ClientBuilder::create()->build(); $params = [ //索引名 'index' => 'new_home', 'type' => '_doc', 'body' => [ 'query' => [ 'match' => [ //条件 'name' => $name ] ], //高亮显示 'highlight' => [ 'pre_tags' => [""], 'post_tags' => [""], 'fields' => [ "name" => new stdClass() ] ] ] ]; //前台高亮显示 $results = $client->search($params); return showMsg(200, '查询成功', $results['hits']['hits']); } }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/748762.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号