0

Hello everyone and thanks in advance for any ideas, suggestions or answers.

First, the environment: I am using CouchDB (currently developing on 1.0.2) and couchdb-lucene 0.7. Obviously, I am using couchdb-lucene ("c-l" hereafter) to provide full-text searching within couchdb.

Second, let me provide everyone with an example couchdb document:

{
   "_id": "5580c781345e4c65b0e75a220232acf5",
   "_rev": "2-bf2921c3173163a18dc1797d9a0c8364",
   "$type": "resource",
   "$versionids": [
       "5580c781345e4c65b0e75a220232acf5-0",
       "5580c781345e4c65b0e75a220232acf5-1"
   ],
   "$usagerights": [
       {
           "group-administrators": 31
       },
       {
           "group-users": 3
       }
   ],
   "$currentversionid": "5580c781345e4c65b0e75a220232acf5-1",
   "$tags": [
       "Tag1",
       "Tag2"
   ],
   "$created": "/Date(1314973405895-0500)/",
   "$creator": "administrator",
   "$modified": "/Date(1314973405895-0500)/",
   "$modifier": "administrator",
   "$checkedoutat": "/Date(1314975155766-0500)/",
   "$checkedoutto": "administrator",
   "$lastcommit": "/Date(1314973405895-0500)/",
   "$lastcommitter": "administrator",
   "$title": "Test resource"
}

Third, let me explain what I want to do. I am trying to figure out how to index the '$usagerights' property. I am using the word index very loosely because I really do not care about being able to search it, I simply want to 'store' it so that it is returned with the search results. Anyway, the property is an array of json objects. Now, these json objects that compose the array will always have a single json property.

Based on my understanding of couchdb-lucene, I need to reduce this array to a comma separated string. I would expect something like "group-administrators:31,group-users:3" to be a final output.

Thus, my question is essentially: How can I reduce the $usagerights json array above to a comma separated string of key:value pairs within the couchdb design document as used by couchdb-lucene?

A previous question I posted regarding indexing of tagging in a similar situation, provided for reference: How-to index arrays (tags) in CouchDB using couchdb-lucene

Finally, if you need any additional details, please just post a comment and I will provide it.

Community
  • 1
  • 1
Lucas
  • 478
  • 4
  • 15

2 Answers2

3

Maybe I am missing something, but the only difference I see from your previous question, is that you should iterate on the objects. Then the code should be:

function(doc) {
  var result = new Document(), usage, right;
  for(var i in doc.$usagerights) {
    usage = doc.$usagerights[i];
    for(right in usage) {
      result.add(right + ":" + usage[right]);
    }
  }
  return result;
}
Marcello Nuccio
  • 3,901
  • 2
  • 28
  • 28
2

There's no requirement to convert to a comma-separated list of values (I'd be intrigued to know where you picked up that idea).

If you simply want the $usagerights item returned with your results, do this;

ret.add(JSON.stringify(doc.$usagerights),
  {"index":"no", "store":"yes", "field":"usagerights"});

Lucene stores strings, not JSON, so you'll need to JSON.parse the string on query.

Robert Newson
  • 4,631
  • 20
  • 18
  • Robert, hey thanks for taking the time. I should have been more clear. As you stated, it needs to be a string, not JSON. I just added the comma requirement so that I would have an easy way to parse the returned "store" value within my program. I really appreciate the help. Thanks for your work on the project! – Lucas Sep 07 '11 at 23:14
  • Correct answer given here due to Robert's simplified code and inclusion of c-l fields (e.g., index, store, field). I had forgot about using the index field, so thanks for that reminder. – Lucas Sep 07 '11 at 23:19
  • You're very welcome! I'm glad you're enjoying couchdb-lucene :) – Robert Newson Sep 08 '11 at 09:31
  • I'll add a caveat here for those following my answer. CouchDB-Lucene doesn't define the JSON class, so you'll currently need to include it yourself to follow my suggestion above. Perhaps I can add this in 0.9 onwards, though. – Robert Newson Jan 10 '12 at 12:41
  • I upgraded to Rhino 1.7R3 which includes the JSON class (added a unit test to prove it), so this will work from 0.9.0 onward. – Robert Newson Jan 10 '12 at 13:06