# Filter Search by Scripted field elastic

**URL:** <https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139>\
**Category:** Elasticsearch\
**Created:** [February 6, 2020, 10:23am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139 "2020-02-06T10:23:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![umay\_fb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umay_fb/32/60582_2.png) [@umay\_fb](https://discuss.elastic.co/u/umay_fb)\
**Post date:** [February 6, 2020, 10:23am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/1 "2020-02-06T10:23:51Z")

</div>

i have create a scripted field query search and filter it. but the hits i founds is zero. and show the error like :

#! Deprecation: Deprecated field [inline] used, expected [source] instead  
{  
"took": 1542,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": {  
"value": 0,  
"relation": "eq"  
},  
"max\_score": null,  
"hits":  
}  
}

this the script i create and i use query term to get data.  
"script\_fields": {  
"date": {  
"script": {  
"lang": "painless",  
"inline": "if (doc['status'].size() \<= 0) {return 'IN PROGRESS';}def status = doc['status'].value;if(status == 0) {return 'IN PROGRESS';}else if(status == 1) {return 'DELIVERED';}else if(status == 2) {return 'LOST/BROKEN';}else if(status == 3) {return 'RETURNED';}else if(status == 99) {return 'CANCELLED';}else{return 'FAILED';}"  
}

can i get the result?

---

<div class="post-metadata">

**Author:** ![umay\_fb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umay_fb/32/60582_2.png) [@umay\_fb](https://discuss.elastic.co/u/umay_fb)\
**Post date:** [February 6, 2020, 10:24am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/2 "2020-02-06T10:24:23Z")

</div>

@flash1293

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 6, 2020, 10:32am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/3 "2020-02-06T10:32:56Z")

</div>

The most obvious thing would be to use the `source` property instead of `inline` (as it says in the response). If there are still problems, could you post the complete request you are sending together with the mapping of your index and an example document that should show up in your search?

---

<div class="post-metadata">

**Author:** ![umay\_fb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umay_fb/32/60582_2.png) [@umay\_fb](https://discuss.elastic.co/u/umay_fb)\
**Post date:** [February 6, 2020, 10:39am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/4 "2020-02-06T10:39:15Z")

</div>

its the script i create. i want filter using the result of script\_fields.  
{  
"query": {  
"term": {  
"Delivstatus[0]": "DELIVERED"  
}  
},  
"\_source": ,  
"script\_fields": {  
"Delivstatus": {  
"script": {  
"lang": "painless",  
"inline": "if (doc['status'].size() \<= 0) {return 'IN PROGRESS';}def status = doc['status'].value;if(status == 0) {return 'IN PROGRESS';}else if(status == 1) {return 'DELIVERED';}else if(status == 2) {return 'LOST/BROKEN';}else if(status == 3) {return 'RETURNED';}else if(status == 99) {return 'CANCELLED';}else{return 'FAILED';}"  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 6, 2020, 11:01am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/5 "2020-02-06T11:01:45Z")

</div>

Can you please also provide the things requested in my latest post? You can black out sensitive information as field names or actual values, but I'm not able to help you with partial information about your situation

---

<div class="post-metadata">

**Author:** ![umay\_fb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umay_fb/32/60582_2.png) [@umay\_fb](https://discuss.elastic.co/u/umay_fb)\
**Post date:** [February 6, 2020, 11:23am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/6 "2020-02-06T11:23:41Z")

</div>

for example i get data :  
{  
"mapping": {  
"properties": { "status": {  
"type": "long"  
},  
"Date": {  
"type": "date"  
},  
"dest": {  
"type": "string"  
},  
"origin": {  
"type": "string"  
},  
"Price": {  
"type": "long"  
}}}

and data like :

| Status | Date | dest | origin | price |
| --- | --- | --- | --- | --- |
| 1 | 23-Jan-20 | Jakarta | Jakarta | 100000 |
| 0 | 23-Jan-20 | Surabaya | Jakarta | 20000 |
| 99 | 23-Jan-20 | Bandung | Jakarta | 13000 |
| 0 | 23-Jan-20 | Bandung | Jakarta | 15500 |
| 0 | 23-Jan-20 | Surabaya | Medan | 166500 |
| 1 | 23-Jan-20 | Jakarta | Medan | 110000 |
| 1 | 23-Jan-20 | Jakarta | Jakarta | 200000 |

and i using this script :  
{  
"query": {  
"term": {  
"SameCity": "yes"  
}

```
},
"_source": [],
"script_fields": {
    "SameCity": {
        "script": {
            "lang": "painless",
            "inline": "if (doc['dest'].size() <= 0 || doc['origin'].size() <= 0) {return 'Fail';}def dest = doc['dest'].value;def ori = doc['origin'].value;if(ori == dest) {return 'yes';}else {return 'no';}"
        }
    }
}

```

}

to get data :

| Status | Date | dest | origin | price | Same City |
| --- | --- | --- | --- | --- | --- |
| 1 | 23-Jan-20 | Jakarta | Jakarta | 100000 | yes |
| 1 | 23-Jan-20 | Jakarta | Jakarta | 200000 | yes |

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 6, 2020, 2:23pm UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/7 "2020-02-06T14:23:24Z")

</div>

If you just want to filter by a script, you can use a script within the query clause like this:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html)

```auto
PUT script_index
PUT script_index/_mapping
{
  
    "properties": {
      "status": {
        "type": "long"
      },
      "Date": {
        "type": "date"
      },
      "dest": {
        "type": "keyword"
      },
      "origin": {
        "type": "keyword"
      },
      "Price": {
        "type": "long"
      }
    }
}

PUT script_index/_doc/1
{ "status": 1, "Date": "2020-01-23", "dest": "Jakarta", "origin": "Jakarta", "Price": 100000 }

PUT script_index/_doc/2
{ "status": 0, "Date": "2020-01-23", "dest": "Surabaya", "origin": "Jakarta", "Price": 20000 }

PUT script_index/_doc/3
{ "status": 99, "Date": "2020-01-23", "dest": "Bandung", "origin": "Jakarta", "Price": 13000 }

GET script_index/_search
{
  "query": {
        "bool" : {
            "filter" : {
                "script" : {
                    "script" : {
                        "source": "if (doc['dest'].size() <= 0 || doc['origin'].size() <= 0) {return false;}def dest = doc['dest'].value;def ori = doc['origin'].value;if(ori == dest) {return true}else {return false}",
                        "lang": "painless"
                     }
                }
            }
        }
    }
}

```

Make sure the text fields your are accessing in your script are indexed as `keyword` type (not text).

---

<div class="post-metadata">

**Author:** ![umay\_fb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/umay_fb/32/60582_2.png) [@umay\_fb](https://discuss.elastic.co/u/umay_fb)\
**Post date:** [February 10, 2020, 7:28am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/8 "2020-02-10T07:28:37Z")

</div>

thanks @flash1293,, its work..

but can we use 2 filter script query?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [February 11, 2020, 9:47am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/9 "2020-02-11T09:47:41Z")

</div>

Filter can also be an array:

```auto
GET script_index/_search
{
  "query": {
        "bool" : {
            "filter" : [{
                "script" : {
                    "script" : {
                        "source": "if (doc['dest'].size() <= 0 || doc['origin'].size() <= 0) {return false;}def dest = doc['dest'].value;def ori = doc['origin'].value;if(ori == dest) {return true}else {return false}",
                        "lang": "painless"
                     }
                }
            }, {
                "script" : {
                    "script" : {
                        "source": "return true",
                        "lang": "painless"
                     }
                }
            }]
        }
    }
}

```

---

<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 10, 2020, 9:47am UTC](https://discuss.elastic.co/t/filter-search-by-scripted-field-elastic/218139/10 "2020-03-10T09:47:44Z")

</div>

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