php运用elasticsearch
来源:互联网 发布:好看的古装电影知乎 编辑:程序博客网 时间:2024/05/17 11:06
PHP 中运用 elasticsearch
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();
创建索引
- 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);
插入索引数据
- 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);
查询
- 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>
- 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;
- 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;
- 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;
- 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;
删除文档
- <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);
IT成长中的那些事儿
阅读全文
0 0
- PHP 中运用 elasticsearch
- php运用elasticsearch
- php中运用elasticsearch(composer)
- PHP ElasticSearch的使用
- Elasticsearch-PHP 概述
- Elasticsearch-PHP 快速开始
- Elasticsearch-PHP 安装
- Elasticsearch-PHP 配置
- Elasticsearch-PHP 索引操作
- Elasticsearch-PHP 索引操作
- Elasticsearch-PHP 搜索操作
- Elasticsearch-PHP 命名空间
- elasticsearch--php检索
- PHP ElasticSearch的使用
- PHP分页+Elasticsearch查询
- PHP mail运用
- php运用daodb分页
- PHP中的MVC运用
- Mysql 存储过程之函数-如判断两个以逗号分隔字符串的包含关系
- ch0 CBsp类的实现
- Java的基本了解
- 云计算的理解
- 笔记-arm-linux-gnueabi-ld: cannot find common/env_embedded.0的解决方法
- php运用elasticsearch
- SSRS 锁定标题栏
- pat1091-1100
- jdbc
- iOS-线程相关
- Android中更换头像功能的实现
- 初识python
- 深入C++的运算符重载
- 策略模式