# Has many relationship in couchdb

**URL:** <https://discuss.elastic.co/t/has-many-relationship-in-couchdb/10895>\
**Category:** Elasticsearch\
**Created:** [February 26, 2013, 12:17am UTC](https://discuss.elastic.co/t/has-many-relationship-in-couchdb/10895 "2013-02-26T00:17:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![troy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/troy/32/2547_2.png) [@troy](https://discuss.elastic.co/u/troy)\
**Post date:** [February 26, 2013, 12:17am UTC](https://discuss.elastic.co/t/has-many-relationship-in-couchdb/10895/1 "2013-02-26T00:17:31Z")

</div>

I'm using couchdb to store user data. Users can belong to an arbitrary  
number of groups. Groups live in separate documents in couchdb. When I load  
a user, I load the user document from couchdb, load a list of all of the  
groups in the system, find the groups the user is associated with, and then  
return the user document and its associated group documents to the  
application layer for further processing. Here's an example of the two  
types of documents:

## User

{  
"\_id": "0c8cda925dd3f88a0d157e9c290013a4",  
"\_rev": "29-668e921b8f723f03188f60c44da24eaa",  
"type": "user",  
"disabled": false,  
"username": "hoyt",  
"firstName": "",  
"lastName": "",  
"emailAddress": "",  
"roles": [  
"user"  
]  
}

## Group

{  
"\_id": "3979684300c58a4c90c7c6e0d6033dce",  
"\_rev": "60-9f88939a2e667766a7c49d501046beb8",  
"type": "group",  
"name": "test",  
"description": "My description",  
"users": [  
"fbf8bc380b8cd64314bd7b4c65031d0d",  
"0c8cda925dd3f88a0d157e9c290013a4"  
]  
}

I'm thinking of ways I can allow searching for all the users in a group by  
group name in elastic search. For example, searching for 'test' would  
return all users in the group with the name 'test'.

I'm planning on using the couchdb-river plugin and adding a script filter.  
If it's possible, I suppose I could go through the same process in the  
script filter, i.e. when the river receives notification that a user  
document has changed it could query couch for a list of groups, find the  
groups the user is associated with, and then store the group names as a  
string field.

I've also looked at setting up a parent-child relationship in  
elasticsearch, but that seems a little scary and I'm not sure if it will  
work...🕶

Any advice on how to proceed is appreciated.

Thanks,

Troy

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 4, 2013, 6:58pm UTC](https://discuss.elastic.co/t/has-many-relationship-in-couchdb/10895/2 "2013-03-04T18:58:29Z")

</div>

Hi Troy,

I think that I will probably try to have only Users in my ES index.  
Because, in fact you seem to search for users, not for groups.

Can't you _redesign_ a bit you data?

Something like:  
{  
"\_id": "0c8cda925dd3f88a0d157e9c290013a4",  
"\_rev": "29-668e921b8f723f03188f60c44da24eaa",  
"type": "user",  
"disabled": false,  
"username": "hoyt",  
"firstName": "",  
"lastName": "",  
"emailAddress": "",  
"roles": [  
"user"  
],  
"groups": [  
{ "name": "test", "description": "My description" }  
]  
}

Does it make sense and fit to your use case?  
Why do you _really need_ a group type?

HTH

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 26 févr. 2013 à 01:17, [troy@scriptedmotion.com](mailto:troy@scriptedmotion.com) a écrit :

> I'm using couchdb to store user data. Users can belong to an arbitrary number of groups. Groups live in separate documents in couchdb. When I load a user, I load the user document from couchdb, load a list of all of the groups in the system, find the groups the user is associated with, and then return the user document and its associated group documents to the application layer for further processing. Here's an example of the two types of documents:
> 
> ## User
> 
> {  
> "\_id": "0c8cda925dd3f88a0d157e9c290013a4",  
> "\_rev": "29-668e921b8f723f03188f60c44da24eaa",  
> "type": "user",  
> "disabled": false,  
> "username": "hoyt",  
> "firstName": "",  
> "lastName": "",  
> "emailAddress": "",  
> "roles": [  
> "user"  
> ]  
> }
> 
> ## Group
> 
> {  
> "\_id": "3979684300c58a4c90c7c6e0d6033dce",  
> "\_rev": "60-9f88939a2e667766a7c49d501046beb8",  
> "type": "group",  
> "name": "test",  
> "description": "My description",  
> "users": [  
> "fbf8bc380b8cd64314bd7b4c65031d0d",  
> "0c8cda925dd3f88a0d157e9c290013a4"  
> ]  
> }
> 
> I'm thinking of ways I can allow searching for all the users in a group by group name in Elasticsearch. For example, searching for 'test' would return all users in the group with the name 'test'.
> 
> I'm planning on using the couchdb-river plugin and adding a script filter. If it's possible, I suppose I could go through the same process in the script filter, i.e. when the river receives notification that a user document has changed it could query couch for a list of groups, find the groups the user is associated with, and then store the group names as a string field.
> 
> I've also looked at setting up a parent-child relationship in elasticsearch, but that seems a little scary and I'm not sure if it will work...🕶
> 
> Any advice on how to proceed is appreciated.
> 
> Thanks,
> 
> Troy
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:48am UTC](https://discuss.elastic.co/t/has-many-relationship-in-couchdb/10895/3 "2017-07-06T02:48:28Z")

</div>


