🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
安装composer:非root启动 ~~~ [root@centos6 es]# cat /home/esu/.composer/config.json { "config": {}, "repositories": { "packagist": { "type": "composer", "url": "https://packagist.phpcomposer.com" } } } [root@centos6 es]# ~~~ composer安装es ~~~ [root@centos6 es]# cat composer.json { "require": { "elasticsearch/elasticsearch": "~6.0" } } [root@centos6 es]# ~~~ su esu ~~~ bash-4.1$ composer install --no-dev Loading composer repositories with package information Updating dependencies Package operations: 5 installs, 0 updates, 0 removals - Installing psr/log (1.0.2): Downloading (100%) - Installing react/promise (v2.5.1): Downloading (100%) - Installing guzzlehttp/streams (3.0.0): Downloading (100%) - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%) - Installing elasticsearch/elasticsearch (v6.0.1): Downloading (100%) Writing lock file Generating autoload files bash-4.1$ ~~~ ~~~ [root@centos6 es]# cat test.php <?php require_once('vendor/autoload.php'); use Elasticsearch\ClientBuilder; function create_index(){ //Elastic search php client $client = Elasticsearch\ClientBuilder::create()->build(); $mysql_conf = array( 'host' => '127.0.0.1:3306', 'db' => 'es', 'db_user' => 'root', 'db_pwd' => '000000', ); $mysqli = @new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']); if ($mysqli->connect_errno) { die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误 } $mysqli->query("set names 'utf8';");//编码转化 $select_db = $mysqli->select_db($mysql_conf['db']); if (!$select_db) { die("could not connect to the db:\n" . $mysqli->error); }$sql = "select * from emp"; $res = $mysqli->query($sql); if (!$res) { die("sql error:\n" . $mysqli->error); } while ($row = $res->fetch_assoc()) { $rtn[] = $row; } //delete index which already created $params = array(); $params['index'] = 'emp_index'; $client->indices()->delete($params); //create index on log_date,src_ip,dest_ip $rtnCount = count($rtn); for($i=0;$i<$rtnCount;$i++){ $params = array(); $params['body'] = array( 'id' => $rtn[$i]['id'], 'fdName' => $rtn[$i]['fdName'], 'fdAge' => $rtn[$i]['fdAge'], 'fdStatus' => $rtn[$i]['fdStatus'] ); $params['index'] = 'emp_index'; $params['type'] = 'emp_type'; //Document will be indexed to log_index/log_type/autogenerate_id $client->index($params); } echo 'create index done!'; } function search(){ //Elastic search php client $client = Elasticsearch\ClientBuilder::create()->build(); $params = array(); $params['index'] = 'emp_index'; $params['type'] = 'emp_type'; $params['body']['query']['match']['fdStatus'] = '1'; // $params['body']['sort'] = array('fdStatus'=>array('order'=>'asc')); $params['size'] = 3; $params['from'] = 1; $rtn = $client->search($params); var_dump($rtn); } set_time_limit(0); //create_index(); search(); ?> ~~~ **疑问:Fielddata is disabled on text field???**