【问题标题】:AppSync mapping response from Lambda getting result from ElasticSearch来自 Lambda 的 AppSync 映射响应从 ElasticSearch 获取结果
【发布时间】:2019-11-20 09:19:09
【问题描述】:

我正在尝试从 AppSync 调用 lambda 函数以传递搜索查询。 lambda 调用弹性搜索返回结果集。

我能够将结果集映射到 graphQL Schama 中的不同字段

$#set($result = {
   "statusCode": "${context.result.statusCode}",
   "headers": "${context.result.headers}",
   "isBase64Encoded": "${context.result.isBase64Encoded}",
   "body": "${context.result.body}"
})
$util.toJson($result)

在正文中获取我需要解析的搜索结果集并将它们映射到一个 Schama。

我无法提取响应 ${context.result.body.hits.hits} 来遍历 _source 并设置搜索结果集

任何建议和指导都会非常有帮助。

【问题讨论】:

    标签: elasticsearch aws-lambda aws-appsync aws-cloud9


    【解决方案1】:

    AppSync 内置了对 Amazon Elasticsearch 解析器的支持。你可以找到更多关于here的信息!

    但是,如果您希望保留当前的 ​​Lambda 解析器,您可以尝试以下映射模板:

    ## Delcare an empty array
    #set( $result = [] )
    
    ## Loop through results
    #foreach($entry in $context.result.hits.hits)   
    
        ## Add each item to the result array
        $util.qr($result.add(
        {
            'id' : $entry.get("_source")['id'],
            'title' : $entry.get("_source")['fields']['title'],
            'plot' : $entry.get("_source")['fields']['plot'],
            'year' : $entry.get("_source")['fields']['year'],
            'url' : $entry.get("_source")['fields']['image_url']
        }))
    
    #end
    
    ## Parse the result
    $util.toJson($result)
    

    【讨论】:

      【解决方案2】:

      通过将上下文结果正文转换如下解决了该问题。一旦完成,我就可以迭代结果集

      [
      #set($result = $context.result)
      
      ## resultSet - parse back to JSON
      #set($result.resultSet = $util.parseJson($context.result.body))
      
      #foreach($entry in $result.resultSet.hits.hits)
      
          ## $velocityCount starts at 1 and increments with the #foreach loop **
          #if( $velocityCount > 1 ) , #end
      
          $util.toJson(
              {
              'id' : $entry.get("_source")['id'],
              'title' : $entry.get("_source")['fields']['title'],
              'plot' : $entry.get("_source")['fields']['plot'],
              'year' : $entry.get("_source")['fields']['year'],
              'url' : $entry.get("_source")['fields']['image_url']
              }
          )
      
      #end
      ]
      

      【讨论】:

        猜你喜欢
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        • 2019-01-05
        • 2018-04-06
        • 1970-01-01
        • 2020-05-29
        • 2020-01-30
        相关资源
        最近更新 更多