Elasticsearch groovy script to check if parameters are included in the array

I use elasticsearch (v2.0.0) to search in Rails and want to add scoring to our custom script, but I either messed up the syntax or just skipped something else completely. All this works without checking in the script for the array, so the only part that does not work.

So, for the index, recipe_user_ids is an array of integers:

 indexes :recipe_user_ids, type: 'integer' 

Then in the search query, I specify the parameter for the script file and which script file:

 functions: [{ script_score: { params: { current_user_id: user.id }, script_file: 'ownership_script' } }] 

And the script.groovy property file:

 if (current_user_id == doc['user_id'].value) { owner_modifier = 1.0 } else { owner_modifier = 0.0 } if (doc['recipe_user_ids'].values.contains(current_user_id)) { recipe_user_modifier = 50.0 } else { recipe_user_modifier = 0.0 } (_score + (doc['score_for_sort'].value + owner_modifier + recipe_user_modifier)*5)/_score 

I am not getting any errors, but the results do not seem to match what I would expect when the recipe_user_ids array contains current_user_id , so everything falls into the else statement. Is that a type question, syntax? Any advice was greatly appreciated.

+5
source share
1 answer

This is due to type mismatch caused by auto-boxing.

doc['field_name].values , integer , long types short seem to return a collection always of type "Long", and the argument contains auto-boxed to integer , causing contains fail.

You could probably use current_user_id explicitly for the Long type:

Example:

doc['recipe_user_ids'].values.contains(new Long(current_user_id))

Or is it better to use the Find method

doc['recipe_user_ids'].values.find {it == current_user_id}

+1
source

All Articles