Skip to content

Instantly share code, notes, and snippets.

@baphled
Created December 10, 2010 16:34
Show Gist options
  • Save baphled/736435 to your computer and use it in GitHub Desktop.
Save baphled/736435 to your computer and use it in GitHub Desktop.
I'm having problems trying to get my head around getting a collection of types along with number of times a skill is found in that doc type.
There are a number of document types that have a list of skills.
<pre><code>
{
"skills": "Windows, Network Admin, Linux",
"type": "Experience"
},
{
"skills": "Windows, Erlang, Linux",
"type": "Experience"
},
{
"skills": "Ruby, Rails, Erlang",
"type": "Project"
}
</code></pre>
I'm trying to get the number of times a skill is found in an document type.
The end result should look something like this:
<pre><code>
{
'type': Experience,
'skills': [
{'skill': 'Erlang', 'count': 1},
{'skill': 'Linux', 'count': 2},
{'skill': 'Network Admin', 'count': 1},
{'skill': 'Rails', 'count': 0},
{'skill': 'Ruby', 'count': 0},
{'skill': 'Windows', 'count': 2}
]
},
{
'type': Project,
'skills': [
{'skill': 'Erlang', 'count': 1},
{'skill': 'Linux', 'count': 0},
{'skill': 'Network Admin', 'count': 0},
{'skill': 'Rails', 'count': 1},
{'skill': 'Ruby', 'count': 1},
{'skill': 'Windows', 'count': 0}
]
}
</code></pre>
What would be the best way to do this?
@baphled
Copy link
Author

baphled commented Dec 11, 2010

Here's an example of the map/reduce I've been using.


    function(doc) {
      if (doc['skills'] != null) {
        var skills = doc['skills'].split(', ');
        for(skill in skills) {
          if ( skills[skill] != '' ) {
            emit([doc['couchrest-type'], skills[skill]], 1);
          }
        }
      }
    }

    function(keys, values, rereduce) {
        return {'type': keys[0][0][1],'entry': keys[0][0][2], 'count': sum(values)};
    }

The reduce function is a little hackish but it gets me close to the structure I am looking.


{type: "Experience", entry: "Erlang", count: 1}

but I also want something like


{type: "Project", 'skill': 'Windows', 'count': 0}

And this seems to be the main gap in my knowledge of couch.

@jchris
Copy link

jchris commented Dec 11, 2010

just change the reduce to "_count"

the structure wont be an exact match but the data will be

@baphled
Copy link
Author

baphled commented Dec 11, 2010

Cool, never knew about the _count call but it still yields essentially the same results though admittedly much nicer way :)

What the problem seems to be here is that I even though none of the Project docs have the Windows skill. I still need there to be an row that displays that a skill was not found in a specific doc type

{type: "Project", 'skill': 'Windows', 'count': 0}
.

I've come to the conclusion that I'd need a list for this, which would cross references the skills in an entry with the collective list of skills found. If the skill is not found then the count is set to zero.

@jchris
Copy link

jchris commented Dec 11, 2010

hmm you may be better off ignoring skills with 0 count, and when you draw the ui, if the particular count is missing then you can assume 0

@baphled
Copy link
Author

baphled commented Dec 11, 2010

I managed to manipulate the data with some jQuery and two separate views but it still feels as though I could do this in a list view combination.

I'll experiment with this later in the week but it would definately be a lot better if I didn't have to rely on client-side code to manipulate the data further.

@jchris
Copy link

jchris commented Dec 11, 2010

I don't understand how it's more code to that than adding a whole new _list piece.

however, if you use a list and are able to get it to directly output the HTML, that will be the fastest solution in the brower. But in production you'll need to run a squid or varnish in front of couch to hold the list runtime cost down

@baphled
Copy link
Author

baphled commented Dec 11, 2010

I must admit my JS is crappy and my knowledge of couch is still in noob territory, the solution I have at the moment is not as clean as I'd like but it works for now.

Thanks a lot for the guidance and help, its much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment