<commit_before><?php
namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Query;
use Elastica\Query\GeoDistanceRange;
use Elastica\Query\MatchAll;
use Elastica\Test\Base as BaseTest;

class GeoDistanceRangeTest extends BaseTest
{
    /**
     * @group functional
     */
    public function testGeoPoint()
    {
        $index = $this->_createIndex();
        $type = $index->getType('test');

        // Set mapping
        $type->setMapping(['point' => ['type' => 'geo_point']]);

        // Add doc 1
        $doc1 = new Document(1,
            [
                'name' => 'ruflin',
            ]
        );

        $doc1->addGeoPoint('point', 17, 19);
        $type->addDocument($doc1);

        // Add doc 2
        $doc2 = new Document(2,
            [
                'name' => 'ruflin',
            ]
        );

        $doc2->addGeoPoint('point', 30, 40);
        $type->addDocument($doc2);

        $index->optimize();
        $index->refresh();

        // Only one point should be in radius
        $query = new Query();
        $geoQuery = new GeoDistanceRange(
            'point',
            ['lat' => 30, 'lon' => 40],
            ['from' => '0km', 'to' => '2km']
        );

        $query = new Query(new MatchAll());
        $query->setPostFilter($geoQuery);
        $this->_markSkipped50('[geo_distance_range] queries are no longer supported for geo_point field types. Use geo_distance sort or aggregations ');
        $this->assertEquals(1, $type->search($query)->count());

        // Both points should be inside
        $query = new Query();
        $geoQuery = new GeoDistanceRange(
            'point',
            ['lat' => 30, 'lon' => 40],
            ['gte' => '0km', 'lte' => '4000km']
        );
        $query = new Query(new MatchAll());
        $query->setPostFilter($geoQuery);
        $index->refresh();

        $this->assertEquals(2, $type->search($query)->count());
    }

    /**
     * @group unit
     * @expectedException \Elastica\Exception\InvalidException
     */
    public function testInvalidRange()
    {
        $geoQuery = new GeoDistanceRange(
            'point',
            ['lat' => 30, 'lon' => 40],
            ['invalid' => '0km', 'lte' => '40000km']
        );
    }

    /**
     * @group unit
     * @dataProvider invalidLocationDataProvider
     * @expectedException \Elastica\Exception\InvalidException
     */<commit_msg>#1184 Fix for skipped GeoDistanceRangeTest::testGeoPoint, replace  [geo_distance_range] queries with geo distance sorting. (#1191)<commit_after><?php

namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Query;
use Elastica\Query\GeoDistanceRange;
use Elastica\Query\MatchAll;
use Elastica\Test\Base as BaseTest;

class GeoDistanceRangeTest extends BaseTest
{
    /**
     * @group functional
     */
    public function testGeoPoint()
    {
        $index = $this->_createIndex();
        $type = $index->getType('test');

        // Set mapping
        $type->setMapping(['point' => ['type' => 'geo_point']]);

        // Add doc 1
        $doc1 = new Document(1,
            [
                'name' => 'ruflin',
            ]
        );

        $doc1->addGeoPoint('point', 17, 19);
        $type->addDocument($doc1);

        // Add doc 2
        $doc2 = new Document(2,
            [
                'name' => 'ruflin',
            ]
        );

        $doc2->addGeoPoint('point', 30, 40);
        $type->addDocument($doc2);

        $index->refresh();
        // Set geo distance sorting with coordinates near to first doc.
        $geoSort =
            ['_geo_distance' => [
                'point' => [
                    'lat' => 15,
                    'lon' => 20,
                ],
                'order' => 'asc',
                'unit' => 'km',
                'distance_type' => 'plane',
            ]];

        $query = new Query(new MatchAll());
        $query->setSort($geoSort);
        //doc #1 are the nearest to sorting point and must be first in result
        $this->assertEquals(1, $type->search($query)->current()->getId());
        $this->assertEquals(2, $type->search($query)->next()->getId());
    }

    /**
     * @group unit
     * @expectedException \Elastica\Exception\InvalidException
     */
    public function testInvalidRange()
    {
        $geoQuery = new GeoDistanceRange(
            'point',
            ['lat' => 30, 'lon' => 40],
            ['invalid' => '0km', 'lte' => '40000km']
        );
    }

    /**
     * @group unit
     * @dataProvider invalidLocationDataProvider
     * @expectedException \Elastica\Exception\InvalidException
     */
{'fraction_non_alphanumeric': 0.15451201038736204, 'fraction_numerical': 0.019476303830339752, 'mean_word_length': 1.865468071915685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5007775', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1344, 'n_words': 297}