How to use PHP and Elasticsearch to highlight search results

WBOY
Release: 2023-07-17 21:26:01
Original
1209 people have browsed it

How to use PHP and Elasticsearch to achieve highlighted search results

Introduction:
In the modern Internet world, search engines have become the main way for people to obtain information. In order to improve the readability and user experience of search results, highlighting search keywords has become a common requirement. This article will introduce how to use PHP and Elasticsearch to achieve highlighted search results.

1. Preparation
Before we begin, we need to ensure that PHP and Elasticsearch have been installed and configured correctly. You can use Composer to manage PHP dependencies, and then use the following command to install the PHP client of Elasticsearch:

composer require elasticsearch/elasticsearch
Copy after login

2. Connect to Elasticsearch
First, we need to create an Elasticsearch client instance in the PHP script . You can use the following code to connect to Elasticsearch:

require 'vendor/autoload.php';

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

3. Create index and mapping
Before searching, we need to create an index and specify the corresponding field mapping so that Elasticsearch can process it correctly Search request. The following is a sample code to create an index and mapping:

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

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

4. Perform a search and highlight the results
Next, we can start to perform the search operation and highlight the matching keywords . Here is a sample code that performs a search and highlights the results:

$params = [
    'index' => 'my_index',
    'body' => [
        'query' => [
            'match' => [
                'title' => '关键字',
            ],
        ],
        'highlight' => [
            'fields' => [
                'title' => new stdClass(),
                'content' => new stdClass(),
            ],
        ],
    ],
];

$response = $client->search($params);

foreach ($response['hits']['hits'] as $hit) {
    $title = isset($hit['highlight']['title'][0]) ? $hit['highlight']['title'][0] : $hit['_source']['title'];
    $content = isset($hit['highlight']['content'][0]) ? $hit['highlight']['content'][0] : $hit['_source']['content'];

    echo "标题:{$title}
";
    echo "内容:{$content}

";
}
Copy after login

The above code will perform a keyword-based search through Elasticsearch and highlight matching fields. In the search results, if there is a matching keyword in a certain field, the highlighted result for that field will be returned.

Conclusion:
This article introduces how to use PHP and Elasticsearch to achieve highlighted search results. By setting up appropriate indexing and mapping, and using relevant search parameters, we can easily achieve search result highlighting. This will undoubtedly improve users' understanding and reading of search results, thereby improving user experience.

Reference materials:

  • Elasticsearch official documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

The above is the detailed content of How to use PHP and Elasticsearch to highlight search results. 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