如何使用PHP客户端更新和删除Elasticsearch文档

介绍:

如果您正在寻找可扩展的全文本搜索引擎,那么Elasticsearch是自然的选择。Elasticsearch为用户提供了一些流行语言(包括PHP)的官方API。Elasticsearch的低级PHP客户端提供了简化的界面,可轻松与Elasticsearch进行通信。可以轻松快捷地完成索引,删除,更新和检索文档等常见任务。在本教程中,我们将说明如何使用PHP客户端库在PHP中更新Elasticsearch文档,并且还将向您展示如何以类似的方式删除文档。

先决条件

在我们尝试使用PHP更新或删除Elasticsearch文档之前,确保有一些前提条件很重要。此任务有许多系统要求:

  • 首先,请确保在服务器上正确安装和配置了Elastic Stack以及Java。
  • 请记住,本教程中显示的示例假定您已收集了可以在演示期间进行修改的数据。
  • 您可以通过http://localhost:9200在浏览器中导航到本地是否运行Elasticsearch或https://{YOUR_DOMAIN}:{ELASTICSEARCH_PORT}在其他服务器上运行来查看Elasticsearch服务是否正在运行。您应该看到一个包含Elasticsearch集群信息的JSON对象响应:

通过导航到9200浏览器中的端口,来自Elasticsearch集群的JSON响应:

  • 确保正确安装了Elasticsearch PHP低级客户端(有关更多详细信息,请参见Elastic的PHP客户端官方Git存储库)。
  • 请记住,Elastic Stack的PHP客户端需要PHP 7.x或更高版本才能正常工作。要查看机器或服务器上正在运行哪个版本的PHP,请在终端中使用php带有-v选项的命令:

php -v

  • 如果您正在运行PHP 7,那么就可以继续前进了! 

为Elasticsearch客户端创建一个新的PHP脚本

我们需要做的第一件事是为Elasticsearch客户端创建一个新的PHP脚本。vendor使用cd终端中的命令导航到PHP客户端库所在的目录。进入该目录后,为您的API代码创建一个新的PHP脚本。您可以使用以下touch命令创建文件并为其命名:

sudo touch my-elastic.php
您可以使用nanovi或其他基于终端的文本编辑器来编辑你的脚本,该脚本将与Elasticsearch PHP库连接:

sudo nano my-elastic.php

将PHP脚本连接到低级Elasticsearch客户端

接下来,我们需要将脚本与Elasticsearch客户端连接。在我们的示例中,连接设置使用HTTPS。如果您使用的是HTTP,只需将$hosts阵列设置更改为http,并将端口设置为9200。所有其他设置可以保持不变。

您可以使用PHPrequire()函数来包含vendor目录的autoload.php脚本:

vendor我们之前提到的文件夹包含由Composer创建的整个Elasticsearch PHP库。该文件夹应与脚本位于同一目录中。如果不是,则说明低级客户端未正确安装,或者Composer在其他目录中运行。

