admin管理员组文章数量:1432187
I am performing a search on my AWS CloudSearch domain from a Lambda function in node.js:
I uploaded a document such as this:
{
“some_field”: “bla bla“,
“some_date_field”: 1.466719E9,
"number_field”: 4,
“some_string”: "some long string blabla"
}
And I perform a search like this
var params = {
query: 'bla bla',
};
cloudsearchdomain.search(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
context.fail(err);
}
else {
context.succeed(data); // successful response
}
});
The search works and as documented here CloudSearch returns document info in fields property of a hit. Here is an example:
{
"status": {
"timems": 2,
"rid": “blabla”
},
"hits": {
"found": 1,
"start": 0,
"hit": [
{
"id": “452545-49B4-45C3-B94F-43524542352-454352435.6666-8532-4099-xxxx-1",
"fields": {
“some_field”: [
“bla bla“
],
“some_date_field”: [
"1.466719E9"
],
"number_field”: [
"4"
],
“some_string”: [
"some long string blabla"
],
}
}
]
}
}
As you can see all the fields are returned as strings in an array. Is there anyway to get the results as a JSON that preserves the type of all the fields?
I am performing a search on my AWS CloudSearch domain from a Lambda function in node.js:
I uploaded a document such as this:
{
“some_field”: “bla bla“,
“some_date_field”: 1.466719E9,
"number_field”: 4,
“some_string”: "some long string blabla"
}
And I perform a search like this
var params = {
query: 'bla bla',
};
cloudsearchdomain.search(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
context.fail(err);
}
else {
context.succeed(data); // successful response
}
});
The search works and as documented here CloudSearch returns document info in fields property of a hit. Here is an example:
{
"status": {
"timems": 2,
"rid": “blabla”
},
"hits": {
"found": 1,
"start": 0,
"hit": [
{
"id": “452545-49B4-45C3-B94F-43524542352-454352435.6666-8532-4099-xxxx-1",
"fields": {
“some_field”: [
“bla bla“
],
“some_date_field”: [
"1.466719E9"
],
"number_field”: [
"4"
],
“some_string”: [
"some long string blabla"
],
}
}
]
}
}
As you can see all the fields are returned as strings in an array. Is there anyway to get the results as a JSON that preserves the type of all the fields?
Share Improve this question edited Jun 29, 2016 at 19:04 Zigglzworth asked Jun 29, 2016 at 18:39 ZigglzworthZigglzworth 6,8439 gold badges71 silver badges112 bronze badges 1- @alexroussos - Thought you might now the answer so tagging you – Zigglzworth Commented Jun 29, 2016 at 18:41
2 Answers
Reset to default 4After submitting a report about this to AWS I received this reply:
Hello, This is actually the intended behavior. The SDK team chose to implement the "fields" property as a dictionary of string keys and string-array values to maintain consistency across the various languages in which the AWS SDK exists. They place the responsibility for handling the various response formats (HTTP request vs. SDK method) on the client. For more details, please see: https://github./aws/aws-sdk-js/issues/791
Unfortunately the only current solutions to the problem I describe above is:
1) Create a parser that will parse the results as needed based on your expected response which takes into account your data types
2) Add a new field to your cloudsearch index (text type) containing a stringified version of your entire json object/document. You can then just use JSON.parse() on this to get the document in JSON format. This solution is not ideal because it adds an unnecessary chunk of text to your document but it proved a quick solution to my problem above.
I'd love to hear of any more solutions if anyone knows of any.
CloudSearch does preserve the field type; the results imply that you've configured these fields as arrays.
You can confirm this by going to Indexing Options for your domain on the AWS web console. You should see fields that are text-array
, literal-array
, etc as in the screenshot below. Those will be returned as arrays. You can change them to non-array types if you will only ever be submitting a single value for each field in each document and you'll get back non-array values.
本文标签: javascriptAWS CloudSearchGetting results of a search in JSON formatStack Overflow
版权声明:本文标题:javascript - AWS CloudSearch - Getting results of a search in JSON format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745588010a2665026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论