# Performance of NativeScripts

**URL:** <https://discuss.elastic.co/t/performance-of-nativescripts/40017>\
**Category:** Elasticsearch\
**Created:** [January 25, 2016, 11:31am UTC](https://discuss.elastic.co/t/performance-of-nativescripts/40017 "2016-01-25T11:31:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmitriy\_olshanskiy](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dmitriy\_olshanskiy](https://discuss.elastic.co/u/dmitriy_olshanskiy)\
**Post date:** [January 25, 2016, 11:31am UTC](https://discuss.elastic.co/t/performance-of-nativescripts/40017/1 "2016-01-25T11:31:03Z")

</div>

Is it expected to have script-based sorting much slower then field-based sorting? (x4 times slower then field-based in my tests) Is there any fundamental performance problem in processing - i.e. why the limitation to one script per request?

Using the following native script for testing:

```auto
public class RealPrice extends AbstractFloatSearchScript {
	public static class Factory implements NativeScriptFactory {

        /**
         * This method is called for every search on every shard.
         *
         * @param params list of script parameters passed with the query
         * @return new native script
         */
        @Override
        public ExecutableScript newScript(@Nullable Map<String, Object> params) {
            return new RealPrice();
        }
    }
	@Override
	public float runAsFloat() {
		Doubles price = (Doubles)doc().get("facet_price");
		return Math.abs(10000 - price.get(0).floatValue());
	}

}

```

Running ~5k queries of the form for 15min:

```auto
{
  "explain": false,
  "fields": [
    "sku",
    "facet_inStock",
    
    "score"
  ],
  "from": 0,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": {
            "term": {
              "searchable": "1"
            }
          },
          "must_not": [
            {
              "term": {
                "product_invisible": "1"
              }
            },
            {
              "terms": {
                "product_visibility": [
                  "1",
                  "3"
                ]
              }
            }
          ]
        }
      },
      "query": {
        "bool": {
          "boost": 2,
          "minimum_should_match": "1",
          "should": [
            {
              "multi_match": {
                "fields": [
                  "attributes_brand_3^2.000000",
                  "category^2.000000",
                  "name^1.500000",
                  "attributes_author_131^1.000000",
                  "keywords_field^2.500000",
                  "skus^5.000000",
                ],
                "minimum_should_match": "2<-1 5<80%",
                "query": "iphone 6 4.7 case",
                "slop": 20,
                "tie_breaker": 1,
                "type": "cross_fields",
                "use_dis_max": true
              }
            },
            {
              "multi_match": {
                "fields": [
                  "name^1.500000",
                  "category^2.000000",
                  "attributes_brand_3^2.000000",
                  "keywords_field^2.500000",
                  "attributes_author_131^1.000000",
                  "skus^5.000000",
                ],
                "operator": "AND",
                "query": "iphone 6 4.7 case",
                "slop": 20,
                "tie_breaker": 1,
                "type": "phrase",
                "use_dis_max": true
              }
            }
          ]
        }
      }
    }
  },
  "size": 20,
  "sort": {
     "_script" : {
          "type" : "number",
          "script" : {
              "lang": "native",
              "script": "real_price"
          }
      }
  }
}

```

---

<div class="post-metadata">

**Author:** ![bleskes](https://avatars.discourse-cdn.com/v4/letter/b/71c47a/32.png) [@bleskes](https://discuss.elastic.co/u/bleskes)\
**Post date:** [January 25, 2016, 12:55pm UTC](https://discuss.elastic.co/t/performance-of-nativescripts/40017/2 "2016-01-25T12:55:27Z")

</div>

Native field sorting has direct access to internal data structures. Using scripting comes with an overhead (even if it's a native script). See for example your line `Doubles price = (Doubles)doc().get("facet_price");` - this involves much more than what an internal code would do. All of that said, scripting are flexible and useful. If the current speed is not acceptable and you need script, considering using more shards increase the parallelism of the search (and giving it more resources)

---

<div class="post-metadata">

**Author:** ![dmitriy\_olshanskiy](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dmitriy\_olshanskiy](https://discuss.elastic.co/u/dmitriy_olshanskiy)\
**Post date:** [February 10, 2016, 12:51pm UTC](https://discuss.elastic.co/t/performance-of-nativescripts/40017/3 "2016-02-10T12:51:30Z")

</div>

Interestingly I observe that performance degradation is noticeable only with scripted sorting, but not with function\_score. Any ideas on what may be the cause? Posted here:

> [@Function\_score script\_score query vs sort script performance](https://discuss.elastic.co/t/function-score-script-score-query-vs-script-sorting-performance/41377):
>
> I've found an order of magnitude difference in performance of queries that use function\_score with script-based score vs queries that use sort with the same script. What could cause this difference ? Does it make sense to stick to creative use of custom\_score for sorting? (I'm using ES 1.7.4) Example queries: { "explain": false, "fields": ["sku"], "from": 0, "query": { "filtered": { "filter": { "bool": { "must": { "term": { …

---

<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, 11:17pm UTC](https://discuss.elastic.co/t/performance-of-nativescripts/40017/4 "2017-07-05T23:17:36Z")

</div>


