how to call _all_docs primary index with list of keys using python-cloudant?

370 Views Asked by At

I'm currently using the primary index to query for a list of keys in a Cloudant database:

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

It looks as though I may be able to achieve this using the cloudant.result.Result. If I've understood the documentation correctly, this will return all documents and you can then filter the returned results. However, I would like to filter by passing parameters to the Cloudant request so that I only return the data I'm interested in. Is this possible?

1

There are 1 best solutions below

1
On BEST ANSWER

Chris - CloudantDatabase gives you access to all_docs, is that what you want?

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