As I was reading the CouchDB documentation I found it weird that views needed compaction while mango indexes did not. Are they not essentially the same thing and subject to the same requirement of cleaning out unused or old entries? It seems like an oversight to me.
I suppose I just need some clarification on how the index trees are different between them.
Thanks!
One may in fact compact a mango index because every index created at the /db/_index
endpoint1 has a "ddoc" (design doc) just like the design docs for map/reduce views.
Quoting from the /db/_index documentation,
Mango is a declarative JSON querying language for CouchDB databases. Mango wraps several index types, starting with the Primary Index out-of-the-box. Mango indexes, with index type json, are built using MapReduce Views.1
Now look to the /db/_compact/design-doc
2 endpoint's documentation*
Compacts the view indexes associated with the specified design document. It may be that compacting a large view can return more storage than compacting the actual db. Thus, you can use this in place of the full database compaction if you know a specific set of view indexes have been affected by a recent database change.
*Emphasis mine
Since every "mango index" has a design-doc, it follows that any mango index may be compacted with the /db/_compact/design-doc
endpoint.
This may be verified easily with curl. Say there is a mango index with ddoc="foo-json-index-ddoc"in the "stack" database,
curl -v -X POST -H "Content-Type: application/json" http://localhost:5984/stack/_compact/foo-json-index-ddoc
The verbose (succcessful) response will look like this
< HTTP/1.1 202 Accepted
< Cache-Control: must-revalidate
< Content-Length: 12
< Content-Type: application/json
< Date: Tue, 18 May 2021 14:30:33 GMT
< Server: CouchDB/2.3.1 (Erlang OTP/19)
< X-Couch-Request-ID: bbf2b7b0c9
< X-CouchDB-Body-Time: 0
<
{"ok":true}
* Connection #0 to host localhost left intact
I left authorization out for clarity.
[1] /db/_index
[2] /db/_compact/design-doc