3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

boto3でCloudSearch検索ドメインに複合クエリを実行する

Last updated at Posted at 2021-03-25

↑をboto3でやってみました。

cloudsearchdomain.py
import boto3

endpoint_url = 'https://search-todofuken-XXXXXXXXXXXXXXXXXXXXXXXXX.ap-northeast-1.cloudsearch.amazonaws.com'

cloudsearchdomain = boto3.client(
    'cloudsearchdomain', endpoint_url=endpoint_url)

query = """
(and 
  prefecture:'{prefecture}'
  city:'{city}'
  (range
    field=zip [{from_zip}, {to_zip}]
  )
)
""".format(prefecture='宮城県', city='仙台市', from_zip=9830000, to_zip=9830100)

returnFields = [
    'city',
    'kana_town',
]

results = cloudsearchdomain.search(
                query=query,
                queryParser='structured',
                returnFields=','.join(returnFields),
                size=3)

print('found:%d' % results["hits"]["found"])
print('count:%d' % len(results["hits"]["hit"]))

for item in results["hits"]["hit"]:
    print(item["fields"])
$ python cloudsearchdomain.py
found:34
count:3
{'city': ['仙台市宮城野区'], 'kana_town': ['タゴ']}
{'city': ['仙台市宮城野区'], 'kana_town': ['タゴニシ']}
{'city': ['仙台市宮城野区'], 'kana_town': ['ツルマキ']}

CloudSearchのboto3のサンプルコードあんまり転がってないので参考になれば。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?