javascript - AWS CloudSearch - Getting results of a search in JSON format - Stack Overflow

I am performing a search on my AWS CloudSearch domain from a Lambda function in node.js:I uploaded a do

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
Add a ment  | 

2 Answers 2

Reset to default 4

After 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745588011a4634660.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信