您还可以指定路径,或通过附加__DIR__常量使用脚本的当前工作路径: `php require DIR . ‘/vendor/autoload.php’ 

配置PHP$hosts数组以匹配Elasticsearch配置

下一步将配置$hosts阵列,使其与服务器或计算机elasticsearch.yml文件中的设置一致:

$hosts = [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'https',
'user' => 'username',
'pass' => 'password!#$?*yeshua'
];

一旦你的$hosts阵列配置正确,你可以用它来创建一个新的$client客户端库的实例。

注意:如果尚未配置数组中的某些选项(例如userpassword),则可能不需要。

创建Elasticsearch PHP库的新客户端实例

您可以通过调用库中的create()方法,为Elasticsearch创建PHP低级客户端的新客户端实例ClientBuilder

$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();

$client对象的内置方法可用于更改Elasticsearch文档。

创建一个$paramsPHP对象数组以选择Elasticsearch索引的文档

您可以创建一个$paramsPHP对象数组,该数组用于通过将索引名和文档的"_id"作为参数传递来在Elasticsearch索引中选择文档:

$params = [
'index' => 'index_name',
'type' => 'doc_type',
'id' => '1'
];

创建此对象数组时,请确保正确传递值,因为它们区分大小写。还请记住,文档'id'必须作为字符串而不是整数传递。

在PHP中更新Elasticsearch文档

  • 警告:更新文档将覆盖以前文档中的旧信息

让我们看一下更新PHP中的Elasticsearch文档所需的代码示例。要做到这一点,你需要指定indextype以及id确定哪些文件得到更新。您在任何字段和值doc,这是内部的body参数,将被合并到更新的文档。在此示例中,我们将值设置age为7:

$params = [
'index' => 'children',
'type' => 'child',
'id' => 'child2'
'body' => [
'doc' => [
'age' => 7
]
]
];

如上例所示,更新文档时可以传递整数和字符串。只要确保索引_mapping将要更新的字段定义为整数类型即可。

得到Elasticsearch的回应

您可以使用tryandcatch块通过update()try方括号内调用方法来捕获Elasticsearch异常:

try{
// Calling the update method
$response = $client->update($params);
} catch (Exception $e) {
// var_dump will serialize the error message response
// and display it on the webpage
var_dump($e->getMessage());
}

使用Kibana验证API调用是否成功

您还可以使用Kibana控制台来验证更新是否成功。只需GET在Kibana控制台用户界面中进行请求:

GET children/child/child2

您应该在Kibana的结果窗格中看到类似于下图的图像:

更新Elasticsearch文档的另一种方法是在文档中添加一个新字段。在此示例中,我们添加了一个名为的字段complexion

$params = [
'index' => 'children',
'type' => 'child',
'id' => 'child2',
'body' => [
'doc' => [
'complexion' => "Moreno"
// new field with value
]
]
];

try{
$response = $client->update($params); // Calling the update method.
} catch (Exception $e) {
var_dump($e->getMessage());
}

 

与之前的更新一样,我们可以使用Kibana控制台来验证更新操作是否成功:

使用PHP客户端删除Elasticsearch文档

您不仅可以在PHP中更新Elasticsearch文档,还可以使用PHP客户端删除文档。要删除一个文件,你需要指出的indextypeid作为参数:

$params = [
'index' => 'children',
'type' => 'child',
'id' => 'child2',

];


$response = $client->delete($params);

在上面所示的例子中,与文档idchild2将被删除。这意味着该文档将不再可搜索。

您可以使用下面显示的命令来验证在Kibana中的删除:

GET children/child/child2

如果删除成功,您将在Kibana的结果窗格中看到以下内容:

 *注意它返回了found : false

结论

在Elasticsearch中处理数据时,更新和删除文档是常见的任务。幸运的是,Elasticsearch PHP客户端提供了一个简单的界面来执行这些操作。按照本教程中提供的说明,使用Elasticsearch PHP客户端可以轻松地从Elasticsearch索引更新和删除文档。

尽管在本教程中我们一次只检查了一段代码,但是您可以在下面完整地检查PHP脚本:

require 'vendor/autoload.php'; # include client library

// or uncomment this to specify the current directory:
//require __DIR__ . '/vendor/autoload.php'`

// create a "hosts" array with the Elasticsearch
// cluster's configuration passed as parameters
$hosts = [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'https',
'user' => 'username',
'pass' => 'password!#$?*yeshua'
];

// create a new client instance
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();


/* GETTING AN ELASTICSEARCH DOCUMENT
pass the index name and doc id as parameters
in a PHP associative array
*/


/*
$params = [
'index' => 'index_name',
'type' => 'doc_type',
'id' => '1'
];
*/


// update a document in the parameters
$params = [
'index' => 'children',
'type' => 'child',
'id' => 'child2'
'body' => [
'doc' => [
'age' => 7
// changes the age to 7
// when passed to client
]
]
]; 

// use a try and catch block to pass the parameters to
// the PHP client library to make a request to Elasticsearch
try{
// Calling the update method
$response = $client->update($params);
} catch (Exception $e) {
// var_dump will serialize the error message response
// and display it on the webpage
var_dump($e->getMessage());
}

 

猜你喜欢

转载自blog.csdn.net/allway2/article/details/108966694