update_by_query object into array with elasticsearch in TypeScript

33 views Asked by At

I need to add a card object to list of cards

use "elasticsearch": "^16.7.3", and "@types/elasticsearch": "^5.0.43",

i try this code:


  const theScript = {
        "inline": "ctx._source.cards.add(card)"
        }
        const client: Client = await this.elastic.client;
        let result: CardDto = card;
        const response = await client.updateByQuery({
            index: "index",
            type: "type",
            body: {
              "query": { "match" : { "id" : idUser}},
               "script" : theScript,
                "params" : {
                "card" : `num1 : ${card.num1},
                  num2:   ${card.num2},
                 num3 : ${card.num3}`
            }}});
          if(response){
            console.log(response);
          }
          return result;
        }

but i get problem with the params and the query

here what i get in postMan : [parsing_exception] Unknown key for a START_OBJECT in [params]., with { line=1 & col=45 }"

1

There are 1 answers

0
Kashyap Thacker On

Two issues

  • The inline script in theScript does not know what card is.
  • card in Param should be a JSON.

Haven't tested this, but please try:

const theScript = {
    "inline": "ctx._source.cards.add(params.card)"
    }
    const client: Client = await this.elastic.client;
    let result: CardDto = card;
    const response = await client.updateByQuery({
        index: "index",
        type: "type",
        body: {
          "query": { "match" : { "id" : idUser}},
           "script" : theScript,
            "params" : {
            "card" : {
              "num1": ${card.num1},
              "num2": ${card.num2},
              "num3": ${card.num3}
            } 
        }}});
      if(response){
        console.log(response);
      }
      return result;
    }