Commit edf34efd authored by chenyuelong's avatar chenyuelong

add:elastic

parent b61fdca2
<?php
namespace App\Services\demo;
use Api\PhpUtils\Elastic\ElasticUtil;
class ElasticService
{
public function createIndex(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$data = [
'mappings' => [
'properties' => [
'test_field' => [
'type' => 'text',
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_smart"
],
]
]
];
$res = $elasticClient->createIndex($index, $data);
var_export($res);
}
public function deleteIndex(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$res = $elasticClient->deleteIndex($index);
var_export($res);
}
public function getSettings(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$res = $elasticClient->getSettings([$index]);
var_export($res);
}
public function putSettings(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$data = [
'settings' => [
'number_of_replicas' => 0,
'refresh_interval' => -1
]
];
$res = $elasticClient->putSettings($index, $data);
var_export($res);
}
public function getMapping(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$res = $elasticClient->getMapping($index);
var_export($res);
}
public function putMapping(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$data = [
'properties' => [
'second_field' => [
'type' => 'text',
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_smart"
],
]
];
$res = $elasticClient->putMapping($index, $data);
var_export($res);
}
public function indexDoc(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$data = ['test_field' => '美国留给伊拉克的是个烂摊子吗'];
$res = $elasticClient->indexDoc($index, $data, 'test_id');
var_export($res);
}
public function getDoc(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$res = $elasticClient->getDoc($index, 'test_id');
var_export($res);
}
public function updateDoc(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$data = [
'doc' => [
'new_field' => 'abc'
]
];
$res = $elasticClient->updateDoc($index, 'test_id', $data);
var_export($res);
}
public function deleteDoc(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$res = $elasticClient->deleteDoc($index, 'test_id');
var_export($res);
}
public function bulk(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$data = [
['index' => ['_index' => $index]],
['test_field' => '公安部:各地校车将享最高路权'],
['index' => ['_index' => $index]],
['test_field' => '中韩渔警冲突调查:韩警平均每天扣1艘中国渔船'],
['index' => ['_index' => $index]],
['test_field' => '中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首'],
];
$res = $elasticClient->bulkIndexDoc($data);
var_export($res);
}
public function searchDoc(): void
{
$elasticClient = ElasticUtil::getInstance();
$index = 'demo';
$data = [
'query' => [
'match' => [
'test_field' => '中国'
]
],
'highlight' => [
'pre_tags' => ['<tag1>', '<tag2>'],
'post_tags' => ['</tag1>', '</tag2>'],
"fields" => [
'test_field' => new \stdClass()
]
]
];
$res = $elasticClient->searchDoc($index, $data);
var_export($res);
}
}
\ No newline at end of file
<?php
ini_set("display_errors", "On");//打开错误提示
ini_set("error_reporting", E_ALL);//显示所有错误
/*
* cli入口脚本
* cli 配置文件:conf/cli.ini
* cli bootstrap:application/BootstrapCli.php ( 在cli.ini中配置
* 默认模块:modules/cli
* 脚本位置:modules/cli/controllers/xxx.php
* 调用方式:php cli.php controller action "a=1&b=2"
* 测试脚本:php cli.php test index "a=1&b=2"
*/
if (!substr(php_sapi_name(), 0, 3) == 'cli') {
die;
}
define('APPLICATION_PATH', realpath(__DIR__ . '/../'));
require APPLICATION_PATH . '/vendor/autoload.php';
$application = new Yaf\Application(APPLICATION_PATH . "/conf/cli.ini");
$es = new \App\Services\demo\ElasticService();
//$es->createIndex();
//$es->getSettings();
//$es->putSettings();
//$es->getSettings();
//$es->getMapping();
//$es->putMapping();
//$es->getMapping();
//$es->indexDoc();
//$es->bulk();
//$es->searchDoc();
//$es->updateDoc();
//$es->getDoc();
//$es->deleteDoc();
//$es->deleteIndex();
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment