前言 Elasticsearch-PHP是PHP连接Elasticsearch库的扩展,是用PHP语言开发的,类似于PHP通过Predis操作redis库的功能。如果想详细了解Elasticsearch的话,可以下载《Elasticsearch权威指南(中文版).pdf》。

下载安装Elasticsearch-PHP 下载Elasticsearch-PHP的话。你可以直接去GitHub上下载,也可以通过Composer包管理工具直接下载。这里我是通过Composer包管理工具直接下载,所以此时我们需要安装Composer,其实很简单的,可以参考composer详细安装教程。 安装好Composer之后就可以进行Elasticsearch-PHP的下载了。 首先我们在服务器根目录下创建一个elasticphp文件夹,在该文件夹中创建一个composer.json文件,内容如下:
{
    "require": {
        "elasticsearch/elasticsearch": "2.2.1"
    }
}

然后启动Windows命令窗口cmd,进入到elasticphp目录下,运行:composer  update   命令。如下图所示:


在这个下载过程中可能需要等待一段时间,下载完后再elasticphp目录下会出现vender目录文件,如下图所示:


接下来我们可以在elasticphp目录下创建一个index.php文件,内容如下:
require 'vendor/autoload.php';    //加载自动加载文件

#如果没有设置主机地址默认为127.0.0.1:9200
$client = Elasticsearch\ClientBuilder::create()->setHosts(['localhost:9200'])->build();
var_dump($client);

如果出现如下图所示,表示连接Elasticsearch服务成功,也即Elasticsearch-PHP安装成功:


PHP通过Elasticsearch-PHP操作Elasticsearch库
PHP使用Elasticsearch-PHP的要求:
  • PHP的版本在5.3以上,我的版本是PHP5.5。
  • 在php.ini中开启curl和openssl扩展

一、通过设置mapping结构创建一个索引库(相当于mysql创建一个数据库)
require 'vendor/autoload.php';    //加载自动加载文件

#如果没有设置主机地址默认为127.0.0.1:9200
$client = Elasticsearch\ClientBuilder::create()->setHosts(['localhost:9200'])->build();

/**
* 创建库索引的mapping结构
*/
$params = [
	'index' => 'my_index',  //索引名(相当于mysql的数据库)
	'body' => [
		'settings' => [
			'number_of_shards' => 5,  #分片数
		],
		'mappings' => [ 
			'my_type' => [ //类型名(相当于mysql的表)
				'_all' => [
					'enabled' => 'false'
				],
				'_routing' => [
					'required' => 'true'
				],
				'properties' => [ //文档类型设置(相当于mysql的数据类型)
					'name' => [
						'type' => 'string',
						'store' => 'true'
					],
					'age' => [
						'type' => 'integer'
					]
				]
			]
		]
	]
];

$res = $client->indices()->create($params);   //创建库索引



二、库索引操作
require 'vendor/autoload.php';    //加载自动加载文件

#如果没有设置主机地址默认为127.0.0.1:9200
$client = Elasticsearch\ClientBuilder::create()->setHosts(['localhost:9200'])->build();

/**
* 库索引操作
*/
$params = [
	'index' => 'my_index-0',
	'client' => [
		'ignore' => 404
	]
];
$res = $client->indices()->delete($params);    //删除库索引
$res = $client->indices()->getSettings($params);//获取库索引设置信息
$res = $client->indices()->exists($params);   //检测库是否存在
$res = $client->indices()->getMapping($params);   //获取mapping信息

三、文档操作
/**
* 获取指定文档
*/
$params = [
	'index' => 'my_index',
	'type' => 'my_type',
	'id' => '3416a75f4cea9109507cacd8e2f2aefc'
];
$res = $client->get($params);   //获取指定的文档
$res = $client->getSource($params);   //获取指定文档的sourse内容(即字段的信息)

 //删除指定的文档
$res = $client->delete($params); 


/**
* 更新指定文档
*/
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => '3416a75f4cea9109507cacd8e2f2aefc',
    'body' => [
        'doc' => [
            'age' => 150
        ]
    ]
];

$res = $client->update($params);


/**
* 文档查询
*/
$params = [
    'index' =>  'my_index',   //['my_index1', 'my_index2'],可以通过这种形式进行跨库查询
    'type' => 'my_type',//['my_type1', 'my_type2'],
    'body' => [
        'query' => [
            'match' => [
                'age' => '24'
            ]
        ],
        'from' => '0',
        'size' => '200'
		'sort' => [
			'age' => 'desc'   //对age字段进行降序排序
		]
    ]
];

$res = $client->search($params);


更多信息参考https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_overview.html

更多推荐

Elasticsearch-PHP基本API使用教程