summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/GeohashCellTest.php
blob: 7cb8aef6bfcbc637b247c467aaefaaecb458051a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace Elastica\Test\Filter;

use Elastica\Test\Base as BaseTest;
use Elastica\Filter\GeohashCell;

class GeohashCellTest extends BaseTest
{
    public function testToArray()
    {
        $filter = new GeohashCell('pin', array('lat' => 37.789018, 'lon' => -122.391506), '50m');
        $expected = array(
            'geohash_cell' => array(
                'pin' => array(
                    'lat' => 37.789018,
                    'lon' => -122.391506
                ),
                'precision' => '50m',
                'neighbors' => false
            )
        );
        $this->assertEquals($expected, $filter->toArray());
    }

    public function testFilter()
    {
        $index = $this->_createIndex('geohash_filter_test');
        $type = $index->getType('test');
        $mapping = new \Elastica\Type\Mapping($type, array(
            'pin' => array(
                'type' => 'geo_point',
                'geohash' => true,
                'geohash_prefix' => true
            )
        ));
        $type->setMapping($mapping);

        $type->addDocument(new \Elastica\Document(1, array('pin' => '9q8yyzm0zpw8')));
        $type->addDocument(new \Elastica\Document(2, array('pin' => '9mudgb0yued0')));
        $index->refresh();

        $filter = new GeohashCell('pin', array('lat' => 32.828326, 'lon' => -117.255854));
        $query = new \Elastica\Query();
        $query->setFilter($filter);
        $results = $type->search($query);

        $this->assertEquals(1, $results->count());

        //test precision parameter
        $filter = new GeohashCell('pin', '9', 1);
        $query = new \Elastica\Query();
        $query->setFilter($filter);
        $results = $type->search($query);

        $this->assertEquals(2, $results->count());

        $index->delete();
    }
}