# How to compare value exactly from array of document with params in script plainess?

**URL:** <https://discuss.elastic.co/t/how-to-compare-value-exactly-from-array-of-document-with-params-in-script-plainess/345691>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 25, 2023, 4:17am UTC](https://discuss.elastic.co/t/how-to-compare-value-exactly-from-array-of-document-with-params-in-script-plainess/345691 "2023-10-25T04:17:20Z")\
**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 25, 2023, 4:17am UTC](https://discuss.elastic.co/t/how-to-compare-value-exactly-from-array-of-document-with-params-in-script-plainess/345691/1 "2023-10-25T04:17:20Z")

</div>

Hi everyone, I have a index example above:

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

```

```auto
PUT target_index/_doc/1
{
  "targetoperator": [">", "=="],
  "targetvalue": [4, 5]
}

PUT target_index/_doc/2
{
  "targetoperator": ["<", "<="],
  "targetvalue": [6, 5.5]
}

PUT target_index/_doc/3
{
  "targetoperator": [">=", ">", "<", "<="],
  "targetvalue": [2, 4, 8, 8.5]
}

PUT target_index/_doc/4
{
  "targetoperator": [">"],
  "targetvalue": [2.5]
}

PUT target_index/_doc/5
{
  "targetoperator": ["!="],
  "targetvalue": [3.5]
}

```

I run a script query above:

```auto
GET target_index/_search
{
  "query": {
    "script": {
      "script": { 
        "source": """
          double userCriteria = params.userTarget.seniority;
          def targetoperator = doc['targetoperator'];
          def targetvalue = doc['targetvalue'];
          if (targetoperator.size() == targetvalue.size()) {
            for (int i = 0; i < targetoperator.size(); i++) {
              def operaVal = targetoperator[i];
              def valueVal = targetvalue[i];
              if (operaVal == "<=") {
                if (valueVal <= userCriteria) {
                  return true;
                }
              }
            }
          }
          return false;
        """, 
        "params": {
          "userTarget": {
            "gender": "male",
            "seniority": 6
          }
        }
      }
    }
  },
  "_source": [
    "targetoperator",
    "targetvalue"
  ]
}

```

My target want check value of field params.userTarget.seniority \<=  
value of field `targetvalue` ?

But, search result return about document id = 2, and id = 3.

```auto
{
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "target_index",
        "_id": "2",
        "_score": 1,
        "_source": {
          "targetoperator": [
            "<",
            "<="
          ],
          "targetvalue": [
            6,
            5.5
          ]
        }
      },
      {
        "_index": "target_index",
        "_id": "3",
        "_score": 1,
        "_source": {
          "targetoperator": [
            ">=",
            ">",
            "<",
            "<="
          ],
          "targetvalue": [
            2,
            4,
            8,
            8.5
          ]
        }
      }
    ]
  }
}

```

**If correct, the result return only document id = 3.** 😔

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [October 26, 2023, 4:41am UTC](https://discuss.elastic.co/t/how-to-compare-value-exactly-from-array-of-document-with-params-in-script-plainess/345691/2 "2023-10-26T04:41:36Z")

</div>

Hi @duyhunter1001

> [@duyhunter1001](#):
>
> ```auto
> for (int i = 0; i < targetoperator.size(); i++) {
> def operaVal = targetoperator[i];
> def valueVal = targetvalue[i];
> if (operaVal == "<=") {
> if (valueVal <= userCriteria) {
> return true;
> }
> }
> }
> 
> ```

From what I understand, you use the item's position in the array to validate if the value is lower than the useCriteria, which is 6. I don't understand why you expect to get the doc id 3, whereas in the operator "\<=" position, the targetvalue will be 8.5

I could be wrong but I saw somewhere that using the array, elasticsearch does not guarantee the order in which it was indexed when you retrieve the field. I could be wrong, let's see what the members say.

---

<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 26, 2023, 6:19am UTC](https://discuss.elastic.co/t/how-to-compare-value-exactly-from-array-of-document-with-params-in-script-plainess/345691/4 "2023-10-26T06:19:16Z")

</div>

Hi @RabBit_BR , I have confusion in my test case. The above case is correct.  
The following case is not correct:

```auto
PUT target_index/_doc/1
{
  "targetoperator": ["<", "<="],
  "targetvalue": [6, 5.5]
}

PUT target_index/_doc/2
{
  "targetoperator": [">=", ">", "<", "<="],
  "targetvalue": [2, 4, 8, 8.5]
}

GET target_index/_search
{
  "query": {
    "script": {
      "script": { 
        "lang": "painless",
        "source": """
          double userCriteria = params.userTarget.seniority;
          def targetoperator = doc['targetoperator'];
          def targetvalue = doc['targetvalue'];
          if (targetoperator.size() == targetvalue.size()) {
            for (int i = 0; i < targetoperator.size(); i++) {
              def operaVal = targetoperator[i];
              def valueVal = targetvalue[i];
              if (operaVal == "<=") {
                if (!(userCriteria <= valueVal)) {
                  return false;
                }
              }
              if (operaVal == ">") {
                if (!(userCriteria > valueVal)) {
                  return false;
                }
              }
            }
          }
          return true;
        """, 
        "params": {
          "userTarget": {
            "gender": "male",
            "seniority": 7
          }
        }
      }
    }
  }
}

```

It return empty document, if correct, It will return about document id = 2 because userCriteria \<= 8.5

---

<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:** [November 23, 2023, 6:19am UTC](https://discuss.elastic.co/t/how-to-compare-value-exactly-from-array-of-document-with-params-in-script-plainess/345691/5 "2023-11-23T06:19:54Z")

</div>

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