# Add context to suggestions field while re-indexing

**URL:** <https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407>\
**Category:** Elasticsearch\
**Tags:** painless, reindex\
**Created:** [February 15, 2024, 6:46pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407 "2024-02-15T18:46:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dat\_boi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dat\_boi](https://discuss.elastic.co/u/dat_boi)\
**Post date:** [February 15, 2024, 6:46pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/1 "2024-02-15T18:46:29Z")

</div>

so i'v been trying to add suggester to my old index which had docs with the fields  
-name --\> text  
-category --\> long  
-status --\> long

i created a new index with the same mapping as the old one and i add the suggestions fields as follows :  
here is my mapping for the suggestions field

```auto
  "name_suggest": {
          "type": "completion",
          "analyzer": "simple",
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 50,
          "contexts": [
            {
              "name": "category",
              "type": "CATEGORY"
            },
            {
              "name": "status",
              "type": "CATEGORY"
            }
          ]
        },

```

so then while trying to reindex my data to the new index i used the following script in the reindex api query

```auto
"script": {
    "source": """
        def doc = ctx._source;
        doc['name_suggest'] = [
            'input': doc['name'],
            'contexts': {
                'category': doc['category'].toString(),
                'status': doc['status'].toString()
            }
        ];
        ctx._source = doc;
    """,
    "lang": "painless"
}

```

this script is supposed to get the data from the filed name and assign it to the name name\_suggest , then get the category filed and assign it to the context field category of the name\_suggest same for status , keeping in mind both these data types are long not string , but i seem to get this 'syntax' error

```auto
 "position": {
      "offset": 123,
      "start": 98,
      "end": 148
    },
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "invalid sequence of tokens near ['{'].",
      "caused_by": {
        "type": "no_viable_alt_exception",
        "reason": null
      }
    }
  } 

```

---

<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:** [February 16, 2024, 12:31pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/2 "2024-02-16T12:31:03Z")

</div>

Hi @dat_boi

Looking at the code, isn't there missing "{ }" in the object inside the array?

> [@dat\_boi](#):
>
> ```auto
> doc['name_suggest'] = [
> 'input': doc['name'],
> 'contexts': {
> 'category': doc['category'].toString(),
> 'status': doc['status'].toString()
> }
> ];
> 
> ```

Ex:

```
    [
       {
        'input': doc['name'],
        'contexts': {
            'category': doc['category'].toString(),
            'status': doc['status'].toString()
        }
      }
    ];

```

---

<div class="post-metadata">

**Author:** ![dat\_boi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dat\_boi](https://discuss.elastic.co/u/dat_boi)\
**Post date:** [February 16, 2024, 1:35pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/3 "2024-02-16T13:35:43Z")

</div>

alright i managed to fix the first issue which was in the syntax but now i'm getting this parsing error ( also i forgot to mention this but this script is being executed in the context a reindex api )

new script

```auto
  def doc = ctx._source;
      def category = doc['category'];
      
      if (category == null) {
         category = [''];
      } 
     
      def status = doc['status'];
        if (status != null) {
          status=status.toString();
        }else{
          status='';
        }
      
       doc['name_suggest.input']= doc['name'];
       doc['name_suggest.contexts.category.value']= category;
       doc['name_suggest.contexts.status.value'] = status;

      ctx._source = doc;
  }

```

Error :

```auto
"type": "document_parsing_exception",
"reason": "[1:70] failed to parse: contexts must be a string, number or boolean or a list of string, number or boolean, but was [START_OBJECT]"

```

i also tried to change the assigned values to a fixed value like 'test' for both context values but still i get the same thing which tells me that it not really a problem in the variables status and category...

---

<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:** [February 16, 2024, 5:09pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/4 "2024-02-16T17:09:52Z")

</div>

maybe this works:

```auto
"script": {
    "source": """
     def doc = ctx._source;
     def category = doc['category'];
     def status = doc['status'];
     
     def ctxCategory = new HashMap();
     ctxCategory.put('category', category);
     
     def ctxStatus = new HashMap();
     ctxStatus.put('status', status);
     
     def contexts = new ArrayList();
     contexts.add(ctxCategory);
     contexts.add(ctxStatus);
     
     def map = new HashMap();
     map.put('input', doc['name']);
     map.put('contexts', contexts);
     
    doc['name_suggest'] = map;
     
     ctx._source = doc;
    """
  }

```

---

<div class="post-metadata">

**Author:** ![dat\_boi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dat\_boi](https://discuss.elastic.co/u/dat_boi)\
**Post date:** [February 16, 2024, 6:35pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/5 "2024-02-16T18:35:09Z")

</div>

> [@RabBit\_BR](#):
>
> ```auto
> def doc = ctx._source;
> def category = doc['category'];
> def status = doc['status'];
>      
> def ctxCategory = new HashMap();
> ctxCategory.put('category', category);
>      
> def ctxStatus = new HashMap();
> ctxStatus.put('status', status);
>      
> def contexts = new ArrayList();
> contexts.add(ctxCategory);
> contexts.add(ctxStatus);
>      
> def map = new HashMap();
> map.put('input', doc['name']);
> map.put('contexts', contexts);
>      
> doc['name_suggest'] = map;
>      
> ctx._source = doc;
> 
> ```

well it now says this

```auto
 "type": "document_parsing_exception",
        "reason": "[1:218] failed to parse: contexts must be an object or an array , but was [START_ARRAY]",
        "caused_by": {
          "type": "illegal_argument_exception",
          "reason": "contexts must be an object or an array , but was [START_ARRAY]"
        }

```

---

<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:** [February 16, 2024, 6:44pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/6 "2024-02-16T18:44:01Z")

</div>

Try this:

```auto
  "script": {
    "source": """
     def doc = ctx._source;
     def category = doc['category'];
     def status = doc['status'];
     
     def contexts = new HashMap();
     contexts.put('category', category);
     contexts.put('status', status);
     
     def map = new HashMap();
     map.put('input', doc['name']);
     map.put('contexts', contexts);
     
    doc['name_suggest'] = map;
     
     ctx._source = doc;
    """
  }

```

---

<div class="post-metadata">

**Author:** ![dat\_boi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dat\_boi](https://discuss.elastic.co/u/dat_boi)\
**Post date:** [February 16, 2024, 7:18pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/7 "2024-02-16T19:18:25Z")

</div>

yup this got it working , thanks mate !

---

<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:** [March 15, 2024, 7:19pm UTC](https://discuss.elastic.co/t/add-context-to-suggestions-field-while-re-indexing/353407/8 "2024-03-15T19:19:23Z")

</div>

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