Kapil
Kapil

Reputation: 103

Cloudant Secondary Index Query include_docs

What is a better approach of emitting the document value:

  1. Keeping the document as the value of the sec. index key, like emit(doc._id, doc) or
  2. Should the value be null and have the query include the include_docs.

As far as i understand it, include_docs=true will make another lookup query(internally) when secondary index is looked at, but on the other side, does the insert/update slow down with the doc being part of the secondary index value.

Any resource which serves as a exhaustive reference to what all can be done in secondary index, would be really useful.

Upvotes: 0

Views: 515

Answers (1)

seb
seb

Reputation: 818

Please have a look at the official couchdb docs: http://docs.couchdb.org/en/1.6.1/couchapp/views/intro.html

It is hard to determine what is better without knowing the use case. The map function you provided emits all the documents and it is useful if you need to fetch all the docs. The following scenarios describe some other cases you could consider:

  • If your documents are small and you are likely to search by certain field in the doc, like date, you can emit that field as the key and nothing as the value (view name could be by_date), define search criteria in the query and include docs:

    emit(doc.date)
    

    And a sample URL would be:

    http://host:port/db/_design/searches/_view/by_date?key="2012-01-5"&include_docs=true
    

    That gives you all documents matching search criteria straight away.

  • Similarly, you can emit the doc as a value and not use the 'include_docs' options:

    emit(doc.date, doc)
    

    And a sample url is now:

    http://host:port/db/_design/searches/_view/by_date?key="2012-01-5"
    

    Unfortunately I do not know what the performance difference between this and above is.

  • If your documents are huge and you are likely to present summaries before fetching a single document, (lets call that view by_date_summaries):

    emit(doc.date, {"id": doc._id, "title":doc.title, "info":doc.otherInfo});
    

    And a sample URL to get the summaries would be:

    http://host:port/db/_design/searches/_view/by_date_summaries?key="2012-01-5"
    

    Now you can show a list of summaries and once user selects one of them, you fetch the actual doc using the id emitted in the summary

There is obviously more mechanisms available: complex keys, reduce functions etc, but I think the above 2 cases are enough without knowing the exact use cases and they are a good starting point for exploring basic couchdb query mechanisms.

It is important to think about your use cases and design your data model and views upfront.

Upvotes: 0

Related Questions