On Jan 21, 2007, at 11:54 PM, Yonik Seeley wrote:
On 1/21/07, Erik Hatcher <[EMAIL PROTECTED]> wrote:
In the built-in simple faceting, I get a Ruby response like this:

  'facet_counts'=>{
   'facet_queries'=>{},
   'facet_fields'=>{
        'subject_genre_facet'=>{
         'Biography.'=>2605,
         'Congresses.'=>1837,
         'Bibliography.'=>672,
         'Exhibitions.'=>642,
         'Periodicals.'=>615},
...

This is using &facet.limit=5 and no sort specified, so the items are
being written in the proper order, however they are written as a Ruby
Hash syntax, which does not iterate in a predictable order (like
Java's Map).  This really should be an Array in order for the client
to assume the response is in a specified order.  I think the response
is best formatted as:

  'facet_counts'=>{
   'facet_queries'=>{},
   'facet_fields'=>{
        'subject_genre_facet'=>[
         {'Biography.'=>2605},
         {'Congresses.'=>1837},
         {'Bibliography.'=>672},
         {'Exhibitions.'=>642},
         {'Periodicals.'=>615}],
...

This makes the navigation of the results a bit clunkier because each
item in a fields array is a single element Hash(Map), but the facets
of a field really need to be in an array to maintain order.

I presume this same dilemma is in the Python/JSON format too?  In
XML, the <lst> has the right semantics, and a parser would easily be
able to deal with it in order:

<lst name="facet_counts">
<lst name="facet_queries"/>
<lst name="facet_fields">
   <lst name="subject_genre_facet">
        <int name="Biography.">2605</int>
        <int name="Congresses.">1837</int>
        <int name="Bibliography.">672</int>
        <int name="Exhibitions.">642</int>
        <int name="Periodicals.">615</int>
   </lst>
...

Thoughts?


But isn't this considered a bug in the data structure used to write out the facets?

Options:
 - Resort yourself (for this specific case), it's probably going to
be faster than having eval() create a new hash object for each anyway.

For sure it'd be no problem to re-sort on the client. I was more concerned about the purity of the response and it losing its semantics as an ordered list.

 - Is there any place to hook into ruby's parser and create a map
that preserves it's order?  I would assume not.

Oh, I'm sure there is a way to accomplish that sort of trickery. Ruby is hyper dynamic, so I'm quite confident that with a little voodoo this could be done. But its the wrong way to approach this

 - Does ruby have a JSON parser that preserves the order?

Sure, even in one line of code :)

        <http://rubyforge.org/snippet/detail.php?type=snippet&id=29>

But even more robustly this looks like the one to use: http:// json.rubyforge.org/

 - A way to specify a different structure for only certain elements...

Again, shouldn't an array be used in this context anyway, rather than a hash, regardless of which response writer is being used?

If that's your *only* sorting problem, find a ruby implementation of a
hash or a map that preserves order, then re-sort the hash, and replace
it with the order-preserving map, and then worry about a more general
solution later?

Maybe what we need is a YAML response writer, and used an ordered map: http://yaml.org/type/omap.html

        Erik

Reply via email to