Monday, May 4, 2009

Sorting, Last Page

‹prev | My Chain | next›

With date sorting working as desired, all that is left in my sorting scenario is to get sorting by prep time and ingredients working:

Both preparation time and number of ingredients are numeric (integer) fields. In the olden days of Lucene, this would mean conversion to string and zero padding to get string sorting working (e.g. "0020", "0015", "0002"). Happily, Lucene now supports sorting on different data types and it attempts to infer data types as best as it can.

No new behavior is required for this effort, I am just re-using existing functionality. So all I should need to do is add a field or two and implement the remaining steps in the scenario. First up are the steps describing preparation time. The step for clicking column headers is already in place, so all I need to do is describe how the results should look after clicking the column header:
Then /^the results should be ordered by preparation time in ascending order$/ do
response.should have_selector("tr:nth-child(2) .prep",
:content => "1")
response.should have_selector("tr:nth-child(3) .prep",
:content => "2")
To get that working, I add the heading and data columns to the search results Haml template. Additionally, preparation time needs to be added to the couchdb-lucene index for retrieval and sorting:
  ret.field('sort_prep',  doc['prep_time'], 'yes', 'not_analyzed');
ret.field('prep_time', doc['prep_time'], 'yes');
While in there, I add the number of ingredients to a sortable field:
  ret.field('sort_ingredient', doc['preparations'].length, 'yes', 'not_analyzed');
With that (and an ingredients step similar to the preparation time step), the entire sorting scenario is done!


No comments:

Post a Comment