Filter be scripted field


(Ali Syed) #1

Is it possible to filter by a scripted field? If so, how would the query look like?

I tried using the scripted field in a post_filter but it dies silently (0 matches)

The goal is to derive a new field as a predicate based on an existing field, and then filter on it based a condition...

GET /concession_test/_search
{
"_source": ["ref_nr","material_text_en"],
"script_fields":{
"test_pred_1":{
"script":{
"lang":"painless",
"source":"if(params._source.material_text_en.contains("TASK_1") || params._source.material_text_en.contains("QUEUE_1") || params._source.material_text_en.contains("TEST_1")){ return true } else { return false }"
}
}
},
"post_filter": {
"term": { "test_pred_1": true }
}
}

Thanks! :slight_smile:


(Ryan Ernst) #2

script fields are only additional fields added to the top N results. They are not run for every document potentially matching the query. You would need to copy the script. However, I strongly discourage that in this case as you are using _source. Instead, use queries. For example, a bool query with should match clauses looking for TEST_1 or QUEUE_1 or TASK_1 should be equivalent?


(Ali Syed) #3

Do you mean doing a script in a query context?

I had to use _source as the fielddata is off for our main server. Getting it changed is too much work :smiley:


(system) #4

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