Sample Application

    +
    Discover how to program interactions with the Couchbase Server via the data, query, and search services — using the Travel Sample Application with the built-in Travel Sample data Bucket.

    Travel Sample Application uses the Travel Sample data Bucket, which ships with Couchbase Server. For Couchbase Server 6.5, make sure that you have at least one node each of data; query; index; and search. For a development box, mixing more than one of these on a single node (given enough memory resources) is perfectly acceptable. If you have yet to install Couchbase Server in your development environment, start here.

    Then load up the Travel Sample Bucket, using either the Web interface or the command line. You will also need to create a Search Index — Query indexes are taken care of by the Sample Bucket.

    Preparation

    As well as the Python SDK 3.0 and Couchbase Server, set up as described above, you will need git to fetch the travel sample application code:

    git clone https://github.com/couchbaselabs/try-cb-python.git

    Change directory into your cloned repository, and check out the latest branch (this will most probably be enabled as the default branch).

    cd try-cb-python
    git checkout 6.5
    python3 -m pip install -r requirements.txt

    Running the Travel Sample Application

    Next, edit the travel.py file to reflect the username, password and host that you require. You can also specify command line arguments at runtime:

    python3 travel.py -c <host> -u <username> -p <password>

    Note that the sample project uses Flask as the web-framework, which you can read more about here.

    After the build, with your Web browser of choice, head to port 8080 of the local machine — http://localhost:8080.

    Using the Sample App

    Give yourself a username and password and click Register.

    Now try out a few queries, and see Search in action for the hotel finder feature.

    Sample App Backend

    The backend code shows Couchbase Python SDK in action with Query and Search, but also how to plug together all of the elements and build an application with Couchbase Server and the Python SDK.

    Here’s the airport search code, which checks to see whether the search term for the query string is a three or four letter FAA or ICAO abbreviation, and if not searches for it as an airport name:

    def findall(self):
        """Returns list of matching airports and the source query"""
        querystr = request.args['search']
        queryprep = "SELECT airportname FROM `travel-sample` WHERE "
        sameCase = querystr == querystr.lower() or querystr == querystr.upper()
        if sameCase and len(querystr) == 3:
            queryprep += "faa=$1"
            queryargs = [querystr.upper()]
        elif sameCase and len(querystr) == 4:
            queryprep += "icao=$1"
            queryargs = [querystr.upper()]
        else:
            queryprep += "POSITION(LOWER(airportname), $1) = 0"
            queryargs = [querystr.lower()]
    
        res = cluster.query(queryprep, *queryargs)
        airportslist = [x for x in res]
        context = [queryprep]
        response = make_response(
            jsonify({"data": airportslist, "context": context}))
        return response

    The travel.py file also contains the functions for handling users, registration, and N1QL queries.

    Data Model

    See the Travel App Data Model reference page for more information about the sample data set used.