Elasticsearch-PHP 快速开始
快速开始
本章节会给你一个客户端的主要功能(函数)是如何工作的快速概述。
安装
- 引入(包含)elasticsearch-php 在你的 composer.json 文件:
- {
- "require": {
- "elasticsearch/elasticsearch": "~1.0"
- }
- }
- 使用composer安装客户端:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install
- 在主项目(一般是index.php)中引入autoloader.php文件(如果你还没有引入的话),并且要实例化Elasticsearch的客户端:
- require 'vendor/autoload.php';
- $client = new Elasticsearch\Client();
索引一个文档
在elasticsearch-php中,几乎所有的东西都是通过数组配置的。REST 的端点(终结点),文档和可选参数,一切都是一个关联数组。
去索引一个文档,我们简单的指定一个主体(body)来包含我们希望索引的文档。文档中的每一个字段都由一个关联数组的键/值对表示。
索引(index),类型(type)和 ID 也被指定在数组参数中,数组如下:
- $params = array();
- $params['body'] = array('testField' => 'abc');
- $params['index'] = 'my_index';
- $params['type'] = 'my_type';
- $params['id'] = 'my_id';
- $ret = $client->index($params);
获取一个文档
让我们来获取我们刚刚索引的文档:
- $getParams = array();
- $getParams['index'] = 'my_index';
- $getParams['type'] = 'my_type';
- $getParams['id'] = 'my_id';
- $retDoc = $client->get($getParams);
搜索一个文档
搜索是 elasticsearch 的一个标志,所以让我们执行搜索。我们打算使用匹配查询作为示范:
- $searchParams['index'] = 'my_index';
- $searchParams['type'] = 'my_type';
- $searchParams['body']['query']['match']['testField'] = 'abc';
- $retDoc = $client->search($searchParams);
删除一个文档
好的,让我们继续删除一个我们之前添加的文档:
- $deleteParams = array();
- $deleteParams['index'] = 'my_index';
- $deleteParams['type'] = 'my_type';
- $deleteParams['id'] = 'my_id';
- $retDelete = $client->delete($deleteParams);
删除一个索引
由于 elasticsearch 的动态性质,我们添加第一个文档的时候自动创建了索引和一些默认设置。让我们删除这个索引,因为我们以后想要指定自己的设置:
- $deleteParams = array();
- $deleteParams['index'] = 'my_index';
- $client->indices()->delete($deleteParams);
创建一个索引
好吧,我们的索引被清空了,现在我们开始添加一个新的索引和一些自定义设置:
- $indexParams['index'] = 'my_index';
- $indexParams['body']['settings']['number_of_shards'] = 2;
- $indexParams['body']['settings']['number_of_replicas'] = 0;
- $client->indices()->create($indexParams);
总结
那些只是在客户端速成课程和语法上的概述。如果你熟悉elasticsearch, 你会注意到,这些方法的命名就像 REST 的端点(终结点)。
你还会发现客户端的配置方式使你发现通过你的IDE配置会非常方便。所有的核心操作都在 $client 对象(索引,搜索,获取等)下。索引和集群管理分别位于 $client->indices() 和 $client->cluster() 对象下。
查看剩下的文档去了解整个客户端是如何工作的。
例子代码
- <?php
- require 'vendor/autoload.php';
- $client = new Elasticsearch\Client();
- index($client);
- //get($client);
- // search($client);
- // deleteDoc($client);
- // deleteIndex($client);
- // createIndex($client);
- function index($client) {
- $params = array ();
- $params ['body'] = array (
- 'testField' => 'abc'
- );
- $params ['index'] = 'my_index';
- $params ['type'] = 'my_type';
- $params ['id'] = 'my_id';
- try {
- $ret = $client->index($params);
- println("create index success");
- } catch(Exception $e) {
- echo $e->getMessage();
- }
- }
- function get($client) {
- $getParams = array ();
- $getParams ['index'] = 'my_index';
- $getParams ['type'] = 'my_type';
- $getParams ['id'] = 'my_id';
- $retDoc = $client->get($getParams);
- println($retDoc);
- }
- function search($client) {
- $searchParams ['index'] = 'my_index';
- $searchParams ['type'] = 'my_type';
- $searchParams ['body'] ['query'] ['match'] ['testField'] = 'abc';
- $retDoc = $client->search($searchParams);
- println($retDoc);
- }
- function deleteDoc($client) {
- $deleteParams = array ();
- $deleteParams ['index'] = 'my_index';
- $deleteParams ['type'] = 'my_type';
- $deleteParams ['id'] = 'my_id';
- $retDelete = $client->delete($deleteParams);
- println($retDelete);
- }
- function deleteIndex($client) {
- $deleteParams = array ();
- $deleteParams ['index'] = 'my_index';
- $retDelete = $client->indices()->delete($deleteParams);
- println($retDelete);
- }
- function createIndex($client) {
- $indexParams ['index'] = 'my_index';
- $indexParams ['body'] ['settings'] ['number_of_shards'] = 2;
- $indexParams ['body'] ['settings'] ['number_of_replicas'] = 0;
- $retCreate = $client->indices()->create($indexParams);
- println($retCreate);
- }
- function println($var) {
- echo "<br>";
- $type = gettype($var);
- if ($type == "array" || $type == "object") {
- echo json_encode($var);
- } else {
- echo $var;
- }
- echo "<br>";
- }
查看每个方法的运行结果:
index():
- create index success
get():
- {
- "_index": "my_index",
- "_type": "my_type",
- "_id": "my_id",
- "_version": 1,
- "found": true,
- "_source": {
- "testField": "abc"
- }
- }
search():
- {
- "took": 3,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "failed": 0
- },
- "hits": {
- "total": 1,
- "max_score": 0.30685282,
- "hits": [
- {
- "_index": "my_index",
- "_type": "my_type",
- "_id": "my_id",
- "_score": 0.30685282,
- "_source": {
- "testField": "abc"
- }
- }
- ]
- }
- }
deleteDoc():
- {
- "found": true,
- "_index": "my_index",
- "_type": "my_type",
- "_id": "my_id",
- "_version": 2
- }
deleteIndex():
- {
- "acknowledged": true
- }
createIndex():
- {
- "acknowledged": true
- }
Elasticsearch-PHP 快速开始的更多相关文章
- logstash+elasticsearch+kibana快速搭建日志平台
使用logstash+elasticsearch+kibana快速搭建日志平台 日志的分析和监控在系统开发中占非常重要的地位,系统越复杂,日志的分析和监控就越重要,常见的需求有: 根据关键字查询日 ...
- 使用logstash+elasticsearch+kibana快速搭建日志平台
日志的分析和监控在系统开发中占非常重要的地位,系统越复杂,日志的分析和监控就越重要,常见的需求有: * 根据关键字查询日志详情 * 监控系统的运行状况 * 统计分析,比如接口的调用次数.执行时间.成功 ...
- Elasticsearch【快速入门】
前言:毕设项目还要求加了这个做大数据搜索,正好自己也比较感兴趣,就一起来学习学习吧! Elasticsearch 简介 Elasticsearch 是一个分布式.RESTful 风格的搜索和数据分析引 ...
- 【转载】使用logstash+elasticsearch+kibana快速搭建日志平台
原文链接:http://www.cnblogs.com/buzzlight/p/logstash_elasticsearch_kibana_log.html 日志的分析和监控在系统开发中占非常重要的地 ...
- elasticsearch的快速安装
在阿里云服务器快速安装ElasticSearch 1.安装好java的jdk环境 2.使用wget下载elasticsearch安装包,wget的速度比较满,如果等不及的话,可以先下载好安装包再上传解 ...
- 3.1_springboot2.x检索之elasticsearch安装&快速入门
1.elasticsearch简介&安装 1.1.1.elasticsearch介绍 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选.他可以快 ...
- 如何通过Elasticsearch Scroll快速取出数据,构造pandas dataframe — Python多进程实现
首先,python 多线程不能充分利用多核CPU的计算资源(只能共用一个CPU),所以得用多进程.笔者从3.7亿数据的索引,取200多万的数据,从取数据到构造pandas dataframe总共大概用 ...
- 使用Elasticsearch Operator快速部署Elasticsearch集群
转载自:https://www.qikqiak.com/post/elastic-cloud-on-k8s/ 随着 kubernetes 的快速发展,很多应用都在往 kubernetes 上面迁移,现 ...
- Elasticsearch的快速使用——Spring Boot使用Elastcisearch, 并且使用Logstash同步mysql和Elasticsearch的数据
我主要是给出一些方向,很多地方没有详细说明.当时我学习的时候一直不知道怎么着手,花时间找入口点上比较多,你们可以直接顺着方向去找资源学习. 如果不是Spring Boot项目,那么根据Elastics ...
- (转)开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引
Github, Soundcloud, FogCreek, Stackoverflow, Foursquare,等公司通过elasticsearch提供搜索或大规模日志分析可视化等服务.博主近4个月搜 ...
随机推荐
- Lua table
获取数组长度 在Lua中可以使用“#”和table.maxn两种方法来获取数组的长度 arr = {,,,} arr[] = 7 都仅统计数字key的长度: #是从1递增到nil的长度: table. ...
- 7个GIF动图帮你瞬间理解三角函数
7个GIF动图帮你瞬间理解三角函数 蝌蚪五线谱 百家号04-2120:53 图片来源:IMGUR 三角函数是数学中研究三角形的一个分支,专门阐述三角形的角度和对应边的关系. 有趣的是,定义边角关系的三 ...
- 最最基本的SQL常用命令
2015-12-01 18:08:52 1.启动/关闭mysql 开始菜单搜索cmd,右击,以管理员身份运行,输入net start mysql启动mysql,输入net stop mysql关闭my ...
- shell监控网卡状态,故障时自动重启网卡
今天朋友找我写个监控网卡状态的脚本,要求在系统网卡挂了可以自己启动起来,这个要求是不是很bt,我考虑了半天,简单的写了个shell脚本来监控,实现原理是使用ping来测试网络连通性,如果不通就重启 ...
- 【转】ubuntu 12.04 /sbin/ldconfig.real: /usr/local/lib/*.so.8 不是符号连接 解决办法
原文网址:http://blog.csdn.net/shulianghe/article/details/21176059 最近在ubuntu12.04下使用sudo apt-get install安 ...
- wordpress插件汉化包,和使用教程
点击下载汉化包 解压后上传到该插件的 languages 目录即可
- WiresShark使用说明
WiresShark是号称全世界最流行的网络分析工具(它的官网自己说的).下载地址:https://www.wireshark.org/#download,目前最新版本是2.6.2.我本地用的是汉化的 ...
- U盘永久系统-centos
U盘永久系统-centos 问题: 服务器centos系统崩溃,重装需要备份其中数据,约4T,实验室有远程存储服务器,然而rescue模式进去后无法挂载远程存储,只好做一个真正的U盘系统解决了. 方案 ...
- CentOS6.8编译安装LAMP
CentOS6.8编译安装Apache2.4.25.MySQL5.7.16.PHP5.6.29 初始化 #固定IP vi /etc/sysconfig/network-scripts/ifcfg-et ...
- (转)Inno Setup入门(二)——修改安装过程中的图片
本文转载自:http://blog.csdn.net/augusdi/article/details/8564793 修改安装过程中的图片 一般编译之后,安装过程中出现在左边图片是是下图这个样子的: ...