Halfstop
Halfstop

Reputation: 1772

How to search and sort with CouchDB in one map function

I'm stumbling a bit with my CouchDB knowledge.

I have a database of content that is tagged with an array of tags and has a created date.

I want to create a view that pulls a limited number of newest stories tagged with a specific tag.

For example, the newest 6 stories tagged "Business."

Ran across this question, which seems to get me almost to where I need to go, but I'm missing one key element, which I think is how to craft the query string to sort by one key while searching by the other.

Here's my map function.

function(doc) {
    if (doc.published == "yes" && doc.type == "news") {
        for (var i = 0; i < doc.tags.length; i++) {
            if (doc.tags[i]) {
                emit([doc.created, doc.tags[i]], doc);
            }
        }
    }
}

So how do I query that view for a all documents tagged "Business" that are the newest documents based on created.

The created attribute is a date sortable format.

Upvotes: 3

Views: 1426

Answers (2)

Dominic Barnes
Dominic Barnes

Reputation: 28429

First, I would switch the order of your emit:

emit([doc.tags[i], doc.created]);

(leave out doc as well, you can just add include_docs=true to get the entire document, and your view won't take up so much disk-space in the process)

Now you can query for the all the stories tagged as "Business" by using the following querystring:

startkey=["Business"]&endkey=["Business",{}]

You'll get all the documents with the tag business, and they'll be sorted by date.

This takes advantage of view collation, which basically is the rules governing how indexes are sorted/queried. For complex keys like this, the sorting is done for each item of the array separately. (ie. the first key is sorted first, the second key is sorted second, etc) This is why the order matters, as you must always move from left to right when querying a view index.

If you want the 6 most recent, your querystring will need to change:

descending=true&limit=6&endkey=["Business"]&startkey=["Business",{}]

NOTICE You need to swap the startkey/endkey values, due to how the descending parameter works. See the View reference page on the wiki for further explanation.

Upvotes: 2

Halfstop
Halfstop

Reputation: 1772

OK, I think I figured this out, but I'm not quite certain I fully understand it.

I found this story about complex keys and searching and sorting.

My map function looks like this:

function(doc) {
    if (doc.published == "yes" && doc.type == "news") {
        for (var i = 0; i < doc.tags.length; i++) {
            if (doc.tags[i]) {
                emit([doc.tags[i], doc.created], doc);
            }
        }
    }
}

And to query and sort using it, the query looks like this.

http://localhost:5984/database/_design/story/_view/tagged?limit=10&startkey=["Business"]&endkey=["Business",{}]&descending=false

I'm getting the results I want, but I'm not entirely certain I understand it all.

Upvotes: 0

Related Questions