Home > Backend Development > PHP Tutorial > How to do full text search using Elasticsearch in PHP

How to do full text search using Elasticsearch in PHP

WBOY
Release: 2023-07-07 22:32:02
Original
708 people have browsed it

How to use Elasticsearch in PHP for full-text search

Elasticsearch is an open source high-performance search engine and distributed data storage system that can handle large-scale, high-speed data query and analysis. Using Elasticsearch in PHP for full-text search can help us process search requests more efficiently. This article will introduce how to use Elasticsearch in PHP for full-text search and give corresponding code examples.

  1. Installation and configuration of Elasticsearch

First, we need to install Elasticsearch and perform related configurations. For specific installation methods, please refer to the official documentation of Elasticsearch. After the installation is complete, we need to start the Elasticsearch service.

  1. Install the PHP extension of Elasticsearch

In order to operate Elasticsearch in PHP code, we need to install the PHP extension of Elasticsearch. You can install the PHP extension of Elasticsearch through Composer. The command is as follows:

composer require elasticsearch/elasticsearch
Copy after login

After the installation is completed, we need to introduce the PHP extension of Elasticsearch into the PHP code:

require 'vendor/autoload.php';
Copy after login
  1. Connect to the Elasticsearch server

Before operating with the Elasticsearch PHP extension, we need to connect to the Elasticsearch server first. The sample code is as follows:

$client = ElasticsearchClientBuilder::create()->build();
Copy after login
  1. Create index

Before using Elasticsearch for full-text search, we need to create an index first. The index is where Elasticsearch uses to store and index documents. Here is a sample code to create an index:

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

$response = $client->indices()->create($params);
Copy after login
  1. Add documents

After creating the index, we can add documents to the index. Documents are the data we need to perform full-text search. The sample code is as follows:

$params = [
    'index' => 'my_index',
    'body' => [
        'title' => 'Elasticsearch入门',
        'content' => 'Elasticsearch是一款高性能的搜索引擎',
    ]
];

$response = $client->index($params);
Copy after login
  1. Perform full-text search

After adding documents to the index, we can use Elasticsearch to perform full-text search. The sample code is as follows:

$params = [
    'index' => 'my_index',
    'body' => [
        'query' => [
            'match' => [
                'content' => '搜索引擎',
            ],
        ],
    ],
];

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

The above code will return documents containing the keyword "search engine".

Through the above steps, we can use Elasticsearch in PHP for full-text search. This greatly improves the efficiency and accuracy with which we process search requests. Hope this article helps you!

The above is the detailed content of How to do full text search using Elasticsearch in PHP. 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