couchdbcloudant

How to get the original document from the index


I want to index every fields from a set of documents, and then retrieve the documents when one of the fields match the query.

Here an example of documents:

{
    "_id": "1234567890",
    "lname": "last",
    "name": "name",
    "mpc": {
        "id": "000000",
        "name": "name"
    },
    "type": "agent",
    …
},
{
    "_id": "1234567891",
    "lname": "last",
    "name": "name",
    "mpc": {
        "id": "000001",
        "name": "name"
    },
    "type": "agent",
    …
}

Starting form the code taken from here: https://github.com/rnewson/couchdb-lucene#example-index-functions, I understand why I get the result, but I did not find out how to retrieve the original documents, and not the aggregates.

{
  "_id": "_design/search",
  "fulltext": {
    "all": {
      "index": "function(doc) { var ret = new Document(); function idx(obj) { for (var key in obj) { switch (typeof obj[key]) { case 'object': idx(obj[key]); break; case 'function': break; default: ret.add(obj[key], {\"store\": \"yes\"}); break; } } }; idx(doc); return ret;}"
    }
  }
}

So, when I try to query http://localhost:5985/local/database/_design/search/all?q=name, I get all the data flatten, but not the original document hierarchy.

Is there any built-in method that allow to get what I want?


Solution

  • According to the Search parameters usable here: https://github.com/rnewson/couchdb-lucene#search-parameters

    You can simply add to your query the following parameter: ?include_docs=true