如何从AppSync解析器执行批量ElasticSearch操作?

问题描述

我正在尝试通过VTL AppSync解析器对ElasticSearch索引执行批量操作。

特别是这种突变:

input CreateTopicsInput {
  language: String!
  texts: [String!]!
}

type Mutation {
  createTopics(input: CreateTopicsInput!): [Topic!]
}

我创建了以下解析器: Mutation.createTopics.req.vtl

#set( $body = "" )
#set( $q = '"' )
#foreach( $text in $ctx.args.input.texts )
  #set( $body = $body.concat("{ ${q}index${q}: { ${q}_id${q}: ${q}${text}${q} } }
") )
  #set( $body = $body.concat("{ ${q}text${q}: ${q}$text${q},${q}suggest${q}: { ${q}input${q}: ${q}$text${q} } }
") )
#end
{
  "version": "2017-02-28","operation": "POST","path": "/topic-${ctx.args.input.language}/doc/_bulk","params": {
    "headers" : [ { "Content-Type" : "application/x-ndjson" } ],"body": "$body"
  }
}

例如在执行以下数据时:

mutation CreateTopic {
  createTopics(input: {
    language: "en",texts: [ "COVID","Election" ]}) {
    text
  }
}

似乎输出正确的调用:

{
  "version": "2017-02-28","path": "/topic-en/doc/_bulk","body": "{ "index": { "_id": "COVID" }
{ "text": "COVID" }
{ "index": { "_id": "Election" }
{ "text": "Election" }
"
  }
}

但不起作用。具体来说会抛出:

{
  "data": {
    "createTopics": null
  },"errors": [
    {
      "path": [
        "createTopics"
      ],"data": null,"errorType": "MappingTemplate","errorInfo": null,"locations": [
        {
          "line": 2,"column": 3,"sourceName": null
        }
      ],"message": "Unexpected character ('i' (code 105)): was expecting comma to separate Object entries\n at [Source: (String)\"{\n  \"version\": \"2017-02-28\",\n  \"operation\": \"POST\",\n  \"path\": \"/topic-en/doc/_bulk\",\n  \"params\": {\n    \"headers\" : { \"Content-Type\" : \"application/x-ndjson\" },\n    \"body\": \"{ \"index\": { \"_id\": \"CODID\" } }\n{ \"text\": \"CODID\",\"suggest\": { \"input\": \"CODID\" } }\n{ \"index\": { \"_id\": \"Election\" } }\n{ \"text\": \"Election\",\"suggest\": { \"input\": \"Election\" } }\n\"\n  }\n}\n\"; line: 7,column: 18]"
    }
  ]
}

有什么主意吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)