SearchApp – Add a Searchbox to your CouchApps and CouchDB Databases in 10 Minutes
David Hardtke
February 03, 2011
Recently we released the developer preview of our full-text indexing and search for Cloudant’s hosted CouchDB. Here we will explain how to use a simple CouchApp to add search to your existing CouchApps and CouchDB databases. The goal is to make your data searchable in 10 minutes are less. Start your timers.
To get this running, you’ll need 4 things:
- Git version control system
- The couchapp command line tool is used to generate code templates in your application and to push your changes to an instance of CouchDB. This has been tested on couchapp version 0.6.1 and higher.
- Free Cloudant account
- An existing CouchDB database, CouchApp, or a data importer.
(Note: CouchApps are JavaScript and HTML5 applications served directly from CouchDB. couchapp is a command line tool used to create and manage CouchApps.)
If your CouchDB database is not already hosted at Cloudant, you’ll need to replicate it to Cloudant. I’m assuming your CouchDB database or CouchApp (named mydb) is on your local CouchDB instance (http://localhost:5984/mydb), and that your Cloudant username/password is <user>/<password>:
$ curl http://localhost:5984/_replicate -H 'Content-Type: application/json' -d '{ "source": "mydb", "target": "https://<user>:<pass>@<user>.cloudant.com/mydb", "create_target":true}'
Now, you’ll need to clone the SearchApp. To do this, use the git clone command:
$ git clone git@github.com:cloudant/SearchApp.git
$ cd SearchApp
For convenience, you can make your database the default target for your SearchApp. Modify the file .couchapprc as follows:
{"env":{"default":{"db":"http://<user>:<pass>@<user>.cloudant.com:5984/mydb"}}}
Now for the last step. Push your SearchApp on top of your database:
$ couchapp push
If you want to push to a different location that specified in the .couchapprc file, do:
$ couchapp push http://<user>:<pass>@<user>.cloudant.com:5984/mydb
You should see the following message:
2011-02-01 11:49:36 [INFO] Visit your CouchApp here:
http://<user>.cloudant.com:5984/mydb/_design/searchapp/index.html
Visit that page and you’ll have a working search box. Now check your watch – has it been more that 10 minutes? Your searches might come up blank at first if you have a large database. Your database needs to be indexed before it can be searched. The indexing is a distributed Map-Reduce view. You can check the status of indexing using the Cloudant dashboard at:
https://cloudant.com/#!/dashboard/<user>/mydb/stats
(be sure to insert your username in the url).
For a more details on configuring SearchApp, please look at the README file. To configure the indexing strategy, check out the search indexing page.