简体   繁体   中英

AWS CloudSearch - Getting results of a search in JSON format

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?

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.com/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. 索引选项

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM