如何从appsync解析程序执行批量elasticsearch操作?

hmtdttj4  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(374)

我正在尝试从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",
  "operation": "POST",
  "path": "/topic-en/doc/_bulk",
  "params": {
    "headers" : [ { "Content-Type" : "application/x-ndjson" } ],
    "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]"
    }
  ]
}

有什么主意吗?

68bkxrlz

68bkxrlz1#

我玩了一会儿,发现你需要
引用你的双引号
全部写在一行中,除以“\n”
该行的最后一部分需要再次使用“\n”
例如:

{
 "version": "2018-05-29",
 "method": "POST",
 "resourcePath": "/_msearch/template",
 "params": {
   "headers": {
     "Content-Type": "application/x-ndjson"
   },
   "body": "{ \"index\": { \"_id": \"COVID\" }\n{ \"text\": \"COVID\" }\n"
 }
}

当然,这并不是很令人信服,对于您的用例来说,这可能会更好:


# set($ndjson = [])

$util.qr($ndjson.add({ "index": "COVID" }))
$util.qr($ndjson.add({ "text": "COVID", "Param2": "vaccine" } }))

{
 "version": "2018-05-29",
 "method": "POST",
 "resourcePath": "/_msearch/template",
 "params": {
   "headers": {
     "Content-Type": "application/x-ndjson"
   },
   "body": "#foreach ($line in $ndjson)$util.escapeJavaScript($util.toJson($line))\n#end"
 }
}

有问题的部分可能是javascript转义。不确定您的数据看起来如何,但它可以根据需要生成更多引用的数据。
p、 s:我把我的实现切换到上面提到的例子,它可以很好地用于多重搜索。

相关问题