# Script for search rank (script\_score)

**URL:** https://discuss.elastic.co/t/script-for-search-rank-script-score/72761
**Category:** Elasticsearch
**Created:** [January 25, 2017, 11:04am UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761 "2017-01-25T11:04:05Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![fix2015](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fix2015](https://discuss.elastic.co/u/fix2015)
#### Post date: [January 25, 2017, 11:04am UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/1 "2017-01-25T11:04:05Z")

</div>

hey guys, i have pb, i have field  
rank = {  
0: 'js',  
1: php  
...  
}  
i need order so i decide use script\_score and write this code  
script\_score{  
script = (1 + (doc['rank'].size() \> 0 ? doc['rank'].values.find{it =="js"} ? 1 : 0 : 0));  
}  
but its not help can you help me how find value in arrys in elasticsearch???

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [January 25, 2017, 11:19am UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/2 "2017-01-25T11:19:46Z")

</div>

Script might not be the best way to achieve what you're after.

Can you afford to reindex your content? If so we can consider some faster solutions

---

<div class="post-metadata">

### Author: ![fix2015](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fix2015](https://discuss.elastic.co/u/fix2015)
#### Post date: [January 25, 2017, 11:49am UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/3 "2017-01-25T11:49:04Z")

</div>

i know but i need this, because all project already use it, and i need just do order so i need this way

---

<div class="post-metadata">

### Author: ![fix2015](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fix2015](https://discuss.elastic.co/u/fix2015)
#### Post date: [January 25, 2017, 11:49am UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/4 "2017-01-25T11:49:36Z")

</div>

what do you mean reindex content?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [January 25, 2017, 11:51am UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/5 "2017-01-25T11:51:23Z")

</div>

> [@fix2015](#):
>
> what do you mean reindex content?

Reorganise your data into a new index with a more appropriate document format/mapping.

---

<div class="post-metadata">

### Author: ![fix2015](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fix2015](https://discuss.elastic.co/u/fix2015)
#### Post date: [January 25, 2017, 12:03pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/6 "2017-01-25T12:03:01Z")

</div>

no i can't i have not access to this, but i need change script for order data

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [January 25, 2017, 12:08pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/7 "2017-01-25T12:08:31Z")

</div>

> [@fix2015](#):
>
> i have not access to this

If you already have an index you have a source of data from which you can reindex.

Never mind, let's focus on your script for now and what you want to achieve with the existing index.  
Looking at the code it seems like you're returning a 1 if you find "js" in the array or or zero if not. Given the field is called "rank" and you're talking about order I presume you want to return a score that is related to _the position in the array_ rather than a simple zero or a one?

---

<div class="post-metadata">

### Author: ![fix2015](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fix2015](https://discuss.elastic.co/u/fix2015)
#### Post date: [January 25, 2017, 1:16pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/8 "2017-01-25T13:16:37Z")

</div>

i have around 5 type of rank, i want write script that will add some number for all category for example  
if js put 5  
it php put 4  
if python put 3  
and then order by \_score

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [January 25, 2017, 1:53pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/9 "2017-01-25T13:53:47Z")

</div>

If the query wants to rank certain skills higher than others,regardless of array position, there's no need for scripts:

```
DELETE test
PUT test
{
   "settings": {
	  "index": {
		 "number_of_shards": 1
	  }    
   },
   "mappings": {
	  "person": {
		 "properties": {
			"skills": {
			   "type": "keyword"
			},
			"name": {
			   "type": "keyword"
			}

		 }
	  }
   }
}
POST test/person
{
	"name":"Mr Webdev",
	"skills":["html", "js"]
}
POST test/person
{
	"name":"Mr PHP Webdev",
	"skills":["php", "js"]
}
POST test/person
{
	"name":"Monty",
	"skills":["python", "flask"]
}

GET test/person/_search
{
   "query": {
	  "bool": {
		 "should": [
			{
			   "constant_score": {
				  "filter": {
					 "term": { "skills": "js" }
				  },
				  "boost": 5
			   }
			},
			{
			   "constant_score": {
				  "filter": { 
					 "term": { "skills": "php" }
				  },
				  "boost": 4
			   }
			},
			{
			   "constant_score": {
				  "filter": {
					 "term": { "skills": "python"}
				  },
				  "boost": 3
			   }
			}
		 ]
	  }
   }
}
```

---

<div class="post-metadata">

### Author: ![fix2015](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fix2015](https://discuss.elastic.co/u/fix2015)
#### Post date: [January 25, 2017, 2:00pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/10 "2017-01-25T14:00:46Z")

</div>

yes tried this way, but you know i have pb, that i need order not only for rank field, i have some other field like experience, and country and other and they should they should be considered too,

---

<div class="post-metadata">

### Author: ![fix2015](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fix2015](https://discuss.elastic.co/u/fix2015)
#### Post date: [January 25, 2017, 2:02pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/11 "2017-01-25T14:02:50Z")

</div>

so order should be like this:  
first show - rank 'js'  
2 -rank php  
3 - country - USA  
4 - experience 2-3 years  
5 - experience1 years  
6 - rank - html

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [January 25, 2017, 2:10pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/12 "2017-01-25T14:10:08Z")

</div>

Check out function score [1]. That's the way to assemble combinations of multiple matching rules with different boosts and score-blending modes.

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

---

<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: [February 22, 2017, 2:10pm UTC](https://discuss.elastic.co/t/script-for-search-rank-script-score/72761/13 "2017-02-22T14:10:44Z")

</div>

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