1. 程式人生 > >PHP 中運用 elasticsearch

PHP 中運用 elasticsearch

環境要求 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?
  1. include(‘./vendor/autoload.php‘);
  2. $elastic = new Elasticsearch\Client();
  3. $index[‘index’] = ‘log’; //索引名稱
  4. $index[‘type’] = ‘ems_run_log’; //類型名稱
  5. $data[‘body’][‘settings’][‘number_of_shards’] = 5; //主分片數量
  6. $data[‘body’][‘settings’][‘number_of_replicas’] = 0; //從分片數量
  7. $elastic->indices()->create($index);

插入索引數據

[php] view plain copy print?
  1. include(‘./vendor/autoload.php‘);
  2. $elastic = new Elasticsearch\Client();
  3. $index[‘index’] = ‘log’; //索引名稱
  4. $index[‘type’] = ‘ems_run_log’; //類型名稱
  5. $index[‘id’] = 1 //不指定id,系統會自動生成唯一id
  6. $index[‘body’] = array(
  7. ‘mac’ => ‘fcd5d900beca‘,
  8. ‘customer_id’ => 3,
  9. ‘product_id’ => 5,
  10. ‘version’ => 2
  11. );
  12. $elastic->index($index);

查詢

[php] view plain copy print?
  1. include(‘./vendor/autoload.php‘);
  2. $elastic = new Elasticsearch\Client();
  3. $index[‘index’] = ‘log’; //索引名稱
  4. $index[‘type’] = ‘ems_run_log’; //類型名稱
  5. $index[‘body’][‘query’][‘match’][‘mac’] = ‘fcd5d900beca’;
  6. $index[‘size’] = 10;
  7. $index[‘from’] = 200;
  8. $elastic->search($index);
  9. 相當於sql語句:
  10. select*from ems_run_log where mac=‘fcd5d900beca’
  11. limit 200,10;<strong>
  12. </strong>

[php] view plain copy print?
  1. include(‘./vendor/autoload.php‘);
  2. $elastic = new Elasticsearch\Client();
  3. $index[‘index’] = ‘log’; //索引名稱
  4. $index[‘type’] = ‘ems_run_log’; //類型名稱
  5. $index[‘body’][‘query’][‘bool’][‘must’] = array(
  6. array(‘match’ => array(‘mac’ => ‘fcd5d900beca’)),
  7. array(‘match’ => array(‘product_id’ => 20))
  8. );
  9. $index[‘size’] = 10;
  10. $index[‘from’] = 200;
  11. $elastic->search($index);
  12. 相當於sql語句:
  13. select*from ems_run_log where mac=‘fcd5d900beca’
  14. and product_id = 20 limit 200,10;

[php] view plain copy print?
  1. include(‘./vendor/autoload.php‘);
  2. $elastic = new Elasticsearch\Client();
  3. $index[‘index’] = ‘log’; //索引名稱
  4. $index[‘type’] = ‘ems_run_log’; //類型名稱
  5. $index[‘body’][‘query’][‘bool’][‘should’] = array(
  6. array(‘match’ => array(‘mac’ => ‘fcd5d900beca’)),
  7. array(‘match’ => array(‘product_id’ => 20))
  8. );
  9. $index[‘size’] = 10;
  10. $index[‘from’] = 200;
  11. $elastic->search($index);
  12. 當於sql語句:
  13. select*from ems_run_log where mac=‘fcd5d900beca’
  14. or product_id = 20 limit 200,10;

[php] view plain copy print?
  1. include(‘./vendor/autoload.php‘);
  2. $elastic = new Elasticsearch\Client();
  3. $index[‘index’] = ‘log’; //索引名稱
  4. $index[‘type’] = ‘ems_run_log’; //類型名稱
  5. $index[‘body’][‘query’][‘bool’][‘must_not’] = array(
  6. array(‘match’ => array(‘mac’ => ‘fcd5d900beca’)),
  7. array(‘match’ => array(‘product_id’ => 20))
  8. );
  9. $index[‘size’] = 10;
  10. $index[‘from’] = 200;
  11. $elastic->search($index);
  12. 相當於sql語句:
  13. select*from ems_run_log where mac!=‘fcd5d900beca’
  14. and product_id != 20 limit 200,10;

[php] view plain copy print?
  1. include(‘./vendor/autoload.php‘);
  2. $elastic = new Elasticsearch\Client();
  3. $index[‘index’] = ‘log’; //索引名稱
  4. $index[‘type’] = ‘ems_run_log’; //類型名稱
  5. $index[‘body’][‘query’][‘range’] = array(
  6. ‘id’ => array(‘gte’ => 20,’lt’ => 30);
  7. );
  8. $index[‘size’] = 10;
  9. $index[‘from’] = 200;
  10. $elastic->search($index);
  11. 相當於sql語句:
  12. select*from ems_run_log where id>=20
  13. and id<30 limit 200,10;

刪除文檔

[php] view plain copy print?
    1. <pre name="code" class="php">include(‘./vendor/autoload.php‘);
    2. $elastic = new Elasticsearch\Client();
    3. $index[‘index‘] = ‘test‘; //索引名稱
    4. $index[‘type‘] = ‘ems_test‘; //類型名稱
    5. $index[‘id‘] = 2;
    6. $elastic->delete($index);

PHP 中運用 elasticsearch