初窥 Elasticsearch-PHP [1.0]
时间:2022-06-19
本文章向大家介绍初窥 Elasticsearch-PHP [1.0]
,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
初始化
Elasticsearch-PHP 下载地址:http://www.thinkphp.cn/code/download/id/1290.html 教程以ThinkPHP为例
下载Elasticsearch-PHP文件放在ThinkPHPLibraryVendor
Vendor('Elasticsearch.autoload');
$params['hosts'] = array(
'127.0.0.1:9200'
);
$this->client = new ElasticsearchClient($params);
如果不给hosts参数也是可以的,默认为localhost:9200
索引
创建一个索引
- 自增ID public function create_index(){ $indexParams['index'] = 'my_index'; $indexParams['type'] = 'my_index'; $indexParams['body']['settings']['number_of_shards'] = 2; $indexParams['body']['settings']['number_of_replicas'] = 0; $this->client->create($indexParams); }
- 使用自己的ID 只要再添加一个id字段即可。例: $indexParams['id'] = '123';
删除索引
public function delete_index(){
$deleteParams['index'] = 'my_index';
$ret = $this->client->indices()->delete($deleteParams);
dump($ret);
}
文档
增
public function add_document(){
$params = array();
$params['body'] = array(
'testField' => 'dfdsfdsf',
'ok' => '1 '
);
$params['index'] = 'my_index2';
$params['type'] = 'my_index2';
$params['id'] = '222';
$ret = $this->client->index($params);
dump($ret);
}
查
- 根据id、index、type来查询(一条数据,精确查询) public function get_document(){ $getParams = array(); $getParams['index'] = 'my_index'; $getParams['type'] = 'my_index'; $getParams['id'] = 'w1231313'; $retDoc = $this->client->get($getParams); dump($retDoc); }
- 模糊查询 public function search_(){ $searchParams = array(); $searchParams['body'] = array( 'query' => array( 'match' => array( 'testField' => 'dfdsfdsf' ) ) ); $retDoc = $this->client->search($searchParams); dump($retDoc); }
- 检查文档是否存在 public function exists(){ $getParams = array( 'index' => 'my_index', 'type' => 'my_index', 'id' => 'w1231313' ); $exists = $this->client->exists($getParams); dump($exists); }
改
public function update_document(){
$updateParams = array();
$updateParams['index'] = 'my_index';
$updateParams['type'] = 'my_index';
$updateParams['id'] = 'w1231313';
$updateParams['body']['doc']['testField'] = 'xxxx';
$response = $this->client->update($updateParams);
dump($response);
}
删
public function delete_document(){
$deleteParams = array();
$deleteParams['index'] = 'my_index';
$deleteParams['type'] = 'my_index';
$deleteParams['id'] = 'w1231313';
$retDelete = $this->client->delete($deleteParams);
dump($retDelete);
}
搜索
- 空查询 public function search(){ $retDoc = $this->client->search(); dump($retDoc); } 当然可以加上其他的一些条件,例如: $searchParams['index'] = 'my_index'; $searchParams['type'] = 'my_index';
- 查询与过滤
term : 相当于模糊查询 match : 相当于精确查询
public function test111(){
$params['index'] = 'my_index';
$params['type'] = 'my_index';
$filter = array();
$filter['term']['my_field'] = 'abc';
$query = array();
$query['match']['my_other_field'] = 'xyz';
$params['body']['query']['filtered'] = array(
"filter" => $filter,
"query" => $query
);
$results = $this->client->search($params);
dump($results);
}
- 分页
from : 跳过开始的结果数,默认0 size: 结果数,默认为10
public function search(){
$searchParams['index'] = 'my_index';
$searchParams['type'] = 'my_index';
$searchParams['from'] = 0;
$searchParams['size'] = 100;
$retDoc = $this->client->search($searchParams);
dump($retDoc);
}
- 布尔查询
must : 多个查询条件的完全匹配,相当于 and。 must_not : 多个查询条件的相反匹配,相当于 not。 should : 至少有一个查询条件匹配, 相当于 or。
public function boolquery(){
$params['index'] = 'my_index';
$params['type'] = 'my_index';
$params['body']['query']['bool']['must'] = array(
array('match' => array('testField' => 'abc')),
array('match' => array('anotherTestField' => 'xyz')),
);
$params['body']['query']['bool']['must_not'] = array(
array('term' => array('testField' => 'abc'))
);
$results = $this->client->search($params);
dump($results);
}
END
大致的一些使用如上,若有疑问,女生请找我,男生自己看API。 传送门 es-API:http://es.xiaoleilu.com/054_Query_DSL/80_Validating_queries.html es-PHP-API:https://www.elastic.co/guide/en/elasticsearch/client/php-api/1.0/_search_operations.html
。
(adsbygoogle = window.adsbygoogle || []).push({});
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Git同时上传到github和码云(国内知名的男性交友网站)
- 读源码——Guava-Cache
- LeetCode 94 | 构造出所有二叉搜索树
- 每日一题 | 最大考试分数问题
- 计算广告——收入分解
- 浅谈mybatis中的占位符
- Python | 详解Python中的协程,为什么说它的底层是生成器?
- 初识Mybatis中的动态sql
- Raw use of parameterized class 'Future'
- javaweb遇到的报错问题以及解决方案(持续更新)
- Spark Java UDAF 输入struct嵌套结构
- 深入理解Java内存模型
- Mybatis高级查询(一):resultMap与resultType
- JDK错误用法—TimSort
- Mybatis高级查询(三):分页查询