I put your script in a runtime field search and I had to do some adjustments to get it to work.
# Create an index
PUT discuss-345617
{
"mappings": {
"properties": {
"targetoperator": { "type": "keyword" },
"targetvalue": { "type": "keyword" }
}
}
}
# Insert a doc with 2 elements in the arrays
POST discuss-345617/_doc
{
"targetoperator": [">","=="],
"targetvalue": [2.0,5.2]
}
# Insert a doc with 3 elements in the arrays
POST discuss-345617/_doc
{
"targetoperator": ["==",">",">"],
"targetvalue": [2.0,5.2,0.5]
}
# Insert a doc with 4 elements in the arrays
POST discuss-345617/_doc
{
"targetoperator": ["==",">",">", "=="],
"targetvalue": [2.0,5.2,0.5, 0.5]
}
# Search the index with a boolean runtime field
GET discuss-345617/_search
{
"runtime_mappings": {
"test": {
"type": "boolean",
"script": {
"source": """
def targetoperators = params._source.targetoperator;
def targetvalues = params._source.targetvalue;
for(int i = 0; i< targetoperators.length; i++){
if (
targetoperators[i] == '>' &&
targetvalues[i] > 1
) {
emit(true);
} else {
emit(false);
}
}
"""
}
}
},
"fields": ["test"],
"query": {"match_all": {}}
}
I had to change to use _source because using doc accesse to the indexed documents, and Lucene removes duplicates. In my tests I had repeated operatos, hence using _source was necessary.
In runtime scripts you use emit, and that can be executed as many times as needed so I had to move the false condition inside the loop otherwise all executions ended with a final false being added.
Apparently, the elements of the resulting array are not added in the same order as executed in the loop.
After I research elasticsearch documents. I know, after elasticsearch insert data with type key "keyword" so data sorted. Therefore, my for loop didn't work properly.
Do you have any advice or solution for me in this case?
p/s: I have asked a question on Stackoverflow, you can refer to my purpose link
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.