我正在使用AWS API网关。我有一个响应从我的后端,我正试图映射响应到一个不同的输出使用映射模板。
我试图返回每个hits.hits._source.display元素。但是映射模板返回带有文字"_source.display"的整个对象。添加到末尾的字符串。
原始的未修改的服务器响应像-
{
"took":7,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":10000,
"relation":"gte"
},
"max_score":1.0,
"hits":[
{
"_index":"address",
"_type":"_doc",
"_id":"GAVIC411064535",
"_score":1.0,
"_source":{
"id":"GAVIC411064535",
"display":"1/28 George Street, Casterton VIC 3311",
"location":{
"lat":-37.59205672,
"lon":141.38825665
},
"component":{
"buildingName":"",
"number":"1/28",
"street":"George Street",
"locality":"Casterton",
"state":"VIC",
"postcode":"3311"
}
}
},
{
"_index":"address",
"_type":"_doc",
"_id":"GAVIC411066597",
"_score":1.0,
"_source":{
"id":"GAVIC411066597",
"display":"52 St Georges Road, Corio VIC 3214",
"location":{
"lat":-37.59205672,
"lon":141.38825665
},
"component":{
"buildingName":"",
"number":"52",
"street":"St Georges Road",
"locality":"Corio ",
"state":"VIC",
"postcode":"3214"
}
}
},
我的映射模板目前看起来像这样-
#set($inputRoot = $input.path('$'))
{
#foreach($elem in $inputRoot.hits.hits)
{
"address": $elem._source.display,
}#if($foreach.hasNext),#end
#end
}
结果输出如下所示。您可以看到它返回整个$elem,而不是返回我所要求的属性。即._source.display
它也字面上打印了._source。显示到最后。
{
"address": {_index=address, _type=_doc, _id=GAVIC411064535, _score=1.0, _source={id=GAVIC411064535, display=1/28 George Street, Casterton VIC 3311, location={lat=-37.59205672, lon=141.38825665}, component={buildingName=, number=1/28, street=George Street, locality=Casterton, state=VIC, postcode=3311}}}._source.display,
},
{
"address": {_index=address, _type=_doc, _id=GAVIC411066597, _score=1.0, _source={id=GAVIC411066597, display=52 St Georges Road, Corio VIC 3214, location={lat=-37.59205672, lon=141.38825665}, component={buildingName=, number=52, street=Georges Road, locality=Corio , state=VIC, postcode=3214}}}._source.display,
},
为简洁起见,我截断了服务器响应和映射模板输出。
的映射模板输出为-
{
"address" : "28 George Street, Casterton VIC 3311",
"address" : "52 St Georges Road, Corio VIC 3214"
}
这是1.7版中的一个bug,已在2版中修复。x版本(2016年7月…AWS真的应该偶尔升级一下它的库)。
你可以这样解决这个bug:
#foreach($elem in $inputRoot.hits.hits)
{
"address": "$elem.get('_source').display"
}#if($foreach.hasNext),#end
#end