# How to query in condition of length of string fields and the field ending with "99"

**URL:** https://discuss.elastic.co/t/how-to-query-in-condition-of-length-of-string-fields-and-the-field-ending-with-99/168528
**Category:** Elasticsearch
**Created:** [February 15, 2019, 6:38am UTC](https://discuss.elastic.co/t/how-to-query-in-condition-of-length-of-string-fields-and-the-field-ending-with-99/168528 "2019-02-15T06:38:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kennymarx0](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@kennymarx0](https://discuss.elastic.co/u/kennymarx0)
#### Post date: [February 15, 2019, 6:38am UTC](https://discuss.elastic.co/t/how-to-query-in-condition-of-length-of-string-fields-and-the-field-ending-with-99/168528/1 "2019-02-15T06:38:52Z")

</div>

there is a error when using the query below:

> GET trxdetail-fj/\_search  
> {  
> "query": {  
> "script": {  
> "script": {  
> "source": "doc['OPPACC'].length()\>10",  
> "lang": "painless"  
> }  
> }  
> }  
> }

errror message:

> {  
> "error": {  
> "root\_cause": [  
> {  
> "type": "query\_shard\_exception",  
> "reason": "failed to create query: {\n "script" : {\n "script" : {\n "source" : "doc['OPPACC'].length()\>10",\n "lang" : "painless"\n },\n "boost" : 1.0\n }\n}",  
> "index\_uuid": "n1UUebJaT7WJiomMDjbCXw",  
> "index": "trxdetail-fj"  
> }  
> ],  
> "type": "search\_phase\_execution\_exception",  
> "reason": "all shards failed",  
> "phase": "query",  
> "grouped": true,  
> "failed\_shards": [  
> {  
> "shard": 0,  
> "index": "trxdetail-fj",  
> "node": "KNkvY1LsSQ-VA61GpNNWkA",  
> "reason": {  
> "type": "query\_shard\_exception",  
> "reason": "failed to create query: {\n "script" : {\n "script" : {\n "source" : "doc['OPPACC'].length()\>10",\n "lang" : "painless"\n },\n "boost" : 1.0\n }\n}",  
> "index\_uuid": "n1UUebJaT7WJiomMDjbCXw",  
> "index": "trxdetail-fj",  
> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "cannot execute scripts using [filter] context"  
> }  
> }  
> }  
> ]  
> },  
> "status": 400  
> }

and my config is :

> ```
> # ---------------------------------- script -----------------------------------
> script.allowed_types: inline
> script.allowed_contexts: search,update,aggs
> 
> ```

finally, i want to search in my index to find some record which is ending with '99' and the length of field is 12.

for example, one of records is "347897109899" in the field naming "oppacc" which match the condition.

how to do it ?

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 15, 2019, 8:10am UTC](https://discuss.elastic.co/t/how-to-query-in-condition-of-length-of-string-fields-and-the-field-ending-with-99/168528/2 "2019-02-15T08:10:49Z")

</div>

While you could solve this with a script, the way you currently wrote this implies, that **every hit** from your query will be run against a script. This might work with a low number of results, but will be painfully slow with a already medium number of documents. Also it will be linearly slower with the number of documents.

Another possible solution is to either take the prefix and suffix during indexing and store them in own fields, so that you just have to execute term queries. If you do not want to change the JSON, you should check out the [ngram tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/analysis-ngram-tokenizer.html) which stores part of the terms.

Hope this helps!

--Alex

---

<div class="post-metadata">

### Author: ![kennymarx0](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@kennymarx0](https://discuss.elastic.co/u/kennymarx0)
#### Post date: [February 15, 2019, 9:25am UTC](https://discuss.elastic.co/t/how-to-query-in-condition-of-length-of-string-fields-and-the-field-ending-with-99/168528/3 "2019-02-15T09:25:11Z")

</div>

thanks for reply. actually, i just want to know how to use script in query, and how to use more complicate script for filtering result.

---

<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: [March 15, 2019, 9:25am UTC](https://discuss.elastic.co/t/how-to-query-in-condition-of-length-of-string-fields-and-the-field-ending-with-99/168528/4 "2019-03-15T09:25:11Z")

</div>

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