# Elasticsearch script weak at handle string change while use for loop?

**URL:** <https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936>\
**Category:** Elasticsearch\
**Created:** [September 8, 2022, 3:26am UTC](https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936 "2022-09-08T03:26:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)\
**Post date:** [September 8, 2022, 3:26am UTC](https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936/1 "2022-09-08T03:26:04Z")

</div>

Elasticsearch really weak at handle string change. i have a script like this

```auto

def newSegment;

if(params.data !=null){
for (entry in params.data.entrySet()){
    if (entry.getValue() != null) {
      ctx._source[entry.getKey()] = entry.getValue();
    }
    else {
      ctx._source.remove(entry.getKey());
    }
  }
}

if (params.segment != null) {
  
  
  if(ctx._source.segment_ids == null){
    newSegment = ' ';
  }
  else{
  newSegment = ctx._source.segment_ids;
  }

  if (params.segment.include != null) {
    for (inc in params.segment.include){
        if (!newSegment.contains(inc)){

          if(newSegment == ' '){
            newSegment = newSegment.trim() + inc;
          }
          else {
            newSegment = newSegment + '|'+inc;
          }

        }
    }
  }
  
  if(newSegment !=' '){

    if (params.segment.exclude != null) {

      for (exc in params.segment.exclude){
        def temp = '|'+exc+'|';

        if (newSegment.contains(temp)){
          newSegment = newSegment.replace(temp,'|');
          continue;
        }

        temp = exc +'|';

        if(newSegment.contains(temp) & newSegment.indexOf(exc)==0){
          newSegment = newSegment.replace(temp,'');
          continue;
        }
        temp = '|' + exc;
        if(newSegment.contains(temp) & newSegment.indexOf(exc) == newSegment.length()-exc.length()){
          newSegment = newSegment.replace(temp,'');
          continue;
        }
        if (newSegment.contains(exc) & newSegment.length() - exc.length() ==0){
        newSegment = newSegment.replace(exc,'');
        } 

      }
  }
}
  if(newSegment == ' '){
   ctx._source.remove('segment_ids');
  }
  else{
  ctx._source.segment_ids = newSegment
  }
}

```

and here is my update body

```auto
{
    "script":{
        "id":"cdp_upsert_v2",
        "params":{
            "data":{
                "customer_id":"999999" 
                           },
            "segment":{
                "include":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53"],
                "exclude":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","123","124","125"]
            }
        }
    }
}

```

every thing is fine if exclude is have little element, but when it have more element like example above, it return false result

here is the response

```auto
{
  "_index" : "v3_customers_33167",
  "_type" : "_doc",
  "_id" : "33167:999999",
  "_version" : 52,
  "_seq_no" : 16994624,
  "_primary_term" : 9,
  "found" : true,
  "_source" : {
    "customer_id" : "999999",
    "segment_ids" : "111111111122222222223333333333444444444455553"
  }
}

```

segment\_ids field should be deleted, not response like that. is it because I use replace too much time?, it's still fine with include, don't need to know how much element in it

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 8, 2022, 5:52am UTC](https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936/2 "2022-09-08T05:52:58Z")

</div>

I recall there [being a limit to the number of loop iterations](https://discuss.elastic.co/t/painless-script-loop-maximum-number-of-statements-limit/70895) but do not seem to be able to find anything in the current documentation around this. Maybe somone from Elastic can shed some light on this.

---

<div class="post-metadata">

**Author:** ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)\
**Post date:** [September 8, 2022, 3:00pm UTC](https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936/3 "2022-09-08T15:00:41Z")

</div>

my script logic is false. I have edited it and it is working well now. thank you for the info about the limit of for loop in ES

---

<div class="post-metadata">

**Author:** ![cheshirecat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cheshirecat/32/109532_2.png) [@cheshirecat](https://discuss.elastic.co/u/cheshirecat)\
**Post date:** [September 9, 2022, 10:13am UTC](https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936/4 "2022-09-09T10:13:57Z")

</div>

Could you please paste the solution?

---

<div class="post-metadata">

**Author:** ![robocon20x](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robocon20x/32/99145_2.png) [@robocon20x](https://discuss.elastic.co/u/robocon20x)\
**Post date:** [September 9, 2022, 10:26am UTC](https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936/5 "2022-09-09T10:26:19Z")

</div>

```auto
def newSegment;

if(params.data !=null){
for (entry in params.data.entrySet()){
    if (entry.getValue() != null) {
      ctx._source[entry.getKey()] = entry.getValue();
    }
    else {
      ctx._source.remove(entry.getKey());
    }
  }
}

if (params.segment != null) {
  
  
  if(ctx._source.segment_ids == null){
    newSegment = '|';
  }
  else{
  newSegment = '|' + ctx._source.segment_ids+'|';
  }

  if (params.segment.include != null) {
    for (inc in params.segment.include){
        if (!newSegment.contains(inc)){

            newSegment = newSegment +inc + '|';
          
        }
    }
  }
  
  if(newSegment !='|'){

    if (params.segment.exclude != null) {

      for (exc in params.segment.exclude){
        def temp = '|'+exc+'|';

        if (newSegment.contains(temp)){
          newSegment = newSegment.replace(temp,'|');
        }

      }
  }
}
  if(newSegment == '|'){
   ctx._source.remove('segment_ids');
  }
  else{
  ctx._source.segment_ids = newSegment.substring(1,newSegment.length() -1)
  }
}

```

here is my new script

---

<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:** [October 7, 2022, 10:26am UTC](https://discuss.elastic.co/t/elasticsearch-script-weak-at-handle-string-change-while-use-for-loop/313936/6 "2022-10-07T10:26:56Z")

</div>

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