如何使用python-cloudant调用带有键列表的_all_docs主索引?

时间:2017-01-12 10:07:58

标签: python cloudant python-cloudant

我目前正在使用主索引来查询Cloudant数据库中的键列表:

class DAO:

    @staticmethod
    def get_movie_names(movie_ids: List[int]) -> Dict[int, str]:

        # The movie_ids in cloudant are stored as strings so convert to 
        # correct format for querying
        movie_ids = [ str(id) for id in movie_ids ]

        keys = urllib.parse.quote_plus(json.dumps(movie_ids))

        # The movie id is stored in the _id field, so we query it 
        # using the 'keys' parameter
        end_point = '{0}/{1}/_all_docs?keys={2}&include_docs=true'.format (
                                                      CL_URL, CL_MOVIEDB, keys
                                                      )
        response = cloudant_client.r_session.get(end_point)
        movie_data = json.loads(response.text)

        movie_names = {}

        if 'rows' in movie_data:
            for row in movie_data['rows']:
                if 'doc' in row:
                    movie_id   = int(row['key'])
                    movie_name = row['doc']['name']
                    movie_names[movie_id] = movie_name
        return movie_names

看起来我可以使用cloudant.result.Result来实现这一目标。如果我已正确理解文档,这将返回所有文档,然后您可以过滤返回的结果。但是,我想通过将参数传递给Cloudant请求进行过滤,以便我只返回我感兴趣的数据。这可能吗?

1 个答案:

答案 0 :(得分:3)

克里斯 - CloudantDatabase可让您访问all_docs,这是您想要的吗?

http://python-cloudant.readthedocs.io/en/latest/database.html#cloudant.database.CouchDatabase.all_docs