redmamoth
redmamoth

Reputation: 107

CouchDB searching linked documents

I'm very new to couchdb and i'm hoping someone can help me with a solution to this problem.

Say I have an address document that contains various keys, but importantly a singleLineAddress and a persons array:

{
  "_id": "002cb726bfe69a79ed9b897931000ec6",
  "_rev": "2-6af6d8896703e9db6f5ba97abb1ca5d7",
  "type": "address",
  ...
  "singleLineAddress": "28 CLEVEDON ROAD, WESTON-SUPER-MARE, BS23 1DG",
  ...
  "persons":["d506d09a1c46e32f6632e6d99a0062bd","002cb726bfe69a79ed9b897931001c80"]
}

Then i have a person document with a number of keys, crucially with firstName & lastName:

{
    "_id": "d506d09a1c46e32f6632e6d99a0062bd",
    "_rev": "4-98fae966a92d5c6c359cb8ddfaa487e1",
    "type": "person",
    ...
    "firstName": "Joe",
    "lastName": "Bloggs"
    ...
}

I understand I can created a linked document view and emit all the person id's linked to address, then I can use include_docs=true to see all the person data. But, from what i'm reading it's not advised to use include_docs=true as it can be expensive.

Ultimately, i'd like to use couchdb-lucene to run a FTS against person @ address using the name & address. Is that even possible using linked documents?

Upvotes: 0

Views: 53

Answers (1)

Glynn Bird
Glynn Bird

Reputation: 5637

Using ?include_docs=true is more expensive than not using it - for every row of the index returned, the database has to fetch the related document body. But sometimes needs must :) You can avoid using ?include_docs=true by "projecting" more data into the index which is returned to you at query time. See https://blog.cloudant.com/2021/11/12/Projection.html

As for Lucene full-text searching, you can certainly search across document types in the same collection but your search results would consist of a mixture of address and people documents - full text searching can't do the "join" between an address and its occupants - you'd have to do that yourself later.

If you desperately need to return address and people objects together, then consider combining the two: your address document would contain an array of people objects that reside there? There is a trade off between combining objects such that data the belongs together is stored together, and keeping every micro object separate for ease of updating.

Upvotes: 1

Related Questions