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个月搜 ...
随机推荐
- WPF 和 UWP 中,不用设置 From 或 To,Storyboard 即拥有更灵活的动画控制
无论是 WPF 还是 UWP 开发,如果用 Storyboard 和 Animation 做动画,我们多数时候都会设置 From 和 To 属性,用于从起始值动画到目标值.然而动画并不总是可以静态地指 ...
- LeetCode 675. Cut Off Trees for Golf Event
原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/description/ 题目: You are asked to ...
- decorator and @property
@property 首先, 它是个装饰器. 其次, 看到这个东西, 意味着它下面的函数可以被当作一个属性(成员变量)来看到. 通常, 这个函数会return点什么东西. 重点讲讲装饰器: 1, py ...
- uwsgi配置理解
最近使用uwsgi 部署了flask应用,出现了不少问题,仔细查阅了一下资料以及翻看了官方文档,就对自己了解到的做个总结~~ 一.http/http-socket/socketuwsgi开头当然少不了 ...
- windows 2016 容器管理
1. docker-compose 安装 python 2.7 pip pip install docker-compose 常见问题: ...
- Linux CentOS6.5 命令修改网络配置
登陆成功后,编辑网络信息文件: 命令:vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改配置如下图并保存,子网掩码.ip.默认网关根据自己网络进行调整: 永 ...
- filter异常捕捉
jsp中抛出一个异常 <% String action = request.getParameter("action"); if ("accountExceptio ...
- 洛谷3195(HNOI2008)玩具装箱
题目:https://www.luogu.org/problemnew/show/P3195 自己做斜率优化的第一道题. 推成斜率优化的样子很重要. 斜率优化的样子就是从 j 中求 i 的话,关系式里 ...
- (转)Java调用SQL Server的存储过程详解
本文转载自:http://dev.yesky.com/128/8088128.shtml 1使用不带参数的存储过程 使用 JDBC 驱动程序调用不带参数的存储过程时,必须使用 call SQL 转义序 ...
- temple-html5
ylbtech-HTML5: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 1 ...