Search code examples
databasenosqlcloudantnode-red

Search for multiple id:s in cloudant


I am using node-red to communicate with cloudant and for each time my flow runs I might have different amount of id:s coming in msg.payload. Later I want to use these id:s to display all the relevant objects. Is it possible to search for multiple id:s in some way? Or do you have any other solution? Can't find anything about this online atm


Solution

  • It looks like Node-RED supports querying by _id, a search index, or all documents. When you use _id there does not seem to be a way to specify more than one ID. You can use a search index, however, to query for multiple IDs.

    Create a search index in Cloudant similar to the following:

    {
      "_id": "_design/allDocSearch",
      "views": {},
      "language": "javascript",
      "indexes": {
        "byId": {
          "analyzer": "standard",
          "index": "function (doc) {\n  index(\"id\", doc._id);\n}"
        }
      }
    }
    

    This corresponds to the following when using the Cloudant dashboard:

    design doc = allDocSearch

    index name = byId

    index function =

    function (doc) {
      index("name", doc.name);
    }
    

    To search for multiple IDs your query would look something like this:

    id:"1" OR id:"2"
    

    In Node-Red set up your Cloudant node to point to the appropriate database, specify a "Search by" of search index, and configure your design document and index name (in this case it would be allDocSearch/byId).

    You can test with a simple inject node with a payload similar to the search query above: id:"1" OR id:"2"