Home  >  Article  >  Backend Development  >  How to implement related search functions through PHP and Elasticsearch

How to implement related search functions through PHP and Elasticsearch

WBOY
WBOYOriginal
2023-07-17 08:46:36958browse

How to implement related search functions through PHP and Elasticsearch

Overview:
In modern web applications, search functions are a very important part. Compared with full-text search in traditional databases, Elasticsearch, as a powerful open source search engine, has more flexible and efficient search capabilities. This article will introduce you to how to use PHP and Elasticsearch to implement related search functions.

  1. Environment setup
    Before we begin, we need to make sure that PHP and Elasticsearch have been installed correctly. It can be downloaded and installed through the official website of PHP and the official website of Elasticsearch. In addition, you also need to install Elasticsearch's PHP client library - Elasticsearch-PHP. It can be installed through Composer and execute the following command:

    composer require elasticsearch/elasticsearch
  2. Create index
    Before using Elasticsearch to search, you first need to create an index. The index is the basic unit for storing and organizing data. . Indexes can be easily created and managed through the API provided by Elasticsearch-PHP. The following is a sample code to create an index and define field mapping:

    require_once 'vendor/autoload.php';
    
    $client = ElasticsearchClientBuilder::create()->build();
    
    $params = [
     'index' => 'my_index',
     'body' => [
         'mappings' => [
             'properties' => [
                 'title' => [
                     'type' => 'text',
                     'analyzer' => 'standard'
                 ],
                 'content' => [
                     'type' => 'text',
                     'analyzer' => 'standard'
                 ],
             ]
         ]
     ]
    ];
    
    $response = $client->indices()->create($params);
  3. Write data
    After the index creation is completed, we need to write the data to Elasticsearch for subsequent use Search. Writing of data can be achieved through simple HTTP requests. The following is a sample code that writes an article to the Elasticsearch index:

    $params = [
     'index' => 'my_index',
     'id' => 1,
     'body' => [
         'title' => 'Elasticsearch相关搜索',
         'content' => 'Elasticsearch是一种强大的搜索引擎,可以实现高效的相关搜索功能。',
     ]
    ];
    
    $response = $client->index($params);
  4. Perform search
    After the data writing is completed, we can perform the search operation. Elasticsearch provides a rich API for different types of searches and supports a variety of query methods, including match queries, phrase queries, range queries, etc. The following is a sample code to perform a related search:

    $params = [
     'index' => 'my_index',
     'body' => [
         'query' => [
             'match' => [
                 'content' => '搜索引擎',
             ]
         ]
     ]
    ];
    
    $response = $client->search($params);
  5. Get search results
    After performing the search operation, we can obtain the search results through $response. Search results are returned in JSON format, which contains a list of matching documents and related score information. The following is a sample code that traverses the search results and outputs it:

    foreach ($response['hits']['hits'] as $hit) {
     echo 'ID: ' . $hit['_id'] . ', Score: ' . $hit['_score'] . ', Title: ' . $hit['_source']['title'] . PHP_EOL;
    }

Summary:
Through the introduction of this article, we have learned how to use PHP and Elasticsearch to implement related search functions. First, we need to install and configure PHP and Elasticsearch environments, then create indexes and write data. Finally, use the API provided by Elasticsearch-PHP to perform relevant searches and obtain search results. I believe that through the guidance of this article, everyone already has the basic knowledge to develop related search functions. In actual development, search query parameters and result display methods can be further optimized according to needs to provide a better search experience.

The above is the detailed content of How to implement related search functions through PHP and Elasticsearch. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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