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.

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Marcello, thank you very much for the reply. I have tried your method and it works.
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.

5 Comments

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!
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.
You're very welcome! I'm glad you're enjoying couchdb-lucene :)
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.