You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Jack L <jl...@yahoo.ca> on 2007/05/03 03:09:50 UTC

facet.sort does not work in python output

When facet.sort is used, the facet fields are sorted by the count
in the reply string when using python output. However, after calling
eval(), the sort order seems to be lost. Not sure if anyone has come
up with a way to avoid this problem.

Using the JSON output with a JSON parser for Python should work but
I haven't tested it yet.

-- 
Best regards,
Jack


Re: facet.sort does not work in python output

Posted by Yonik Seeley <yo...@apache.org>.
On 5/3/07, Mike Klaas <mi...@gmail.com> wrote:
> On 5/3/07, Jack L <jl...@yahoo.ca> wrote:
> > The Python output uses nested dictionaries for facet counts.
>
> This might be fixed in the future

It's fixed in the current development version (future 1.2), already.
See http://wiki.apache.org/solr/SolJSON
which is the "base" for both Python and Ruby.

The default is json.nl=flat which results in alternating term and
count in a flat array.

"facet_fields":{
        "cat":[
         "electronics",3,
         "card",2,
         "graphics",2,
         "music",1]}},

-Yonik

Re: Re[2]: facet.sort does not work in python output

Posted by Mike Klaas <mi...@gmail.com>.
On 5/4/07, Jack L <jl...@yahoo.ca> wrote:
> I use this to sort the facet field values against count in
> reverse order in Python:
>
> sorted(facet_field_values.items(), lambda x, y: cmp(x[1], y[1]), reverse = True)

FWIW, the key= parameter is generally more efficient for python 2.4+:

sorted(facet.field_values.items(), key=lambda x: x[1], reverse=True)

or even
from operator import itemgetter
sorted(facet.field_values.items(), key=itemgetter(1), reverse=True)

digressionally,
-Mike

Re[2]: facet.sort does not work in python output

Posted by Jack L <jl...@yahoo.ca>.
I use this to sort the facet field values against count in
reverse order in Python:

sorted(facet_field_values.items(), lambda x, y: cmp(x[1], y[1]), reverse = True)

Thursday, May 3, 2007, 6:18:05 PM, you wrote:

> We resort it in solr-ruby:

>    def field_facets(field)
>      facets = []
>      values = @data['facet_counts']['facet_fields'][field]
>      Solr::Util.paired_array_each(values) do |key, value|
>        facets << FacetValue.new(key, value)
>      end

>      facets
>    end



> On May 3, 2007, at 8:10 PM, Mike Klaas wrote:

>> On 5/3/07, Jack L <jl...@yahoo.ca> wrote:
>>> The Python output uses nested dictionaries for facet counts.
>>> I read it online that Python dictionaries do not preserve order.
>>> So when a string is eval()'d, the sorted order is lost in the
>>> generated Python object. Is it a good idea to use list to wrap
>>> around the dictionary? This is only needed for the fields, sorted
>>> by counts.
>>
>> This might be fixed in the future, but for now, either resort on the
>> client-side (a one- or zero-liner), or specify json.nl=arrarr (which
>> affects the whole python response structure... probably not
>> recommended).
>>
>> There is some past discussion on the list if you search the archives.
>>
>> -Mike


Re: facet.sort does not work in python output

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
We resort it in solr-ruby:

   def field_facets(field)
     facets = []
     values = @data['facet_counts']['facet_fields'][field]
     Solr::Util.paired_array_each(values) do |key, value|
       facets << FacetValue.new(key, value)
     end

     facets
   end



On May 3, 2007, at 8:10 PM, Mike Klaas wrote:

> On 5/3/07, Jack L <jl...@yahoo.ca> wrote:
>> The Python output uses nested dictionaries for facet counts.
>> I read it online that Python dictionaries do not preserve order.
>> So when a string is eval()'d, the sorted order is lost in the
>> generated Python object. Is it a good idea to use list to wrap
>> around the dictionary? This is only needed for the fields, sorted
>> by counts.
>
> This might be fixed in the future, but for now, either resort on the
> client-side (a one- or zero-liner), or specify json.nl=arrarr (which
> affects the whole python response structure... probably not
> recommended).
>
> There is some past discussion on the list if you search the archives.
>
> -Mike


Re: facet.sort does not work in python output

Posted by Mike Klaas <mi...@gmail.com>.
On 5/3/07, Jack L <jl...@yahoo.ca> wrote:
> The Python output uses nested dictionaries for facet counts.
> I read it online that Python dictionaries do not preserve order.
> So when a string is eval()'d, the sorted order is lost in the
> generated Python object. Is it a good idea to use list to wrap
> around the dictionary? This is only needed for the fields, sorted
> by counts.

This might be fixed in the future, but for now, either resort on the
client-side (a one- or zero-liner), or specify json.nl=arrarr (which
affects the whole python response structure... probably not
recommended).

There is some past discussion on the list if you search the archives.

-Mike

Re: facet.sort does not work in python output

Posted by Jack L <jl...@yahoo.ca>.
The Python output uses nested dictionaries for facet counts.
I read it online that Python dictionaries do not preserve order.
So when a string is eval()'d, the sorted order is lost in the
generated Python object. Is it a good idea to use list to wrap
around the dictionary? This is only needed for the fields, sorted
by counts.

-- 
Best regards,
Jack

Wednesday, May 2, 2007, 6:09:50 PM, you wrote:


> When facet.sort is used, the facet fields are sorted by the count
> in the reply string when using python output. However, after calling
> eval(), the sort order seems to be lost. Not sure if anyone has come
> up with a way to avoid this problem.

> Using the JSON output with a JSON parser for Python should work but
> I haven't tested it yet.