Use PHP to operate Elasticsearch database

王林
Release: 2023-05-17 09:10:02
Original
1449 people have browsed it

With the advent of the big data era, many companies have begun to choose to use Elasticsearch databases to store and index massive data. PHP is a widely used web development language, so it will be helpful to understand how to use PHP to operate the Elasticsearch database.

  1. Install Elasticsearch and PHP extensions

First, you need to install Elasticsearch and PHP extensions on the server. Elasticsearch installation can be completed through the following command (taking Ubuntu as an example):

sudo apt-get update
sudo apt-get install elasticsearch
Copy after login

And the PHP extension can be installed through PECL:

sudo pecl install elasticsearch
Copy after login
  1. Establish a connection

Before using PHP to operate the Elasticsearch database, you need to establish a connection first. This can be achieved through the following code:

$params = [
    'hosts' => ['localhost:9200']
];

$client = ElasticsearchClientBuilder::create()->setHosts($params['hosts'])->build();
Copy after login
  1. Create index

The command to create an index is as follows:

$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 2,
            'number_of_replicas' => 0
        ]
    ]
];

$response = $client->indices()->create($params);
Copy after login

In this example, we create an index named Index of "my_index" with two shards and zero replicas set up.

  1. Insert data

The following is a code example for inserting data into the index:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => '1',
    'body' => [
        'title' => 'The quick brown fox',
        'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
    ]
];

$response = $client->index($params);
Copy after login

In this example, we insert data into the index named "my_index "The document of type "my_type" inserts a title and body data.

  1. Query data

The following is a code example for querying data from the index:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'title' => 'quick brown'
            ]
        ]
    ]
];

$response = $client->search($params);
Copy after login

In this example, we use the "quick brown" condition to Query all documents of type "my_type" whose index name is "my_index" and return results that contain the "title" field.

  1. Update data

The following is a code example for updating data:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => '1',
    'body' => [
        'doc' => [
            'content' => 'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
        ]
    ]
];

$response = $client->update($params);
Copy after login

In this example, we change the "content" of the document with ID 1 ” field is updated to “Sed do eiusmod…”.

  1. Delete data

The following is a code example to delete data:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => '1'
];

$response = $client->delete($params);
Copy after login

In this example, we deleted the document with ID 1.

Summary

When using PHP to operate the Elasticsearch database, you need to pay attention to the following points:

  • You need to install Elasticsearch and PHP extensions first.
  • After establishing a connection, you can use various functions of Elasticsearch, including creating indexes, inserting data, querying data, updating data and deleting data.
  • Be familiar with the parameters and formats of different operations.

Using PHP to operate the Elasticsearch database allows us to manage and retrieve large amounts of data more effectively, thereby increasing the data value of the enterprise.

The above is the detailed content of Use PHP to operate Elasticsearch database. 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