PHP 中運用 elasticsearch
阿新 • • 發佈:2019-01-10
環境要求 last pri pop 令行 toc req fun rds
PHP擴展安裝
1. 環境要求:PHP_VERSION >= 5.3.9,composer工具
2. 在E盤新建文件夾命名為elastic,,拷貝composer.phar到
E:/elastic目錄下面
3. 打開命令行窗口,進入E:/elastic
4. 在命令行運行:
php composer.phar require elasticsearch/elasticsearch
5. 此時E:/elastic目錄下會出現一個vendor目錄,安裝成功
6. 使用方法:
require ‘vendor/autoload.php‘;
$client = new Elasticsearch\Client();
創建索引
[php] view plain copy print?
- include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index’] = ‘log’; //索引名稱
- $index[‘type’] = ‘ems_run_log’; //類型名稱
- $data[‘body’][‘settings’][‘number_of_shards’] = 5; //主分片數量
- $data[‘body’][‘settings’][‘number_of_replicas’] = 0; //從分片數量
- $elastic->indices()->create($index);
插入索引數據
[php] view plain copy print?
- include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index’] = ‘log’; //索引名稱
- $index[‘type’] = ‘ems_run_log’; //類型名稱
- $index[‘id’] = 1 //不指定id,系統會自動生成唯一id
- $index[‘body’] = array(
- ‘mac’ => ‘fcd5d900beca‘,
- ‘customer_id’ => 3,
- ‘product_id’ => 5,
- ‘version’ => 2
- );
- $elastic->index($index);
查詢
[php] view plain copy print?
- include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index’] = ‘log’; //索引名稱
- $index[‘type’] = ‘ems_run_log’; //類型名稱
- $index[‘body’][‘query’][‘match’][‘mac’] = ‘fcd5d900beca’;
- $index[‘size’] = 10;
- $index[‘from’] = 200;
- $elastic->search($index);
- 相當於sql語句:
- select*from ems_run_log where mac=‘fcd5d900beca’
- limit 200,10;<strong>
- </strong>
[php] view plain copy print?
- include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index’] = ‘log’; //索引名稱
- $index[‘type’] = ‘ems_run_log’; //類型名稱
- $index[‘body’][‘query’][‘bool’][‘must’] = array(
- array(‘match’ => array(‘mac’ => ‘fcd5d900beca’)),
- array(‘match’ => array(‘product_id’ => 20))
- );
- $index[‘size’] = 10;
- $index[‘from’] = 200;
- $elastic->search($index);
- 相當於sql語句:
- select*from ems_run_log where mac=‘fcd5d900beca’
- and product_id = 20 limit 200,10;
[php] view plain copy print?
- include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index’] = ‘log’; //索引名稱
- $index[‘type’] = ‘ems_run_log’; //類型名稱
- $index[‘body’][‘query’][‘bool’][‘should’] = array(
- array(‘match’ => array(‘mac’ => ‘fcd5d900beca’)),
- array(‘match’ => array(‘product_id’ => 20))
- );
- $index[‘size’] = 10;
- $index[‘from’] = 200;
- $elastic->search($index);
- 當於sql語句:
- select*from ems_run_log where mac=‘fcd5d900beca’
- or product_id = 20 limit 200,10;
[php] view plain copy print?
- include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index’] = ‘log’; //索引名稱
- $index[‘type’] = ‘ems_run_log’; //類型名稱
- $index[‘body’][‘query’][‘bool’][‘must_not’] = array(
- array(‘match’ => array(‘mac’ => ‘fcd5d900beca’)),
- array(‘match’ => array(‘product_id’ => 20))
- );
- $index[‘size’] = 10;
- $index[‘from’] = 200;
- $elastic->search($index);
- 相當於sql語句:
- select*from ems_run_log where mac!=‘fcd5d900beca’
- and product_id != 20 limit 200,10;
[php] view plain copy print?
- include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index’] = ‘log’; //索引名稱
- $index[‘type’] = ‘ems_run_log’; //類型名稱
- $index[‘body’][‘query’][‘range’] = array(
- ‘id’ => array(‘gte’ => 20,’lt’ => 30);
- );
- $index[‘size’] = 10;
- $index[‘from’] = 200;
- $elastic->search($index);
- 相當於sql語句:
- select*from ems_run_log where id>=20
- and id<30 limit 200,10;
刪除文檔
[php] view plain copy print?
- <pre name="code" class="php">include(‘./vendor/autoload.php‘);
- $elastic = new Elasticsearch\Client();
- $index[‘index‘] = ‘test‘; //索引名稱
- $index[‘type‘] = ‘ems_test‘; //類型名稱
- $index[‘id‘] = 2;
- $elastic->delete($index);
PHP 中運用 elasticsearch