# Problems with Code

**URL:** <https://discuss.elastic.co/t/problems-with-code/22570>\
**Category:** Elasticsearch\
**Created:** [March 7, 2015, 3:29am UTC](https://discuss.elastic.co/t/problems-with-code/22570 "2015-03-07T03:29:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cliff\_S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cliff_s/32/840_2.png) [@Cliff\_S](https://discuss.elastic.co/u/Cliff_S)\
**Post date:** [March 7, 2015, 3:29am UTC](https://discuss.elastic.co/t/problems-with-code/22570/1 "2015-03-07T03:29:10Z")

</div>

I have setup a page that calculates the total number of points that a user  
has on a server. I am not trying to convert that to a level number. Here is  
what I have but it is not working. Any help of ideas would be great. I just  
cant seem to wrap my head around this. Thanks.

PlayerScore.score is calculated before on the page and works correctly.  
This will not seem to work at all.  
"script\_fields": {  
"PlayerScore.level": {  
"script": "if (doc['PlayerScore.score'].value \<= 100) {  
(doc['PlayerScore.level'].value == 1) }else (doc['PlayerScore.score'].value  
\<= 300) { (doc['PlayerScore.level'].value == 2) }  
else (doc['PlayerScore.score'].value \<= 500) {  
(doc['PlayerScore.level'].value == 3) }  
else (doc['PlayerScore.score'].value \<= 900) {  
(doc['PlayerScore.level'].value == 4) }  
else (doc['PlayerScore.score'].value \<= 1400) {  
(doc['PlayerScore.level'].value == 5) }  
else (doc['PlayerScore.score'].value \<= 2000) {  
(doc['PlayerScore.level'].value == 6) }  
else (doc['PlayerScore.score'].value \<= 2700) {  
(doc['PlayerScore.level'].value == 7) }  
else (doc['PlayerScore.score'].value \<= 3500) {  
(doc['PlayerScore.level'].value == 8) }  
else (doc['PlayerScore.score'].value \<= 5000) {  
(doc['PlayerScore.level'].value == 9) }  
else (doc['PlayerScore.score'].value \>= 7500) {  
(doc['PlayerScore.level'].value == 10) }"  
}  
},

Here is where I am adding it.

\<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Leaderboard\_model extends CI\_Model { function \_\_construct() { parent::\_\_construct(); } function get($serverId = NULL) { $a3servers = $this-\>config-\>item("a3w\_servers"); if (!isset($a3servers)) { return new stdClass(); } $serverConfig = NULL; if (!is\_string($serverId)) { $serverConfig = reset($a3servers); } else if(array\_key\_exists($serverId,$a3servers)){ $serverConfig = $a3servers[$serverId]; } if (!isset($serverConfig)) { return new stdClass(); } $query = \<\<\<EOF { "from": 0, "size": 1000, "fields": ["PlayerInfo.Name", "PlayerScore.playerKills", "PlayerScore.aiKills", "PlayerScore.deathCount", "PlayerScore.reviveCount", "PlayerScore.captureCount", "updatedAt\_"], "script\_fields": { "PlayerScore.kdr": { "script": "if (doc['PlayerScore.deathCount'].value == 0) { doc['PlayerScore.playerKills'].value } else { Math.round(100 \* (doc['PlayerScore.playerKills'].value / doc['PlayerScore.deathCount'].value))/100}" } }, "script\_fields": { "PlayerScore.score": { "script": "if (doc['PlayerScore.deathCount'].value == 0) { doc['PlayerScore.playerKills'].value } else { Math.round(2 \* doc['PlayerScore.playerKills'].value + doc['PlayerScore.aiKills'].value + (5 \* doc['PlayerScore.captureCount'].value))}" } }, "sort": { "PlayerScore.playerKills": { "order": "desc" } }, "filter": { "and": [{ "exists": { "field": "PlayerScore.playerKills" } }, { "exists": { "field": "PlayerScore.playerKills" } }, { "range": { "updatedAt\_": { "gt": "now-1M" } } }] } } EOF; $page = trim($serverConfig["ES\_Players\_Index"], "/") . "/\_search"; $ch = curl\_init(); curl\_setopt($ch, CURLOPT\_URL, $page); curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($ch, CURLOPT\_POST, 1); curl\_setopt($ch, CURLOPT\_POSTFIELDS, $query); curl\_setopt($ch, CURLOPT\_HTTPHEADER, array('Content-Type: application/json')); curl\_setopt($ch, CURLOPT\_HTTPHEADER, array('Accept: application/json')); $json= curl\_exec($ch); curl\_close ($ch); if (!is\_string($json)) { return new stdClass(); } $data = json\_decode($json); if (!is\_object($data)) { return new stdClass(); } return $data; } } -- You received this message because you are subscribed to the Google Groups "elasticsearch" group. To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/1e764a21-bfb1-429c-9bde-874e8135bbf9%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

---

<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 6, 2017, 12:27am UTC](https://discuss.elastic.co/t/problems-with-code/22570/2 "2017-07-06T00:27:53Z")

</div>

Closed on July 6, 2017, 12:27am UTC


