Tuesday, April 14, 2009

Recipe Search Results

‹prev | My Chain | next›

Currently, the search results from couchdb-lucene are of the format:
{
"q":"+_db:eee +all:wheatberries",
"etag":"1209e596ea8",
"skip":0,
"limit":25,
"total_rows":1,
"search_duration":2,
"fetch_duration":1,
"rows":[
{
"_id":"2008-07-19-oatmeal",
"score":0.5951423645019531
}
]
}
The _id is enough information to retrieve the recipe. To display things like the recipe title or the date on which it was added to the cookbook, a separate request for each recipe will be needed. On a search results page with 25, 50, or 100 results, that is 25, 50, or 100 separate requests of the CouchDB server. Luckily, that overhead is not necessary with lucene—it is possible to store computed values in the index.

The couchdb-lucene API for accomplishing this is doc.field('key', value, 'yes'), where doc is the lucene document instance, 'key' is the key for the field, value is the value associated with that key, and 'yes' indicates that the value should be stored in addition to indexed.

In the lucene design document that I am using, the local variable doc is the CouchDB record and ret is an instance of a lucene document. To add date and title to the search results, I add the following code:
  ret.field('date',  doc['date'],  'yes');
ret.field('title', doc['title'], 'yes');
Searching with that design document in place returns:
{"q":"+_db:eee +all:wheatberries",
"etag":"1209e596eaa",
"skip":0,
"limit":25,
"total_rows":1,
"search_duration":1,
"fetch_duration":1,
"rows":[
{
"_id":"2008-07-19-oatmeal",
"date":"2008/07/19",
"title":"Multi-grain Oatmeal",
"score":0.6080121994018555
}
]
}
Now I can display the date, title, and link to the recipe in the recipe search results without the need for a separate request.
(commit, adding this to the test DB)

No comments:

Post a Comment