# Correct way to construct PHP function\_score for gauss -\> location?

**URL:** <https://discuss.elastic.co/t/correct-way-to-construct-php-function-score-for-gauss-location/44382>\
**Category:** Elasticsearch\
**Created:** [March 15, 2016, 12:37am UTC](https://discuss.elastic.co/t/correct-way-to-construct-php-function-score-for-gauss-location/44382 "2016-03-15T00:37:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonHead](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@JasonHead](https://discuss.elastic.co/u/JasonHead)\
**Post date:** [March 15, 2016, 12:37am UTC](https://discuss.elastic.co/t/correct-way-to-construct-php-function-score-for-gauss-location/44382/1 "2016-03-15T00:37:51Z")

</div>

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/sorting-by-distance.html#scoring-by-distance)  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html)

and also found this:

> <https://github.com/elastic/elasticsearch-php/issues/47>

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"
                    ]
                ]

            ];

        }
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:08pm UTC](https://discuss.elastic.co/t/correct-way-to-construct-php-function-score-for-gauss-location/44382/2 "2017-07-05T23:08:13Z")

</div>


