Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean value scripting issue with MVEL and Elasticsearch

I have a field mapping defined as

{"top_seller":{"type":"boolean"}}

In my query, I'm trying to do a custom score query based on the boolean value. I'm pulling my hair out. Every time I run a script such as this:

return if(doc['top_seller'].value==true) {10} else {0}

Every single document gets the true 10 boost. Only 1% of my documents are set as TRUE. I've tried without ==true, with =='true'. I've tried the ternary. doc['top_seller'].value==true?10:0. I've tried 1/0 instead of true/false.

I even did an experiment where I created a new index and type with with a single true and a single false document. In a match_all query, they both get the boost as though they have the true value.

like image 556
J.T. Avatar asked Dec 01 '22 20:12

J.T.


1 Answers

Wow, on a whim, I was looking at the core type settings for boolean.

The boolean type Maps to the JSON boolean type. It ends up storing within the index either T or F, with automatic translation to true and false respectively.

The answer is:

doc['top_seller'].value == 'T' ? 10 : 0

Edit: As of 5.2.x, I am finally able to use doc['top_seller'] ? 10 : 0. https://www.elastic.co/guide/en/elasticsearch/reference/current/boolean.html

like image 131
J.T. Avatar answered May 17 '23 06:05

J.T.