How do I connect to an existing CloudSearch domain in boto?
Asked Answered
P

5

6

I'm just starting to work with boto to connect to Amazon CloudSearch.

I got the examples working, but I can't find any examples of connecting to an existing domain, all the examples create a new domain.

Poking around, I found get_domain, but that fails if I call it on the connection object.

>>> conn.get_domain('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Layer2' object has no attribute 'get_domain'

Any suggestions as to how I can connect to an existing domain?

[edit] I started from this: http://boto.cloudhackers.com/en/latest/cloudsearch_tut.html

So, all I'm doing this

import boto
conn = boto.connect_cloudsearch()
Palette answered 6/10, 2012 at 19:33 Comment(0)
D
10

You can either do conn.list_domains() which will return a list of Domain objects for all of your current domains or you can do conn.lookup('foo') which will return a Domain object for the specified domain name.

Despicable answered 6/10, 2012 at 20:33 Comment(6)
Thanks! (I deleted a previous comment about this not working; it was operator error).Palette
calling conn.lookup('name') raises the following error: AttributeError: 'Layer2' object has no attribute 'lookup'. Searching through the code, I can't find a 'lookup' method within CloudSearch.Allhallowtide
Hmm. The cloudsearch.layer2 module definitely defines a lookup method. It has had it since 2012/08/22 when it was added with this commit (github.com/boto/boto/commit/…). Perhaps you are using an older version of boto?Despicable
Bah, just upgraded my boto to 2.9.5 and lookup is not found, I guess it's been moved since ?Fichu
It's still there. It's in boto/cloudsearch/layer2.py.Despicable
boto.connect_cloudsearch returns an instance of layer1. I've used boto.connect_cloudsearch2 which uses layer2 and has the lookup method.Tropism
S
7

This is the perfect solution. I am using boto 2.38.0

I had same issue which are faced by other. Then i made this script to connect aws search domain and get result

import boto.cloudsearch2
from boto.cloudsearch2.layer2 import Layer2
from boto.cloudsearch2.domain import Domain

# from boto.cloudsearch.domain import Domain
conn = boto.cloudsearch2.connect_to_region("xxxxxx",
                aws_access_key_id='xxxxxxxxxx',
                aws_secret_access_key='xxxxxxxxx')

domain_data =  conn.describe_domains('domaainname')

domain_data = (domain_data['DescribeDomainsResponse']
                          ['DescribeDomainsResult']
                          ['DomainStatusList'])

domain = Domain(conn, domain_data[0])
search_service = domain.get_search_service()
results = search_service.search(q="abc")

print map(lambda x: x, results)

Let me know any error. I hope this will work for all.

Segregationist answered 26/5, 2015 at 6:2 Comment(0)
B
2

Using boto 2.36, I got this working by taking a look at the source code.

import boto.cloudsearch
# login to AWS
conn = boto.connect_cloudsearch2(region="us-west-1",
                aws_access_key_id='xxxxx',
                aws_secret_access_key='xxxxx')


# get the right Domain:
domain = conn.lookup('toolbox')

print domain
Behlke answered 19/3, 2015 at 23:48 Comment(0)
M
0

this worked for me,
we have only one domain,
dom = Domain(con,con.describe_domains()[0])

Maladjustment answered 8/7, 2013 at 14:28 Comment(0)
S
0

I initially implemented the connection using the Layer2 approach:

Layer2(region='region name').lookup('domain name').

However, after some profiling I found the latency in creating a connection to be very high.

When I say very high, I mean the time to create a connection was rivaling the time to actually perform the query and get a response (> 500ms in most cases).

My solution, therefore, was to create the Domain directly. Note: this solution is brittle, but it does decrease latency significantly

You can create the domain by doing something like (many of these values can be found by doing aws cloudsearch describe-domains):

        domain = Domain(boto.cloudsearch2.connect_to_region('region name'), {
            'Created': True,
            'Deleted': False,
            'Processing': False,
            'RequiresIndexDocuments': False,
            'DomainId': 'domain_id',
            'DomainName': 'domain_name',
            'SearchInstanceCount': 2,
            'SearchPartitionCount': 1,
            'DocService': {
                'Endpoint': 'doc_service_endpoint',
            },
            'ARN': 'domain_arn',
            'SearchService': {
                'Endpoint': 'search_service_endpoint'
            }
        })
Sunup answered 7/6, 2017 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.