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-poetry\json\poet.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] => 山居詩  其六一
                                )

                        )

        )

)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM