我有一个RESTful API,我已在EC2实例上使用Elasticsearch的实现公开了索引内容的语料库。我可以通过从终端机(MacOSX)运行以下命令来查询搜索:
curl -XGET 'http://ES_search_demo.com/document/record/_search?pretty=true' -d '{ "query": { "bool": { "must": [ { "text": { "record.document": "SOME_JOURNAL" } }, { "text": { "record.articleTitle": "farmers" } } ], "must_not": [], "should": [] } }, "from": 0, "size": 50, "sort": [], "facets": {} }'
如何使用python/requests或python/urllib2(不确定要使用哪个请求- 一直在使用urllib2,但听说请求更好…)将以上转换为API请求?我是否可以通过标题?
python/requests
python/urllib2
使用请求:
import requests url = 'http://ES_search_demo.com/document/record/_search?pretty=true' data = '''{ "query": { "bool": { "must": [ { "text": { "record.document": "SOME_JOURNAL" } }, { "text": { "record.articleTitle": "farmers" } } ], "must_not": [], "should": [] } }, "from": 0, "size": 50, "sort": [], "facets": {} }''' response = requests.post(url, data=data)
然后,根据您的API返回的响应类型,您可能需要查看response.text或response.json()(或可能response.status_code先检查)。请参阅此处的快速入门文档,尤其是本节。
response.text
response.json()
response.status_code