Correct way to construct PHP function_score for gauss -> location?


(Jason Head) #1

I'm getting the error:
query_parsing_exception: failed to parse [START_OBJECT]. malformed query, expected a [START_ARRAY] while parsing functions but got a [function_score] instead

The documentation for gauss / location inside function_score doesn't specify the stdClass / array structure needed.

Here is the PHP for the object being built:
I think the problem starts at the function_score section.
I replaced a 'filtered' / 'filter' section and a 'sort' section to implement distance scoring.

// search by location and match
// alt param body build
if ($latitude && $longitude) {
    $scaleVal = $radius+1;

    $location = new \stdClass();
    $location->origin = new \stdClass();
    $location->origin->lat = $latitude;
    $location->origin->lon = $longitude;
    $location->offset = $radius . $distance_units;
    $location->scale = $scaleVal . $distance_units;

    $params['body'] = array(
        'aggs' => $aggregationParam,
        'query' => array(
            'function_score' => array(
                'functions' => array(
                    array(
                        array('gauss' => $location)
                    )
                )
            )
        )
    );
}

Is there documentation for the PHP side that covers this?
Somewhere that has close examples?

I have taken a look at the documentation at
https://www.elastic.co/guide/en/elasticsearch/guide/current/sorting-by-distance.html#scoring-by-distance
https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html

and also found this:

What I am updating:

$params['body'] = [
                'aggs' => $aggregationParam,
                'query' => [
                    'filtered' => [
                        "query" => $query,

                        "filter" => [
                            "geo_distance" => [
                                "distance" => $radius . $distance_units,
                                "site_addresses.lat_lon" => [
                                    "lat" => $latitude,
                                    "lon" => $longitude
                                ]
                            ]
                        ]
                    ],

                sort by distance
                "sort" => [
                    "_geo_distance" => [
                        "site_addresses.lat_lon" => [
                            "lat" => $latitude,
                            "lon" => $longitude
                        ],
                        "order" => "asc",
                        "unit" => $distance_units,
                        "distance_type" => "plane"
                    ]
                ]

            ];

        }

(system) #2