AWS CloudSearch:[fieldname]的值不能是JSON数组或对象

mwo*_*ton 2 json node.js amazon-cloudsearch aws-sdk

我在int-array类型的字段中遇到此问题.使用aws-sdk for Node.js,我通过CloudSearchDomain.uploadDocuments()方法提交文档.文档的JSON(在searchContent变量中)是在节点进程中创建的,然后我使用:

var params = {
  contentType: 'application/json',
  documents: JSON.stringify([searchContent])
};
csd.uploadDocuments(params, function(err, data){
  ...(callback process)...
});
Run Code Online (Sandbox Code Playgroud)

非字符串化的searchContent对象如下所示:

{ id: 1,
  type: 'Product',
  hash_type_id: 'Product-1',
  name: 'Test product',
  description: 'A test product',
  category: [ 2 ],
  content: '<some text here>',
  state: [ 1 ],
  hash_all: '<some text>'
}
Run Code Online (Sandbox Code Playgroud)

和字符串化如下:

[{"id":1,"type":"Product","hash_type_id":"Product-1","name":"Test product","description":"A test product","content":" <some text>","category":[2],"state":[1],"hash_all":"<some text>"}]
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

{
  "message": "{ [\"The value of category cannot be a JSON array or object\"] }",
  "code": "DocumentServiceException",
  "time": "2014-11-20T01:24:27.499Z",
  "statusCode": 400,
  "retryable": false
}
Run Code Online (Sandbox Code Playgroud)

如上所述,category字段是int-array类型.我为什么要收到这条消息?

更新:我还尝试使用类型数组(Int16Array)作为类别字段,结果完全相同.

mwo*_*ton 6

该文档需要包装在批处理参数中.在批处理中,每个文档都需要具有以下格式:

{
  type: "add|update|delete",
  id: "Unique ID",
  fields: Document JSON here
}
Run Code Online (Sandbox Code Playgroud)

这需要在一个数组中,即使它只是一个文档.因此问题中文档的JSON变为:

{
  type: "add",
  id: "Product-1",
  fields: { id: 1,
    type: 'Product',
    hash_type_id: 'Product-1',
    name: 'Test product',
    description: 'A test product',
    category: [ 2 ],
    content: '<some text here>',
    state: [ 1 ],
    hash_all: '<some text>'
  }
}
Run Code Online (Sandbox Code Playgroud)