How to connect PHP to Elasticsearch database

王林
Release: 2023-10-03 09:14:02
Original
1123 people have browsed it

PHP 如何与 Elasticsearch 数据库连接

How to connect PHP to the Elasticsearch database requires specific code examples

1. Background introduction
Elasticsearch is an open source search engine based on Lucene, which provides a A distributed, multi-tenant full-text search engine that can implement functions such as real-time data analysis, data search, and data storage. When building web applications, connecting to Elasticsearch provides efficient data query and retrieval capabilities. This article explains how to connect to an Elasticsearch database in PHP and provides specific code examples.

2. Installation and Settings

  1. Install Elasticsearch: You can download the appropriate version from the Elasticsearch official website (https://www.elastic.co/downloads/elasticsearch) and follow the official Documentation for installation and setup.
  2. Install the Elasticsearch PHP client library: You can install the Elasticsearch PHP client library through Composer. Add the following dependencies in the composer.json file in the project directory and run the composer install command to install it.
"require": {
    "elasticsearch/elasticsearch": "~7.0"
}
Copy after login

3. Connect to Elasticsearch
To connect to the Elasticsearch database in PHP, you need to use the classes and methods provided by the Elasticsearch PHP client library. The following is a simple example:

require 'vendor/autoload.php';

use ElasticsearchClientBuilder;
$client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();

$params = [
    'index' => 'my_index', // 索引名称
    'id' => '1', // 文档 ID
];

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

print_r($response);
Copy after login

In the above code example, we first introduced the Elasticsearch PHP client library and created an Elasticsearch client (client). When creating the client, we need to specify the host and port number of Elasticsearch (the default "localhost:9200" is used in this example).

4. Specific operations

  1. Add documents
    The following is a sample code for adding documents to the Elasticsearch database:
$params = [
    'index' => 'my_index', // 索引名称
    'id' => 1, // 文档 ID
    'body' => [
        'title' => "PHP Elasticsearch",
        'content' => "Elasticsearch 是一个强大的搜索引擎。",
        'tags' => ['php', 'elasticsearch']
    ]
];
$response = $client->index($params);
Copy after login
  1. Query Document
    The following is a sample code to query a document based on ID:
$params = [
    'index' => 'my_index', // 索引名称
    'id' => 1 // 文档 ID
];
$response = $client->get($params);
print_r($response);
Copy after login
  1. Update document
    The following is a sample code to update a document:
$params = [
    'index' => 'my_index', // 索引名称
    'id' => 1, // 文档 ID
    'body' => [
        'doc' => [
            'title' => "Updated Title"
        ]
    ]
];
$response = $client->update($params);
Copy after login
  1. Delete documents
    The following is a sample code for deleting documents:
$params = [
    'index' => 'my_index', // 索引名称
    'id' => 1 // 文档 ID
];
$response = $client->delete($params);
Copy after login

5. Summary
This article introduces how to connect to the Elasticsearch database in PHP and provides specific code example. By using the Elasticsearch PHP client library, we can easily perform data addition, deletion, modification and query operations. I hope this article has provided some help for everyone using Elasticsearch in PHP. If you have any questions or concerns, please leave a message to discuss.

The above is the detailed content of How to connect PHP to Elasticsearch database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template