In Part 1 and Part 2 of the tutorial we described how to build a Node.js application on the CenturyLink Cloud Platform using our cloud products for deployment and data storage. The application is deployed in CenturyLink AppFog and uses CenturyLink Object Storage to store documents. Document metadata is stored in CenturyLink Orchestrate.

In this installment, we are going add search capabilities to the application using Orchestrate's powerful Search functionality.

Adding a Search Field to the Menu Bar

The first step is modifying the main layout for our application's views. We want a search field to be added to the menu bar so it's universally available throughout the application.

  1. Open views/layout.jade in your favorite text editor.
  2. Locate the line that says a(href='/documents/list') List Documents.
  3. Below that line, add the following code:
        form#searchbar(method='get', action='/documents/search')
        input(type='text', name='q', size='20', placeholder='Search term')
        input(type='submit', value='Search')
    

Note: Since Jade uses significant whitespace, make sure that the first line of the form is indented to match the line above it.

We also need to add a CSS rules to control the search bar's location in the menu bar.

  1. Open public/stylesheets/style.css in your text editor.
  2. At the end of the file, add this code:
        #searchbar {
           float: right;
        }
    

If you want to follow along with our already-completed example, download the code for this project. It is located in the Git repository. The code for this part of the tutorial is in the "part3" tag.

Clone the Git repository with the following commands:

       $ git clone https://github.com/pymander/clc-nodejs-tutorial
       $ cd clc-nodejs-tutorial
       $ git checkout tags/part3

Add a Search Route The next step is to add an Express application route to run search queries.

  1. Open routes/documents.js in your text editor.
  2. Locate the line at the end of the file which says module.exports = router;.
  3. Above that line, add the following code:

        /* SEARCH for documents using Orchestrate metadata */
        router.get('/search', function(req, res, next) {
          if (!req.user) { res.redirect('/'); }
    
          // Redirect to a plain document listing if there's no search query.
          if (!req.query.q) {
            res.redirect('/documents/list');
          }
    
          var tpl = { title : 'List Documents',
                      user : req.user,
                      session : req.session };
    
          // Build our query string.
          var queryString = 'title:"*XX*" OR keywords:"*XX*" OR filename:"*XX*"'.replace(/XX/g, req.query.q);
          tpl.querystring = queryString;
    
          db.search('documents', queryString, {
            sort: 'reftime.desc',
            limit: 20
          })
            .then(function (result) {
              tpl.items = result.body.results;
    
              res.render('list', tpl);
            })
            .fail(function (err) {
              throw new Error(err);
            });
        });
    

Note: Orchestrate uses Lucene Query Parser Syntax, which offers a great amount of flexibility and power. You can read more about Orchestrate's search capabilities in the API documentation.

Update the Document List View The search route uses the same view as the document list route. However, if a search is being executed, we want to display additional information for the end user.

  1. Open views/list.jade in your text editor.
  2. Find the line that says h1 =title.
  3. Below this line, add the following code:
        if querystring
          h2 Search query: #{querystring}
          p #{items.length} results found.
    

Note: Make sure that the first line in the new code is indented to the same column as the line above it.

Test and Deploy the Application

Testing the Application Now that we've added a search form, it's time to test the application.

  1. Run the following command in your project directory.
        $ npm start
    
  2. Visit http://localhost:3000/ in your web browser.
  3. Click Please sign in.
  4. Enter your username and password.
  5. Click Login.
  6. After logging in, perform a search using the search bar in the upper right hand corner. Search Bar Example

Deploying the Application Once you've confirmed that the application is working correctly, it's time to deploy it to the cloud.

Run the following command in your project directory to deploy to the cloud. Replace <your-project-name> with the name of your project.

        $ cf push <your-project-name>

Your updated application is now deployed.

The Next Step: Document Comments

Your application now offers a search interface that allows users to find the exact documents they're looking for. CenturyLink Orchestrate has powerful, fast search capabilities providing the exact performance you need. Our next step will be to implement a document comment feature that allows users to leave feedback and reviews for uploaded documents.

Links to the Complete Tutorial Series

Part 1 - Store and Authenticate User Credentials Part 2 - Build a Document Storage System Part 3 - Include Powerful Search Capabilities Part 4 - Add a Customized PDF Viewer and Comment System Part 5 - The Next Step for Your Web Application