0

I have the response in the following format after doing groupby in solr query. I am using solr version 3.5

"grouped":{
    "channel_id":{
      "matches":48,
      "ngroups":26,
      "groups":[{
          "groupValue":"204",
          "doclist":{"numFound":1,"start":0,"docs":[
              {
                "channel_name":"ZeeTv",
                "channel_num":4,
                "title":"The Name",
                "channel_id":"204"
          }},
        {
          "groupValue":"166",
          "doclist":{"numFound":2,"start":0,"docs":[
              {
                "channel_name":"Sony",
                "channel_num":2,
                "title":"The Name",
                "channel_id":"166",
              {
                "channel_name":"Sony",
                "channel_num":2,
                "title":"The Puzzle",
                "channel_id":"166"
          }}]}}

I am taking the response in an array in the following way :

for(var chl in data.grouped.channel_id.groups) {
    config['playlist'].push(data.grouped.channel_id.groups[chl]['doclist']['docs']); 
}

Thus an individual array of each groupValue is formed. The struture of the array is:

"0"=>{"0"=>"value"},"1"=>{"0"=>"result1","1"=>"result2"}

But i want to change the key name i.e. "0","1" to the groupValue from the response while creating an array so that i can do config['playlist']['166'] to check all the shows for this channel_id from the array. Can this be done and if so how. I am expecting the following :

"204"=>{"0"=>"value"},"166"=>{"0"=>"result1","1"=>"result2"}

Also if possible can the solr query be made such that the channel_num in the response comes in ascending order i.e. first result for channel_num 2 and then 4. I have done groupby:channel_id

user850234
  • 3,373
  • 15
  • 49
  • 83

2 Answers2

2

What about that?

for(var chl in data.grouped.channel_id.groups) {
    config['playlist'][data.grouped.channel_id.groups[chl].groupValue] = data.grouped.channel_id.groups[chl]['doclist']['docs'];
}

Push is there to add an element at the end of an array. But any Javascript object is just a hash table, so you can use it that way.

By the way, you can make the code simpler with a for each :

for each(var chl in data.grouped.channel_id.groups) {
    config['playlist'][ch1.groupValue] = ch1['doclist']['docs'];
}
Pik'
  • 6,819
  • 1
  • 28
  • 24
  • 1
    Your second code block is not valid javascript outside of firefox. Also, all of the for-each loops are missing a `hasOwnProperty` check. – st-boost Jun 06 '12 at 09:30
  • 2
    You shouldn't use `for`...`in` on arrays. Use `len = data.grouped.channel_id.groups.length; for (var chl=0; chl < len; ++chl )` instead. See discussion [here](http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript). – Mark Reed Jun 06 '12 at 10:09
2

Pikrass has answered correctly... as far as ordering with channel_num is concerned try adding the following in your query:

&sort=channel_num asc
user850234
  • 3,373
  • 15
  • 49
  • 83
Suryansh Purwar
  • 140
  • 1
  • 8