zoukankan      html  css  js  c++  java
  • php调用elasticsearch7.x

    2020年10月7日01:00:17

    laravel8 php7.3.4 mysql 5.7

    centos8安装elasticsearch,出门左转找一下

    安装扩展

    composer require elasticsearch/elasticsearch

    elasticsearch的中文开发文档

    https://learnku.com/docs/elasticsearch-php/6.0

    官方php客户端文档

    https://www.elastic.co/guide/cn/elasticsearch/php/current/_index_management_operations.html

    Elasticsearch 权威指南

    http://blog.didispace.com/books/elasticsearch-definitive-guide-cn/

    注意:这里的只是类比,不是完全一样

    你需要线上使用,请注意,深入理解elasticsearch的接口,因为php也是封装之后的使用,所以会有出入

    $params = [
        'index' => 'my_index',  //索引名(相当于mysql的数据库)
        'body' => [
            'settings' => [
                'number_of_shards' => 5,  #分片数
            ],
            'mappings' => [ 
                'my_type' => [ //类型名(相当于mysql的表)
                    '_all' => [
                        'enabled' => 'false'
                    ],
                    '_routing' => [
                        'required' => 'true'
                    ],
                    'properties' => [ //文档类型设置(相当于mysql的数据类型)
                        'name' => [
                            'type' => 'string',
                            'store' => 'true'
                        ],
                        'age' => [
                            'type' => 'integer'
                        ]
                    ]
                ]
            ]
        ]
    ];

    实例demo

    测试数据

    https://github.com/chinese-poetry/chinese-poetry

    使用的是

    chinese-poetryjsonpoet.song.0.json

    写入数据库

    CREATE TABLE `poet` (
     `id` bigint(20) NOT NULL AUTO_INCREMENT,
     `author` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
     `paragraphs` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
     `title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
     PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=290 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

    写入的是时候会出现繁体问题

    使用https://github.com/uutool/hanzi-convert

    //简体转繁体:
    $str = "好难";
    HanziConvert::convert($str,true);
     
    //繁体转简体
    $str = "好難";
    HanziConvert::convert($str);

    虽然支持的不是很全,但是写入个100-200条足够测试了,或者可以吧你项目新闻类,或者产品类的数据库作为测试数据

     $data1 = file_get_contents('./poet.song.0.json');
            $dd = json_decode($data1, true);
            if (!empty($dd)) {
                foreach ($dd as $k => $v) {
    
                    $Poet = new Poet();
                    $Poet->author = HanziConvert::convert($v['author']);
                    $Poet->paragraphs = HanziConvert::convert(implode(',', $v['paragraphs']));
                    $Poet->title = HanziConvert::convert($v['title']);
                    $Poet->save();
                }
            }
            die;
    
            $hosts = [
                '192.168.3.15:9200', // IP + Port
            ];
            $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
                    ->setHosts($hosts)      // Set the hosts
                    ->build();              // Build the client object
    
            $Poet = Poet::get()->toArray();
            foreach ($Poet as $k => $v) {
                $params = [
                    'index' => 'zx_index',
                    'type' => 'zx_type',
                    'id' => 'zx_' . $v['id'],
                    'body' => [
                        'id' => $v['id'],
                        'author' => $v['author'],
                        'paragraphs' => $v['paragraphs'],
                        'title' => $v['title'],
                    ],
                ];
                $response = $client->index($params);
            }
    
            $serparams = [
                'index' => 'zx_index',
                'type' => 'zx_type',
            ];
    
            $serparams['body']['query']['match']['paragraphs'] = '未离';
    //        $serparams['body']['query']['match']['author'] = '宋太祖';
            $resech = $client->search($serparams);
    
            print_r($resech);


    结果

    Array
    (
        [took] => 6
        [timed_out] => 
        [_shards] => Array
            (
                [total] => 1
                [successful] => 1
                [skipped] => 0
                [failed] => 0
            )
    
        [hits] => Array
            (
                [total] => Array
                    (
                        [value] => 23
                        [relation] => eq
                    )
    
                [max_score] => 9.34944
                [hits] => Array
                    (
                        [0] => Array
                            (
                                [_index] => zx_index
                                [_type] => zx_type
                                [_id] => zx_2
                                [_score] => 9.34944
                                [_source] => Array
                                    (
                                        [id] => 2
                                        [author] => 宋太祖
                                        [paragraphs] => 未离海底千山黑,纔到天中万国明。
                                        [title] => 句
                                    )
    
                            )
    
                        [1] => Array
                            (
                                [_index] => zx_index
                                [_type] => zx_type
                                [_id] => zx_45
                                [_score] => 4.187741
                                [_source] => Array
                                    (
                                        [id] => 45
                                        [author] => 陈抟
                                        [paragraphs] => 常人无所重,惟睡乃为重。,举世皆为息,魂离神不动。,觉来无所知,贪求心愈用。,堪笑尘中人,不知梦是梦。
                                        [title] => 赠金励睡诗  其一
                                    )
    
                            )
    
                        [2] => Array
                            (
                                [_index] => zx_index
                                [_type] => zx_type
                                [_id] => zx_190
                                [_score] => 3.757749
                                [_source] => Array
                                    (
                                        [id] => 190
                                        [author] => 孟宾于
                                        [paragraphs] => 仙岛却回空说梦,清朝未达自嫌身。
                                        [title] => 句  其四
                                    )
    
                            )
    
                        [3] => Array
                            (
                                [_index] => zx_index
                                [_type] => zx_type
                                [_id] => zx_76
                                [_score] => 3.6272902
                                [_source] => Array
                                    (
                                        [id] => 76
                                        [author] => 陶谷
                                        [paragraphs] => 是箇碑文念得全,聪明灵性自天然。,离吴别楚三千里,入洛游梁二十年。,负艺已闻喧世界,高眠长见卧云烟。,相逢与我情何厚,问佛方知宿有缘。
                                        [title] => 寄赠梦英大师
                                    )
    
                            )
    
                        [4] => Array
                            (
                                [_index] => zx_index
                                [_type] => zx_type
                                [_id] => zx_142
                                [_score] => 3.6272902
                                [_source] => Array
                                    (
                                        [id] => 142
                                        [author] => 释延寿
                                        [paragraphs] => 散诞疏狂得自然,免教拘迫事相牵。,潜龙不离滔滔水,孤鹤唯宜远远天。,透室寒光松槛月,逼人凉气石渠泉。,非吾独了西来意,竹祖桐孙尽入玄。
                                        [title] => 山居诗  其六一
                                    )
    
                            )
    
            )
    
    )
  • 相关阅读:
    shell中逻辑与的两种表示方法
    Git学习之Git恢复进度
    RH318之域控服务器
    《征服C指针》读书笔记
    2013年:一个技术领导的启程
    sqlite的一个Unable to Open database file的坑爹错误
    我的2013——青春的躁动
    C/C++注册动态对象到Lu系统并进行运算符重载
    Geeks面试题:Min Cost Path
    Leetcode Gray Code
  • 原文地址:https://www.cnblogs.com/zx-admin/p/13776105.html
Copyright © 2011-2022 走看看