# How to access index of array correct in plainess?

**URL:** <https://discuss.elastic.co/t/how-to-access-index-of-array-correct-in-plainess/345617>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 24, 2023, 8:17am UTC](https://discuss.elastic.co/t/how-to-access-index-of-array-correct-in-plainess/345617 "2023-10-24T08:17:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![duyhunter1001](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/duyhunter1001/32/126712_2.png) [@duyhunter1001](https://discuss.elastic.co/u/duyhunter1001)\
**Post date:** [October 24, 2023, 8:17am UTC](https://discuss.elastic.co/t/how-to-access-index-of-array-correct-in-plainess/345617/1 "2023-10-24T08:17:59Z")

</div>

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

I have index example:

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

```

Suppose data of document above:

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

```

I write query with script lang plainess:

```auto
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" ??

---

<div class="post-metadata">

**Author:** ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)\
**Post date:** [October 27, 2023, 8:55am UTC](https://discuss.elastic.co/t/how-to-access-index-of-array-correct-in-plainess/345617/2 "2023-10-27T08:55:45Z")

</div>

I put your script in a runtime field search and I had to do some adjustments to get it to work.

```auto
# 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.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/4/24d6fb364e041940982a05c0646aef30f32cc022.png)

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

---

<div class="post-metadata">

**Author:** ![duyhunter1001](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/duyhunter1001/32/126712_2.png) [@duyhunter1001](https://discuss.elastic.co/u/duyhunter1001)\
**Post date:** [November 13, 2023, 8:46am UTC](https://discuss.elastic.co/t/how-to-access-index-of-array-correct-in-plainess/345617/3 "2023-11-13T08:46:51Z")

</div>

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](https://stackoverflow.com/questions/77363990/how-to-use-for-loop-exactly-in-script-plainess-elasticsearch)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 11, 2023, 8:47am UTC](https://discuss.elastic.co/t/how-to-access-index-of-array-correct-in-plainess/345617/4 "2023-12-11T08:47:14Z")

</div>

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