# Query function script score if multiple values in a field

**URL:** https://discuss.elastic.co/t/query-function-script-score-if-multiple-values-in-a-field/144181
**Category:** Elasticsearch
**Created:** [August 13, 2018, 1:41pm UTC](https://discuss.elastic.co/t/query-function-script-score-if-multiple-values-in-a-field/144181 "2018-08-13T13:41:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![atom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atom/32/34758_2.png) [@atom](https://discuss.elastic.co/u/atom)
#### Post date: [August 13, 2018, 1:41pm UTC](https://discuss.elastic.co/t/query-function-script-score-if-multiple-values-in-a-field/144181/1 "2018-08-13T13:41:52Z")

</div>

Hi,  
I have a question about creating the following query:  
My field _location_ can store multiple values in it, e.g.:

```
"geolocations": [
     "51.2171728, 8.654843",
     "50.2171728, 8.645487",
]

```

Is it possible to increase or decrease the score?

I want a higher score, if there is only one element in it and a lower score the more elements are in it.

The last hour i spend searching for a solution in the net and documentations but did not found anything.

Maybe using a functions with script\_score? Appreciate any help 🙂

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [August 15, 2018, 2:55am UTC](https://discuss.elastic.co/t/query-function-script-score-if-multiple-values-in-a-field/144181/2 "2018-08-15T02:55:49Z")

</div>

Yes, a function\_score query with a script score function would work. Something like this:

```auto
GET test/_search
{
  "query": {
    "function_score": {
      "query": {
        ***YOUR REGULAR QUERY GOES HERE***
      },
      "functions": [
        {
          "script_score": {
            "script": "if (doc['geolocations'].values.length > 0) { return 1.0 / (doc['geolocations'].values.length) } else { return 0 }"
          }
        }
      ]
    }
  }
}

```

You could also add a field to your documents at index time that indicates how many geolocations there are. Then you could use that number in a function\_score [field value factor](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor) function, and you would not have to use scripting.

---

<div class="post-metadata">

### Author: ![atom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atom/32/34758_2.png) [@atom](https://discuss.elastic.co/u/atom)
#### Post date: [August 15, 2018, 6:31am UTC](https://discuss.elastic.co/t/query-function-script-score-if-multiple-values-in-a-field/144181/3 "2018-08-15T06:31:44Z")

</div>

Hi Abdon,  
thank you so much for the script 🙂  
But i think your 2nd idea is even better 😃

---

<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: [September 12, 2018, 6:31am UTC](https://discuss.elastic.co/t/query-function-script-score-if-multiple-values-in-a-field/144181/4 "2018-09-12T06:31:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
