Filter Search by Scripted field elastic

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?

@flash1293

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?

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';}"
}
}
}
}

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

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

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

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).

thanks @flash1293,, its work..

but can we use 2 filter script query?

Filter can also be an array:

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"
                     }
                }
            }]
        }
    }
}

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