Add context to suggestions field while re-indexing

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

  "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

"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

 "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
      }
    }
  } 

Hi @dat_boi

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

Ex:

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

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

  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 :

"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...

maybe this works:

"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;
    """
  }

well it now says this

 "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]"
        }

Try this:

  "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;
    """
  }
1 Like

yup this got it working , thanks mate !

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