14
votes

I have the following two fields in my solr schema:

<field name="brand_id"     type="string" indexed="true" stored="true" />
<field name="category_id"  type="string" indexed="true" stored="true" />

When I make a request with facets enabled (faceting on brand_id) http://example.com/solr/select?wt=json&facet=true&facet.mincount=1&facet.field=brand_id&q=* :*

faceting output is returned in object notation:

"facet_counts": {
    "facet_queries": { }
    "facet_fields": {
         "brand_id": [
            {"1350492":14},
            {"1350578":12},
            {"1350600":11},
            {"1350617":8}
        ]
    }
}

However, repeating the same request using the 'category_id' as a facet field returns an array notation http://example.com/solr/select?wt=json&facet=true&facet.mincount=1&facet.field=category_id&q=* :*

"facet_counts":{
    "facet_queries":{},
    "facet_fields":{
        "category_id":[
            "230",20,
            "259",13,
            "154",12,
            "249",11
        ]
    }
}

Is there a way to force the object notation formatting? I am using Solr 3.6

-- UPDATE --

Using XML format returns correct results:

<lst name="facet_counts">
<lst name="facet_queries"/>
<lst name="facet_fields">
    <lst name="brand_id">
        <int name="269115">136</int>
        <int name="269394">110</int>
    </lst>


    <lst name="category_id">
        <int name="1348814">228</int>
        <int name="1350591">218</int>
    </lst>
3
what is the output when you change wt from json to xml?pensz
@pensz XML returns correct structure (I updated the question).mjalajel
Are you absolutely sure you're not adding json.nl=map to the first query and not the second?Bill Dueber
@BillDueber It's not set for neither of them. But knowing that this parameter exists answers my question. Thank You.mjalajel

3 Answers

10
votes

As Bill Dueber mentioned, You can format the JSON output using json.nl parameter. Check this page, https://wiki.apache.org/solr/SolJSON#JSON_specific_parameters

1
votes

If you're using the JSON response writer, you can add the json.nl parameter to the query string to format the facet counts.

json.nl=arrmap will format as [{"facetValue1": facetCount1}, {"facetValue2": facetCount2}].

json.nl=map will format as {"facetValue1": facetCount1, "facetValue2": facetCount2}.

Docs for the response writers, and specifically json.nl (nl = named lists) can be found here: https://cwiki.apache.org/confluence/display/solr/Response+Writers#ResponseWriters-JSON-SpecificParameters

0
votes

I experienced a similar thing a while back.

try adding the tag multiValued=false to the definition of the fields. I remember that fixing it for me.

You may have to reindex though.