Home > Backend Development > PHP Tutorial > Search result display and customization technology based on Elasticsearch in PHP

Search result display and customization technology based on Elasticsearch in PHP

WBOY
Release: 2023-10-03 09:34:02
Original
1198 people have browsed it

PHP 中基于 Elasticsearch 的搜索结果展示与定制技术

Search result display and customization technology based on Elasticsearch in PHP

Introduction:
In the modern Internet era, search functions are an integral part of websites and applications . How to quickly and accurately display the results that users want through search engines has always been a challenge faced by developers. Elasticsearch is an open source distributed full-text search engine that is widely popular among developers for its high performance, powerful search and analysis capabilities. This article will introduce the Elasticsearch-based search result display and customization technology in PHP, and provide specific code examples.

1. Installation and configuration Elasticsearch

First, we need to install and start Elasticsearch. You can download the latest stable version from the official website of Elasticsearch (https://www.elastic.co/), and install and configure it according to the official documentation.

2. PHP Elasticsearch client library

In PHP, we can use the officially provided Elasticsearch client library to interact with Elasticsearch. You can use Composer to install:

composer require elasticsearch/elasticsearch
Copy after login

After the installation is complete, you can introduce the Elasticsearch client library into the project:

require 'vendor/autoload.php';

use ElasticsearchClientBuilder;
Copy after login

3. Create indexes and documents

Before starting the search , we need to create an index and add some documents to Elasticsearch. The following is an example PHP code:

$client = ClientBuilder::create()->build();

$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 1,
            'number_of_replicas' => 0
        ],
        'mappings' => [
            'properties' => [
                'title' => [
                    'type' => 'text'
                ],
                'content' => [
                    'type' => 'text'
                ]
            ]
        ]
    ]
];

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

$params = [
    'index' => 'my_index',
    'body' => [
        [
            'index' => [
                '_index' => 'my_index',
                '_id' => '1'
            ]
        ],
        [
            'title' => 'PHP Elasticsearch 搜索实战',
            'content' => '在 PHP 中使用 Elasticsearch 进行高效的搜索技术实战。'
        ],
        [
            'index' => [
                '_index' => 'my_index',
                '_id' => '2'
            ]
        ],
        [
            'title' => 'Elasticsearch 基础教程',
            'content' => '学习使用 Elasticsearch 基础教程,从入门到精通。'
        ],
    ]
];

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

4. Search and display

Next, we will show how to use Elasticsearch in PHP to search and display the results to the user. Here is an example PHP code:

$client = ClientBuilder::create()->build();

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

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

foreach ($response['hits']['hits'] as $hit) {
    echo $hit['_source']['title'] . '<br>';
    echo $hit['_source']['content'] . '<br><br>';
}
Copy after login

The above code will search for documents indexed to my_index that contain Elasticsearch in the title, and print the results.

5. Customization of search results

In addition to simple search and display, we can also customize search results. The following is a sample code for customized processing of search results:

$client = ClientBuilder::create()->build();

$params = [
    'index' => 'my_index',
    'body' => [
        'query' => [
            'match' => [
                'title' => 'Elasticsearch'
            ]
        ],
        'highlight' => [
            'pre_tags' => '<strong>',
            'post_tags' => '</strong>',
            'fields' => [
                'title' => new stdClass(),
                'content' => new stdClass()
            ]
        ]
    ]
];

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

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

    echo $title . '<br>';
    echo $content . '<br><br>';
}
Copy after login

The above code will introduce the highlighting function and use <strong> tags for the matching keywords in the search results. Make a mark.

Conclusion:
By using Elasticsearch and the PHP client library, we can quickly implement search functions in PHP and customize search results. I hope the content of this article will help you use Elasticsearch in PHP to display and customize search results.

The above is the detailed content of Search result display and customization technology 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