2

I am trying to send a geopolygon filter to Elastic Search. It expects lat/long sets as numbers. But when I try to make that combination through loops, I am unable to to create it and constantly getting NumberFormatException[For input string. Here is the code I am trying.

    $points = $this->input->post('points');
    $points_to_pass = array();
    foreach ($points as $point) {
        $splits = explode(",", $point);

        $points_to_pass[] = [$splits[1],$splits[0]];
    }
    $json = [
        'query' => [
            'bool' => [
                'must_not' => [
                    ['terms' => ['_id' => []]],
                    ['terms' => ['rarity' => []]]
                ],
                'must' => [
                    'range' => [
                        'disappearTime' => [
                            'gte' => 'now',
                            'lte' => 'now+1d'
                        ]
                    ]
                ],
                'filter' => [
                    ['term' => ['pokemon_id' => 16]],
                    [
                        'geo_bounding_box' => [
                            'location' => [
                                'top_left' => [
                                    'lat' => 52.280577919216356,
                                    'lon' => -113.78533601760866
                                ],
                                'bottom_right' => [
                                    'lat' => 52.26306210545918,
                                    'lon' => -113.81855249404909
                                ]
                            ]
                        ]
                    ],
                    [
                        'geo_polygon' => [
                            'location' => [
                                "points" => [
                                    $points_to_pass
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ];

Whereas if I put hard coded values it works perfectly. Working example of hard coded values is as follows.

    $json = [
        'query' => [
            'bool' => [
                'must_not' => [
                    ['terms' => ['_id' => []]],
                    ['terms' => ['rarity' => []]]
                ],
                'must' => [
                    'range' => [
                        'disappearTime' => [
                            'gte' => 'now',
                            'lte' => 'now+1d'
                        ]
                    ]
                ],
                'filter' => [
                    ['term' => ['pokemon_id' => 16]],
                    [
                        'geo_bounding_box' => [
                            'location' => [
                                'top_left' => [
                                    'lat' => 52.280577919216356,
                                    'lon' => -113.78533601760866
                                ],
                                'bottom_right' => [
                                    'lat' => 52.26306210545918,
                                    'lon' => -113.81855249404909
                                ]
                            ]
                        ]
                    ],
                    [
                        'geo_polygon' => [
                            'location' => [
                                "points" => [
                                    [-113.78721646175813, 52.29637194474555],
                                    [-113.76335508934484, 52.281770664368565],
                                    [-113.76335508934484, 52.26112133563143],
                                    [-113.78721646175813, 52.24652005525444],
                                    [-113.82096153824187, 52.24652005525444],
                                    [-113.84482291065517, 52.26112133563143],
                                    [-113.84482291065517, 52.281770664368565],
                                    [-113.82096153824187, 52.29637194474555],
                                    [-113.78721646175813, 52.29637194474555],
                                    [-113.69997059121626, 52.298658944745554],
                                    [-113.67610798767082, 52.28405766436857],
                                    [-113.67610798767082, 52.26340833563143],
                                    [-113.69997059121626, 52.248807055254446],
                                    [-113.73371740878373, 52.248807055254446],
                                    [-113.75758001232917, 52.26340833563143],
                                    [-113.75758001232917, 52.28405766436857],
                                    [-113.73371740878373, 52.298658944745554],
                                    [-113.69997059121626, 52.298658944745554]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ];

How can I convert the $points_to_pass in a format that is accepted by ElasticSearch.

thanks

1 Answer 1

1

Since $points_to_pass is already an array of arrays, you should try like this:

                [
                    'geo_polygon' => [
                        'location' => [
                            "points" => $points_to_pass
                        ]
                    ]
                ]
Sign up to request clarification or add additional context in comments.

2 Comments

this is the error I received Error: parse_exception: numeric value expected\n
You might want to print out $points_to_pass and see if the values are numeric, as far as I can tell they are strings. You might need to transform $splits[i] to a number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.