Searching in a REST API

Andy Balaam
artificialworlds.net/blog

Contents

Recap

Recap

List poems (GET):
$ curl http://localhost:8080\
    /api/v1/poems

["this-is-a-photo", "a-question"]

This is it

The Twitter Way

/api/v1/search/poems.json?q=Robert%20Frost

Our Way

/api/v1/poems?search=Robert%20Frost

$ curl http://localhost:8080\
    /api/v1/poems\
    ?search=Robert%20Frost

['a-question']

Paging

/api/v1/poems?search=x&since_id=y

Implementation

def listpoems( ... search=None ):
    if search is not None:
        ids = _search_poems( db, search )
    else:
        ids = ( id for id in db.poems )

    ... paging code as before

Implementation

search_js = """function( doc ) {
    if( doc.author ) {
        if ( doc.author === '%s' ) {
            emit( doc._id, doc._id )
}}}"""

def _search_poems( db, search ):
    return ( res["value"] for
        res in db.poems.query(
            search_js % search ) )

Implementation notes

More info

Videos youtube.com/user/ajbalaam
Twitter @andybalaam
Blog artificialworlds.net/blog
Projects artificialworlds.net