简体   繁体   中英

AWS cloudsearch in android

I am planning to implement a predictive search functionality in Android app, I am using AWS as backend, i got to know about the AWS cloud search, Please someone let me know how to access the cloudsearch from Android, I am using AWS Android SDK for development.

I know how to configure the Cloudsearch and add suggester. Only i need to how to get suggestions, for example if user types "foo" it should return the suggestions

You can set up and perform a request something like this:

final String SERVICE_NAME = "cloudsearch";

// Replace AnonymousAWSCredentials with whatever kind of credentials you really want
AnonymousAWSCredentials anonCredentials = new AnonymousAWSCredentials();
final String searchHost = "https://<CloudSearch endpoint goes here>.eu-west-1.cloudsearch.amazonaws.com";

ClientConfiguration clientConfiguration = new ClientConfiguration();

Request request = new DefaultRequest(SERVICE_NAME);
request.setEndpoint(URI.create(searchHost));
request.setResourcePath("/2013-01-01/search");
request.setHttpMethod(HttpMethodName.GET);
request.addParameter("q", searchTerms); // searchTerms is whatever you want to search for

request.addHeader("Content-Type", "application/json");

AWS4Signer signer = new AWS4Signer();
signer.setServiceName(SERVICE_NAME);
signer.setRegionName(Region.getRegion(Regions.EU_WEST_1).getName());
// Make sure your Request here matches that in searchHost above

ExecutionContext executionContext = new ExecutionContext();

signer.sign(request, anonCredentials);

AmazonHttpClient webClient = new AmazonHttpClient(clientConfiguration);
webClient.execute(request, new HttpResponseHandler<AmazonWebServiceResponse<String>>() {
                @Override
                public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception {
                    Log.v(TAG, "Successful response!");
                    return null;
                }

                @Override
                public boolean needsConnectionLeftOpen() {
                    return false;
                }
            }, new HttpResponseHandler<AmazonServiceException>() {
                @Override
                public AmazonServiceException handle(HttpResponse response) throws Exception {
                    BufferedReader r = new BufferedReader(new InputStreamReader(response.getContent()));
                    StringBuilder total = new StringBuilder();
                    for (String line; (line = r.readLine()) != null; ) {
                        total.append(line).append('\n');
                    }

                    Log.v(TAG, "Error response: " + response.getStatusText() + " / " + total.toString());
                    return null;
                }

                @Override
                public boolean needsConnectionLeftOpen() {
                    return false;
                }
            }, executionContext);

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