okcapp
okcapp

Reputation: 405

query a Couchbase bucket from Python

I can see in the Couchbase admin console that my following Python code is putting the data/documents in the bucket:

def write_to_bucket(received_cluster, current_board, always_never, position):
    board_as_a_string = ''.join(['s', 'i', 'e'])

    cb = received_cluster.open_bucket('boardwascreated')
    cb.upsert(board_as_a_string,
          {'BoardAsString': board_as_a_string})

But then no matter what I do I can't query the data from Python. I try things like:

def database_query(receiving_cluster):
    cb = receiving_cluster.open_bucket('boardwascreated')

    for row in cb.n1ql_query('SELECT * FROM boardwascreated'):
        print(row)

I am trying every possible thing from https://docs.couchbase.com/python-sdk/2.5/n1ql-queries-with-sdk.html, but every time I try something I get the following error:

 No index available on keyspace boardwascreated that matches your query.
    Use CREATE INDEX or CREATE PRIMARY INDEX to create an index, 
    or check that your expected index is online.

Upvotes: 1

Views: 789

Answers (1)

Johan Larson
Johan Larson

Reputation: 1890

To run N1QL queries on a bucket, you need to an index on that bucket. The basic way to do that is to create a primary index on it.

Open your admin console and go to the Query WorkBench. Once you're in the admin console, you should see a "Query" tab on the left. Then run this command to create the primary index.

CREATE PRIMARY INDEX ON boardwascreated

You will also need to supply username/password credentials in order to access the bucket. Initially, you can use whatever Administrator/admin_password combination you have created. I'm not sure off-hand how to supply that using the Python SDK; check the docs.

Later, you should go to the Security tab and create a specialized user for whatever application you are building and give that user whatever query permissions they need.

Upvotes: 2

Related Questions