PHP code example of crx / hyperf-php-elasticsearch

1. Go to this page and download the library: Download crx/hyperf-php-elasticsearch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crx / hyperf-php-elasticsearch example snippets


$host = [
    '链接'
]
$d = new \Crx\HyperfPhpElasticsearch\CrxElasticsearch($host);

$index = 'chat_data'
$d->createIndex($index);

$params = [
   'index' => 'chat_data',
   'body' => [
       '_source' => [
           'enabled' => true
       ],
       'properties' => [
           'item1' => [
               'type' => 'text',
           ],
           'item2' => [
               'type' => 'integer',
           ],
       ]
  ]
];
$d->putMapping($params);

$params = [
   'index' => 'chat_data',
];
$d->getMapping($params);

$index = 'index_exists_';
$d->indexExistsEs($index);

$index = 'index_exists_'
$d->deleteIndex($index);

$params = [
    'index' => '',
    'body' => []
];
$d->indexEs($params);

$data = [...]; // 数据集
foreach($data as $v) {
    $params['body'][] = [
        'index' => [
            '_index' => 'chat_data',
        ]
    ];
    $params['body'][] = $v;
}
$crxElasticsearch->bulk($params);

$params = [
   'index' => 'chat_data',
    'id' => '文档id',
    'doc' => [
       '字段名1' => '要修改的值',
       '字段名2' => '要修改的值',
       '字段名3' => '要修改的值',
    ]
]
$crxElasticsearch->update($params);

$search = [
    'item1' => '1',
    'item2' => '2',
    'item3' => '3',
];
$params['search'] = $search;
$crxElasticsearch->search($params);