12. Geo Point

Fields of type geo_point accept latitude-longitude pairs

Set the index mappings

 1<?php
 2
 3$index = $elastic
 4        ->index()
 5        ->name('my_index')
 6        ->mappings(function(){
 7                return $this
 8                        ->body([
 9                            'properties' => [
10                                'location' => [
11                                    'type' => 'geo_point',
12                                ]
13                            ]
14                        ])
15                        ->getMap();
16        })
17        ->updateMap();

Let’s add an example data:

 1<?php
 2
 3$data = $elastic->document()
 4                ->index('my-index')
 5                ->body([
 6                    'text' => 'Şêro',
 7                    'location' => [
 8                        "lat" => 41.12,
 9                        "lon" => -71.34
10                    ]
11                ])
12                ->save();

Searching…

1<?php
2
3$search = $elastic->geoPoint()
4                    ->index('my-index')
5                    ->topLeft(42, -72)
6                    ->bottomRight(40, -74)
7                    ->getMap();
8
9return $search;

You can search with location function

 1<?php
 2
 3$geo = $elastic->geoPoint()
 4                ->index('my-index')
 5                ->pointType('geo_distance')
 6                ->location([
 7                    'location' => [
 8                        "top_left" => [
 9                            "lat" => 42,
10                            "lon" => -72
11                        ],
12                        "bottom_right" => [
13                            "lat" => 40,
14                            "lon" => -74
15                        ]
16                    ]
17                ])
18                ->search();
19
20// you don't need to use pointType(), it will return geo_bounding_box by default
21
22return $geo;