Char-Lez Braden
Char-Lez Braden

Reputation: 48

Using AWS.cloudSearch with javaScript

I'm trying to use AWS cloudSearch on my site, and I'm missing something fundamental. I've had no luck finding a working example.

In my HTML head I've included the SDK. per: http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-intro.html

<script type="text/javascript" src="https://sdk.amazonaws.com/js/aws-sdk-2.0.31.min.js"></script>

Then in my script tag I have this snippet (with my creds, not the ones shown here). I used hard coded creds for my testing per: http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html

var s3 = new AWS.S3({region: 'ap-southeast-2', maxRetries: 15});
AWS.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'});
console.log(AWS);
//
var cloudsearch = new AWS.CloudSearch();

When it tries to execute the last line javaScript tells me: Uncaught TypeError: undefined is not a function

I've been unable to find a demo, example or tutorial. The docs don't even have working examples.

Can you help?

Thank you for your time.

Upvotes: 1

Views: 2710

Answers (2)

Bruce Allen
Bruce Allen

Reputation: 91

Created an angularjs project that demos this and added to git hub. You need CORS-anywhere to route properly to the domain.

https://github.com/tkntobfrk/amazon-cloudsearch-angular

project uses suggesters to search domain data and populate the input field with autocomplete data. Uses bootstrap.ui typeahead.

Upvotes: 0

Bruce Allen
Bruce Allen

Reputation: 91

That error is saying it doesn't find reference to cloudsearch when you are calling the new AWS.Cloudsearch().

The script you reference doesn't appear to have a reference to cloudsearch api. I think you need to build it out like they specify in Building the SDK for Use in the Browser.

For kicks I replicated the error you received, then did a browser build as specified by the link above. It worked.

Upon further investigation after looking through the libraries. I think you probably want to use CloudSearchDomain to issue searches to a given domain setup within cloudsearch.

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudSearchDomain.html

The other cloudsearch library is all administrative/setup related to Search domain creation and other administrative types of objects. Looks like there are some issues with CORS/setup for cloudsearch in general so if you want to issue this straight from the browser might be a bit tricky.

The most common way to get around the CORS issue is to use a proxy.

The easiest way to play around with search from the browser\local is using a form, as suggested here.

Amazon CloudSearch query

<form action="https://$YOURSEARCHDOMAIN/2013-01-01/search" method="get">
    <label>Search: <input name="q" /></label>
    <input type="hidden" name="q.parser" value="simple" />
    <button type="submit">Search</button>
</form>

above solution is pretty much cut and paste\play, works like the AWS cloudsearch console, and returns the json to the browser. Not sure if you can incorporate suggests without the api directly. You will need to open up your access policy to work, I believe.

I tried a variety of access policies and various tricks to get around the CORS issue, all no dice.

You actually need the CORS plugin for chrome to make this work properly.

Upvotes: 3

Related Questions