Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 755
Zigglzworth Avatar asked Jun 29 '16 18:06

Zigglzworth


People also ask

Is Amazon CloudSearch deprecated?

Amazon search is deprecated: Amazon search service is no longer supported. To set up a search functionality on your site(s), configure one of the three built-in search services instead. Amazon Cloud Search is deprecated in Sitefinity 13.3.

What is the difference between CloudSearch and Elasticsearch?

In Elasticsearch, searching happens on both index and types using a search API. The search API also includes Faceting and Filtering for searching data. In CloudSearch, users create a search domain that includes sub-services to upload documents. A search service provides the means to search indexed data.

What is facet in CloudSearch?

A facet is an index field that represents a category that you want to use to refine and filter search results. When you submit search requests to Amazon CloudSearch, you can request facet information to find out how many documents share the same value in a particular field.

How do I use CloudSearch?

To start searching your data with Amazon CloudSearch, you simply take the following steps: Create and configure a search domain. Upload and index the data you want to search. Send search requests to your domain.


2 Answers

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.

like image 50
Zigglzworth Avatar answered Sep 28 '22 11:09

Zigglzworth


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. indexing options

like image 41
alexroussos Avatar answered Sep 28 '22 12:09

alexroussos