Home > Backend Development > PHP Tutorial > How to connect to Elasticsearch database using PDO

How to connect to Elasticsearch database using PDO

WBOY
Release: 2023-07-29 07:38:02
Original
1400 people have browsed it

How to connect to an Elasticsearch database using PDO

Overview:
Elasticsearch is a high-performance, scalable and distributed open source search and analysis engine. One of its features is support for sophisticated full-text search and real-time data analysis. PDO is a database abstraction layer for PHP that provides a concise way to connect and operate different types of databases. This article will introduce how to use PDO to connect to the Elasticsearch database and demonstrate several common operations.

Step 1: Install Elasticsearch and PDO extensions
First, you need to install Elasticsearch and the appropriate PDO extensions on the server. For installation methods, you can refer to official documentation or use package management tools.

Step 2: Create PDO connection
Next, we can start writing PHP code. First, import the PDO library at the beginning of the file:

require_once 'path/to/PDO.php';
Copy after login

Then, create a PDO connection object:

try {
  $pdo = new PDO('elastic:host=localhost;port=9200');
} catch(PDOException $e) {
  die('连接失败:' . $e->getMessage());
}
Copy after login

Please note that elastic here is the PDO DSN (data source name) used to specify the type of database that PDO connects to. Port number 9200 is the default port number for Elasticsearch. You can customize it according to your needs.

Step 3: Execute query
Next, we can perform some query operations against the Elasticsearch database. The following are several common query operation examples:

1. Query all documents

try {
  $query = $pdo->query('GET /_search');
  $results = $query->fetchAll(PDO::FETCH_ASSOC);
  print_r($results);
} catch(PDOException $e) {
  die('查询失败:' . $e->getMessage());
}
Copy after login

2. Query documents by conditions

try {
  $query = $pdo->query('POST /my-index/_search', ['q' => 'keyword']);
  $results = $query->fetchAll(PDO::FETCH_ASSOC);
  print_r($results);
} catch(PDOException $e) {
  die('查询失败:' . $e->getMessage());
}
Copy after login

Heremy-index is an example index name, you need to replace it according to the actual situation. The results of the query will include all documents that meet the criteria.

3. Add document

try {
  $query = $pdo->query('PUT /my-index/_doc/1', ['title' => 'example', 'content' => 'Lorem ipsum...']);
  $result = $query->fetch(PDO::FETCH_ASSOC);
  print_r($result);
} catch(PDOException $e) {
  die('添加失败:' . $e->getMessage());
}
Copy after login

Here_doc/1 is a sample document ID, you can replace it as needed. Where title and content are the field names and field values ​​of the example.

Summary:
The above are the steps and sample code on how to use PDO to connect to the Elasticsearch database. By connecting to Elasticsearch via PDO, you can easily perform various queries and data operations. I hope this article can provide some help for you to understand and use PDO and Elasticsearch.

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

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