Home > Backend Development > PHP Tutorial > Development practice of related search functions based on Elasticsearch in PHP

Development practice of related search functions based on Elasticsearch in PHP

王林
Release: 2023-10-03 09:44:01
Original
838 people have browsed it

PHP 中基于 Elasticsearch 的相关搜索功能开发实践

Related search function development practice based on Elasticsearch in PHP

Overview
In modern Web development, search function is a very important part. As a powerful and flexible distributed search engine, Elasticsearch is widely used in various web applications. This article will introduce how to use Elasticsearch in PHP to develop related search functions, and attach specific code examples.

Installation and configuration Elasticsearch
First, we need to install Elasticsearch and perform related configurations. You can select the version suitable for your operating system through the download page of the Elasticsearch official website (https://www.elastic.co/cn/downloads/elasticsearch) and install it according to the official installation steps.

After the installation is complete, you need to modify the Elasticsearch configuration file elasticsearch.yml. Open this file, find and modify the following configuration:

cluster.name: my-cluster
node.name: my-node
network.host: 0.0.0.0
http.port: 9200
Copy after login

Using Elasticsearch PHP client
To use Elasticsearch in PHP, we need to install the Elasticsearch PHP client. It can be installed through Composer, the command is as follows:

composer require elasticsearch/elasticsearch
Copy after login

After the installation is completed, we can use the following code to initialize an Elasticsearch client in PHP:

require 'vendor/autoload.php';

$client = ElasticsearchClientBuilder::create()->build();
Copy after login

Create indexes and mappings
In use Before Elasticsearch can search, we need to create indexes and set up mappings. Indexes are like tables in a database, and mappings are like fields in a table. Each index can have multiple mappings.

The following is an example of creating an index and mapping:

$params = [
    'index' => 'my_index',
    'body' => [
        'mappings' => [
            'properties' => [
                'title' => [
                    'type' => 'text',
                ],
                'content' => [
                    'type' => 'text',
                ],
                'created_at' => [
                    'type' => 'date',
                ],
            ],
        ],
    ],
];

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

Add documents to the index
After the index creation is complete, we can add data to the index so that it can be searched. The following is an example of adding documents to the index:

$params = [
    'index' => 'my_index',
    'id' => '1',
    'body' => [
        'title' => 'Elasticsearch 示例文章',
        'content' => '这是一个关于Elasticsearch的示例文章。',
        'created_at' => '2022-01-01',
    ],
];

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

Search for documents
With the index and data, we can perform search operations. The following is a simple full-text search example:

$params = [
    'index' => 'my_index',
    'body' => [
        'query' => [
            'match' => [
                'content' => 'Elasticsearch',
            ],
        ],
    ],
];

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

The above code will return all documents containing the "Elasticsearch" keyword in the content.

Aggregation and filtering
In addition to simple full-text search, Elasticsearch also provides powerful aggregation and filtering functions. Here is an example:

$params = [
    'index' => 'my_index',
    'body' => [
        'aggs' => [
            'avg_views' => [
                'avg' => [
                    'field' => 'views',
                ],
            ],
        ],
        'query' => [
            'term' => [
                'category' => 'technology',
            ],
        ],
    ],
];

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

The above code will return the average number of views in documents classified as "technology".

Summary
This article introduces how to use Elasticsearch to develop related search functions in PHP. We go through installing and configuring Elasticsearch, using the Elasticsearch PHP client, creating indexes and mappings, and adding documents to the index. Additionally, we demonstrate the use of simple full-text search and aggregate filtering capabilities. The above sample code is for reference only and needs to be adjusted according to specific needs in actual projects.

I hope this article can help you understand and use the relevant search functions based on Elasticsearch in PHP.

The above is the detailed content of Development practice of related search functions based on Elasticsearch in PHP. 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