# Need help In Function Scoring

**URL:** https://discuss.elastic.co/t/need-help-in-function-scoring/55825
**Category:** Elasticsearch
**Created:** [July 19, 2016, 6:17am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825 "2016-07-19T06:17:18Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Nisar\_Adappadathil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nisar_adappadathil/32/13224_2.png) [@Nisar\_Adappadathil](https://discuss.elastic.co/u/Nisar_Adappadathil)
#### Post date: [July 19, 2016, 6:17am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/1 "2016-07-19T06:17:19Z")

</div>

POST: [http://localhost:9200/myindex/type/\_search](http://localhost:9200/myindex/type/_search)  
{  
"query": {  
"function\_score": {  
"functions": [  
{  
"gauss": {  
"b": {  
"origin": "0",  
"scale": "1000"  
}  
}, "weight":"2"  
}  
],  
"query": {  
"match": {"b":656}  
},  
"score\_mode": "multiply"  
}  
}  
}

I have 2 fields in my doc a:string and b:number  
So I was trying various ways to change scoring pattern.

Requirement: I need to have the score based on only coord and not on tf-idf.  
Example: If my query is "location":"bangalore" OR "location":"chennai" OR "location":"mumbai"  
So If a record contains all the matches, it should come on top, then a document with less number of matches like that.

So the scoring should respect coord only and not tf-idf. How can i achieve this.  
From lucene documentation this is the scoring logic:  
score(q,d) = coord(q,d) · queryNorm(q) · ∑( tf(t in d)· idf(t)2 · t.getBoost()· norm(t,d))

---

<div class="post-metadata">

### Author: ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)
#### Post date: [July 19, 2016, 7:05am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/2 "2016-07-19T07:05:34Z")

</div>

Hi,  
If your list of location is static you can go with a hand made field location\_score and you can set points if the location match.  
In your case if your document have the location "bangalore" set the location\_score to 2.  
i.e set the scoring logic before saving your document, so on query you only need to sort.

---

<div class="post-metadata">

### Author: ![Nisar\_Adappadathil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nisar_adappadathil/32/13224_2.png) [@Nisar\_Adappadathil](https://discuss.elastic.co/u/Nisar_Adappadathil)
#### Post date: [July 19, 2016, 9:00am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/3 "2016-07-19T09:00:00Z")

</div>

Thanks for your help.  
In my scenario its the number of matches which i am taking into consideration

If my document has location bangalore,chennai,mumbai the there are 3 matches..It should come on the top.

If my document has location bangalore,chennai the there are 2 matches..It should come below the one with 3 matches..  
Its like this  
Is there a way to do this.

---

<div class="post-metadata">

### Author: ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)
#### Post date: [July 19, 2016, 10:08am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/4 "2016-07-19T10:08:31Z")

</div>

Hi,

I don't know enough about your data, but I can guess if you have documents like this ones:  
{"id": 1, "location": ["bangalore", "chennai"], ...}  
{"id": 2, "location": ["bangalore", "bollywood"], ...}  
{"id": 3, "location": ["bollywood"], ...}

before saving your document you need to check your location and add points so it will give:

{"id": 1, "location": ["bangalore", "chennai"], "location\_score": 4} \<--- 4 because 2 points by correct location  
{"id": 2, "location": ["bangalore", "bollywood"], "location\_score": 2} \<--- 2 because there's only one good location  
{"id": 3, "location": ["bollywood"], "location\_score": 0} \<--- 0 because bollywood is not in the list of good location

So if you sort by "location\_score" you'll get your data in the correct order.

---

<div class="post-metadata">

### Author: ![Nisar\_Adappadathil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nisar_adappadathil/32/13224_2.png) [@Nisar\_Adappadathil](https://discuss.elastic.co/u/Nisar_Adappadathil)
#### Post date: [July 19, 2016, 10:36am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/5 "2016-07-19T10:36:05Z")

</div>

**my query is "location":"bangalore" OR "location":"chennai" OR "location":"mumbai"**

I can't put a new field in my document (location\_score) because the number of match i am saing is based on the query.  
In this case how many conditions of the query is matched with the document.

If my document is like:  
Doc21: { location :["bangalore","chennai","mumbai","london"] } // for the given query there are 3 matches

for document Doc2:{ location :["bangalore","chennai","berlin","paris"] }// for the given query there are 2 matches

so here the scoring should be based on the number of matches. So Doc1 will come above Doc2.

If my query was "location":"bangalore" OR "location":"chennai" OR "location":"berlin"  
here Doc2 will come on top.

This is the requirement.

---

<div class="post-metadata">

### Author: ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)
#### Post date: [July 20, 2016, 3:44am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/6 "2016-07-20T03:44:03Z")

</div>

Hi,

Does this match your requirements:

# create the docuemnts

```
curl -XPOST 127.0.0.1:9200/test_city/city_score/1 -d '{"id":1, "location":["bangalore", "paris", "mumbai"]}'
curl -XPOST 127.0.0.1:9200/test_city/city_score/2 -d '{"id":2, "location":["bangalore", "paris"]}'

```

# terms query with sort on \_score

`curl -XPOST 127.0.0.1:9200/test_city/city_score/_search -d '{"query":{"terms":{"location":["bangalore":mumbai]}}, "sort":"_score"}'`

it return:  
`{"took":10,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":2,"max_score":0.581694,"hits":[{"_index":"test_city","_type":"city_score","_id":"1","_score":0.581694, "_source" : {"id":1, "location":["bangalore", "paris", "mumbai"]}},{"_index":"test_city","_type":"city_score","_id":"2","_score":0.09494676, "_source" : {"id":2, "location":["bangalore", "paris"]}}]}}`

Doc 1 have a better score: 0.581694  
than doc2 0.09494676

---

<div class="post-metadata">

### Author: ![Nisar\_Adappadathil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nisar_adappadathil/32/13224_2.png) [@Nisar\_Adappadathil](https://discuss.elastic.co/u/Nisar_Adappadathil)
#### Post date: [July 20, 2016, 1:45pm UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/7 "2016-07-20T13:45:00Z")

</div>

This will not work every time.

Doc1 : "location": ["bangalore", "paris", "mumbai", "kolkata"]  
Doc2 : "location": ["bangalore", "paris","mumbai"]  
Doc3 : "location": ["bangalore", "paris", "mumbai","calicut"]  
Doc4 : "location": ["bangalore","paris"]  
Doc5 : "location": ["bangalore","paris","bangalore","bangalore"]  
Doc6 : location": ["bangalore", "mumbai", "kolkata"]

I have inserted these data.

Queried for this : {"query":{"terms":{"location":["bangalore","mumbai","calicut","kolkata","paris"] } }, "sort":"\_score"}

here the doc with 3 matches came above one with 4 matches.  
Here Doc2 came above Doc3.

Result I got:  
{  
"took": 4,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 5,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "test\_city",  
"\_type": "city\_score",  
"\_id": "5",  
"\_score": 1,  
"\_source": {  
"location": [  
"bangalore",  
"paris",  
"mumbai",  
"kolkata"  
]  
}  
},  
{  
"\_index": "test\_city",  
"\_type": "city\_score",  
"\_id": "1",  
"\_score": 1,  
"\_source": {  
"id": 1,  
"location": [  
"bangalore",  
"paris",  
"mumbai"  
]  
}  
},  
{  
"\_index": "test\_city",  
"\_type": "city\_score",  
"\_id": "6",  
"\_score": 1,  
"\_source": {  
"location": [  
"bangalore",  
"paris",  
"mumbai",  
"calicut"  
]  
}  
},  
{  
"\_index": "test\_city",  
"\_type": "city\_score",  
"\_id": "2",  
"\_score": 1,  
"\_source": {  
"id": 2,  
"location": [  
"bangalore",  
"paris"  
]  
}  
},  
{  
"\_index": "test\_city",  
"\_type": "city\_score",  
"\_id": "7",  
"\_score": 1,  
"\_source": {  
"location": [  
"bangalore",  
"paris",  
"bangalore",  
"bangalore"  
]  
}  
}  
]  
}  
}

---

<div class="post-metadata">

### Author: ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)
#### Post date: [July 22, 2016, 6:13am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/8 "2016-07-22T06:13:21Z")

</div>

There's something strange in your result all your \_score are equal to 1???  
in my test the score are different : \_score":0.581694", "\_score":0.09494676!!

---

<div class="post-metadata">

### Author: ![Nisar\_Adappadathil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nisar_adappadathil/32/13224_2.png) [@Nisar\_Adappadathil](https://discuss.elastic.co/u/Nisar_Adappadathil)
#### Post date: [July 22, 2016, 6:19am UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/9 "2016-07-22T06:19:45Z")

</div>

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html)

I have used constant\_query which says it'll ignore tf-idf and consider only coord.

I need to ignore this tf-idf some how and consider only coord.

---

<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:33pm UTC](https://discuss.elastic.co/t/need-help-in-function-scoring/55825/10 "2017-07-05T22:33:32Z")

</div>


