Elasticsearch in PHP implements geographical location search and surrounding recommendations

WBOY
Release: 2023-10-03 09:22:01
Original
1321 people have browsed it

PHP 中 Elasticsearch 实现地理位置搜索与周边推荐

Elasticsearch in PHP implements geographical location search and surrounding recommendations

Abstract: Geographical location search and surrounding recommendations play a vital role in many web applications. Elasticsearch is a powerful open source search engine that provides rich functionality and flexibility to easily implement geographical location searches and nearby recommendations. This article will introduce how to use PHP and Elasticsearch for geographical location search and nearby recommendations, and provide specific code examples.

Text of article:

Introduction:
With the popularization of mobile Internet and geographical location services, users’ demand for searches and recommendations based on geographical location information is also increasing. Whether it is restaurant recommendations, nearby business searches, or tourist attraction recommendations, location search and surrounding recommendations have become one of the important functions of many web applications. Elasticsearch is one of the most popular search engines currently. It provides rich geographical location search and surrounding recommendation functions, and with the perfect combination of PHP, we can easily implement these functions.

Geographical location search:
Geographical location search is to search for results related to the location within a given range by specifying a geographical location coordinate. For example, search for restaurants or businesses within a certain distance. First, we need to create an index in Elasticsearch with a geolocation field. Here is sample code to create an index:

$params = [
    'index' => 'restaurants',
    'body'  => [
        'mappings' => [
            '_doc' => [
                'properties' => [
                    'name' => [
                        'type' => 'text',
                    ],
                    'location' => [
                        'type' => 'geo_point',
                    ],
                ],
            ],
        ],
    ],
];

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

Next, we can store the name and geolocation coordinates of the restaurant into Elasticsearch. Here is sample code to store documents into the index:

$params = [
    'index' => 'restaurants',
    'type'  => '_doc',
    'body'  => [
        'name'     => 'Restaurant A',
        'location' => [
            'lat' => 40.7128,
            'lon' => -74.0060,
        ],
    ],
];

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

When searching, we can filter the results using the geolocation parameter. The following is a sample code to search for restaurants based on a given geographical location and distance:

$params = [
    'index' => 'restaurants',
    'body'  => [
        'query' => [
            'bool' => [
                'filter' => [
                    'geo_distance' => [
                        'distance' => '1km',
                        'location' => [
                            'lat' => 40.7128,
                            'lon' => -74.0060,
                        ],
                    ],
                ],
            ],
        ],
    ],
];

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

Surrounding recommendations:
Surrounding recommendations are based on the user's current location, recommending the closest places or services to the user. In Elasticsearch, we can use geographical location aggregation to implement nearby recommendations. Here is the sample code to search for nearby restaurants based on a given geographical location and sort them by distance:

$params = [
    'index' => 'restaurants',
    'body'  => [
        'query' => [
            'match_all' => [],
        ],
        'sort'  => [
            '_geo_distance' => [
                'location' => [
                    'lat' => 40.7128,
                    'lon' => -74.0060,
                ],
                'order'  => 'asc',
                'unit'   => 'km',
            ],
        ],
        'size'  => 10,
    ],
];

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

In the above example, we use the _geo_distance sort parameter to sort the restaurants and specify the order The unit is kilometers.

Summary:
By using PHP and Elasticsearch combined, we can easily implement the functions of geographical location search and surrounding recommendations. First, we need to create an index with a geolocation field. We can then store geolocation information and search using geolocation parameters. For nearby recommendations, we can use the geolocation aggregation function and sort the results by distance. The above is the basic sample code, you can make appropriate modifications according to actual needs. I hope this article will help you understand and learn how to implement geographical location search and surrounding recommendations using Elasticsearch in PHP.

The above is the detailed content of Elasticsearch in PHP implements geographical location search and surrounding recommendations. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!