# Trying to use function\_score to create a compromise between location and price

**URL:** https://discuss.elastic.co/t/trying-to-use-function-score-to-create-a-compromise-between-location-and-price/50750
**Category:** Elasticsearch
**Created:** [May 23, 2016, 6:04pm UTC](https://discuss.elastic.co/t/trying-to-use-function-score-to-create-a-compromise-between-location-and-price/50750 "2016-05-23T18:04:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![111106](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111106/32/6665_2.png) [@111106](https://discuss.elastic.co/u/111106)
#### Post date: [May 23, 2016, 6:04pm UTC](https://discuss.elastic.co/t/trying-to-use-function-score-to-create-a-compromise-between-location-and-price/50750/1 "2016-05-23T18:04:10Z")

</div>

I've been beating myself over how to factor both the location and the price into the scoring process. The idea is to display objects that are, in the first case, both close and lowly priced, and, in the second case, both close and highly priced.

At this point, I honestly have no idea. This is my latest attempt at building a query (of course it doesn't do what I need), but that's pretty much what I've been toying around with.

```
> { query: 
> { bool: 
> { must: 
> [ { function_score: 
> { score_mode: 'multiply',
> boost_mode: 'replace',
> query: { match_all: {} },
> functions: 
> [ { script_score: 
> { script: 'doc[\'price\'].value != 1 ? (_score * ((1/doc[\'price\'].value))) : 0' } }, // check if the value is absent, since it returns 1 in that case
> { gauss: 
> { location: 
> { origin: { lat: 50.00, lon: 6.00 },
> scale: '10m',
> offset: '0m',
> decay: 0.99999 } } },
> [length]: 2 ] } },
> [length]: 1 ],
> filter: [{ bool: {} }, [length]: 1 ] } },
> size: 30,
> from: 0,
> _source: ['id', '_score', 'location', [length]: 3 ],
> script_fields: 
> { distance: 
> { script: 'doc["location"].arcDistance(lat,lon)',
> params: { lat: 50.00, lon: 6.00 } } } }

```

An indexed document looks like this:

```
> {
> location: {
> lat: 50.00,
> lon: 50.00
> },
> price: 550.00,
> name: 'Test',
> rating: 5.00,
> }
```

---

<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, 10:49pm UTC](https://discuss.elastic.co/t/trying-to-use-function-score-to-create-a-compromise-between-location-and-price/50750/2 "2017-07-05T22:49:44Z")

</div>


