Question

I am building a messaging system with Cloudant, comprised of 'message' and 'membership' documents:

message documents:

{"_id":"1","type"="message","group":"a","text":"this is message 1"},
{"_id":"2","type"="message","group":"a","text":"this is message 2"},
{"_id":"3","type"="message","group":"b","text":"this is message 3"},
...

membership documents:

{"_id":"a","type"="membership","user":"joe","group":"a"},
{"_id":"b","type"="membership","user":"bob","group":"a"},
{"_id":"c","type"="membership","user":"bob","group":"b"},
...

Each message is associated with one group. A user may have a membership in hundreds of different groups.

I would like to perform full text searches on the message text, on behalf of a particular user. The application requires that users must not see messages from groups that they are not members of.

how can I do a full text search that returns only messages from groups that a particular user belongs to?

Was it helpful?

Solution

You could try to do this by making a smart search string. To set this up, when you write your search index function, index the message's group as well as the message text. For instance:

function(doc){
  index("group", doc.group);
  index("text", doc.text);
}

Then, when querying this index, use the lucene syntax to build a search string containing all of the user's membership.

(group:a or group:b or group:c or ...) and text:"search string goes here"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top