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

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

我正在尝试从vtl appsync解析程序对elasticsearch索引执行批量操作。
特别是这种突变:

  1. input CreateTopicsInput {
  2. language: String!
  3. texts: [String!]!
  4. }
  5. type Mutation {
  6. createTopics(input: CreateTopicsInput!): [Topic!]
  7. }

我创建了以下解析器:mutation.createtopics.req.vtl

  1. # set( $body = "" )
  2. # set( $q = '"' )
  3. # foreach( $text in $ctx.args.input.texts )
  4. #set( $body = $body.concat("{ ${q}index${q}: { ${q}_id${q}: ${q}${text}${q} } }
  5. ") )
  6. #set( $body = $body.concat("{ ${q}text${q}: ${q}$text${q}, ${q}suggest${q}: { ${q}input${q}: ${q}$text${q} } }
  7. ") )
  8. # end
  9. {
  10. "version": "2017-02-28",
  11. "operation": "POST",
  12. "path": "/topic-${ctx.args.input.language}/doc/_bulk",
  13. "params": {
  14. "headers" : [ { "Content-Type" : "application/x-ndjson" } ],
  15. "body": "$body"
  16. }
  17. }

例如,当用这个数据执行时:

  1. mutation CreateTopic {
  2. createTopics(input: {
  3. language: "en",
  4. texts: [ "COVID", "Election" ]}) {
  5. text
  6. }
  7. }

似乎输出了正确的调用:

  1. {
  2. "version": "2017-02-28",
  3. "operation": "POST",
  4. "path": "/topic-en/doc/_bulk",
  5. "params": {
  6. "headers" : [ { "Content-Type" : "application/x-ndjson" } ],
  7. "body": "{ "index": { "_id": "COVID" }
  8. { "text": "COVID" }
  9. { "index": { "_id": "Election" }
  10. { "text": "Election" }
  11. "
  12. }
  13. }

但它不起作用。特别是它抛出:

  1. {
  2. "data": {
  3. "createTopics": null
  4. },
  5. "errors": [
  6. {
  7. "path": [
  8. "createTopics"
  9. ],
  10. "data": null,
  11. "errorType": "MappingTemplate",
  12. "errorInfo": null,
  13. "locations": [
  14. {
  15. "line": 2,
  16. "column": 3,
  17. "sourceName": null
  18. }
  19. ],
  20. "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]"
  21. }
  22. ]
  23. }

有什么主意吗?

68bkxrlz

68bkxrlz1#

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

  1. {
  2. "version": "2018-05-29",
  3. "method": "POST",
  4. "resourcePath": "/_msearch/template",
  5. "params": {
  6. "headers": {
  7. "Content-Type": "application/x-ndjson"
  8. },
  9. "body": "{ \"index\": { \"_id": \"COVID\" }\n{ \"text\": \"COVID\" }\n"
  10. }
  11. }

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

  1. # set($ndjson = [])
  2. $util.qr($ndjson.add({ "index": "COVID" }))
  3. $util.qr($ndjson.add({ "text": "COVID", "Param2": "vaccine" } }))
  4. {
  5. "version": "2018-05-29",
  6. "method": "POST",
  7. "resourcePath": "/_msearch/template",
  8. "params": {
  9. "headers": {
  10. "Content-Type": "application/x-ndjson"
  11. },
  12. "body": "#foreach ($line in $ndjson)$util.escapeJavaScript($util.toJson($line))\n#end"
  13. }
  14. }

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

展开查看全部

相关问题