How to access index of array correct in plainess?

Hi everyone, I'm facing a situation like this.

I have index example:

PUT my_index
{
  "mappings": {
    "properties": {
        "targetoperator": { type: "keyword" },
        "targetvalue": { type: "keyword" }
    }
}

Suppose data of document above:

"targetoperator": [">","=="]
"targetvalue": [2,5.2]

I write query with script lang plainess:

def targetoperators = doc['targetoperator'];
def targetvalues = doc['targetvalue'];
for (int i = 0; i < targetoperators.length; i++) {
   if (targetoperators[i] == '>' && targetvalues[i] > 1) {
       return true;
   }
}
return false;

But search result reponse empty. I try hardcode index i = 0 at targetvalues[0] > 1 in condition and found result.

Why access index of array incorrect with variable "i" ??

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.

I'm moving this to the Elasticsearch forum since it has nothing to do with Kibana.

Hi @jsanz ,

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

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