Sample Application

  • tutorial
    +
    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. Discover how to program interactions with Couchbase Server 7.X via the Data, Query, and Search services — using the Travel Sample Application with the built-in Travel Sample data Bucket.

    Quick Start

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

    With Docker installed, you should now be able to run a bare-bones copy of Couchbase Server, load the travel-sample, add indexes, install the sample-application and its frontend, all by running a single command:

    docker-compose --profile local up

    Running the code against your own development Couchbase server.

    For Couchbase Server 7.6, 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.

    See the README at https://github.com/couchbaselabs/try-cb-ruby for full details of how to run and tweak the Ruby SDK travel-sample app.

    Using the Sample App

    Travel Sample Register

    Give yourself a username and password and click Register.

    You can now try out searching for flights, booking flights, and searching for hotels. You can see which Couchbase SDK operations are being executed by clicking the red bar at the bottom of the screen:

    Couchbase Query Bar

    Sample App Backend

    The backend code shows Couchbase Ruby 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 Ruby 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 get_airports(search_param)
      query_type = 'N1QL query - scoped to inventory: '
    
      query_prep = 'SELECT airportname FROM `travel-sample`.inventory.airport WHERE '
    
      same_case = search_param == search_param.downcase || search_param == search_param.upcase
      if same_case && search_param.length == 3
        query_prep += "faa=?"
        query_args = [search_param.upcase]
      elsif same_case && search_param.length == 4
        query_prep += "icao=?"
        query_args = [search_param.upcase]
      else
        query_prep += "POSITION(LOWER(airportname), ?) = 0"
        query_args = [search_param.downcase]
      end
    
      airport_list = []
      options = Cluster::QueryOptions.new
      options.positional_parameters(query_args)
    
      res = @cluster.query(query_prep, options)
      res.rows.each do |row|
        airport_list.push('airportname' => row['airportname'])
      end
    
      { 'context' => ["#{query_type} #{query_prep}"], 'data' => airport_list }
    end

    The travel.rb file also contains the functions for handling users, registration, and SQL++ queries.

    Data Model

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

    REST API

    You can explore the REST API here in read-only mode, or once you are running the application, at the /apidocs endpoint.