问题
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()
回答1:
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.
回答2:
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.
回答3:
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
回答4:
this worked for me,
we have only one domain,
dom = Domain(con,con.describe_domains()[0])
回答5:
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'
}
})
来源:https://stackoverflow.com/questions/12763129/how-do-i-connect-to-an-existing-cloudsearch-domain-in-boto