Alfred Koen
Alfred Koen

Reputation: 15

Cloudant database search index

I have a Json document in cloudant as:

{
 "createdAt": "2022-10-26T09:16:29.472Z",
 "user_id": "4499c1c2-7507-4707-b0e4-ec83e2d2f34d",
 "_id": "606a4d591031c14a8c48fcb4a9541ff0"
}
{
 "createdAt": "2022-10-24T11:15:24.269Z",
 "user_id": "c4bdcb54-3d0a-4b6a-a8a9-aa12e45345f3",
 "_id": "fb24a15d8fb7cdf12feadac08e7c05dc"
}
{
 "createdAt": "2022-10-24T11:08:24.269Z",
 "user_id": "06d67681-e2c4-4ed4-b40a-5a2c5e7e6ed9",
 "_id": "2d277ec3dd8c33da7642b72722aa93ed"
}

I have created a index json as:

{
 "type": "json",
 "partitioned": false,
 "def": {
  "fields": [
   {
    "createdAt": "asc"
   },
   {
    "user_id": "asc"
   }
  ]
 }
}

I have created a index text as:

{
 "type": "text",
 "partitioned": false,
 "def": {
  "default_analyzer": "keyword",
  "default_field": {},
  "selector": {},
  "fields": [
   {
    "_id": "string"
   },
   {
    "createdAt": "string"
   },
   {
    "user_id": "string"
   }
  ],
  "index_array_lengths": true
 }
}

I have created a selctor cloudant query :

{
   "selector": {
      "$and": [
         {
            "createdAt": {
               "$exists": true
            }
         },
         {
            "user_id": {
               "$exists": true
            }
         }
      ]
   },
   "fields": [
      "createdAt",
      "user_id",
      "_id"
   ],
   "sort": [
      {
         "createdAt": "desc"
      }
   ],
   "limit": 10,
   "skip": 0
}

This code work fine inside the cloudant ambient.

My problem is in the Search Index.

I created this function code that works,

function (doc) {
  index("specialsearch", doc._id);
  if(doc.createdAt){
    index("createdAt", doc.createdAt, {"store":true})
  }
  if(doc.user_id){
    index("user_id", doc.user_id, {"store":true})
  }
}

result by this url:

// https://[user]-bluemix.cloudant.com/[database]/_design/attributes/_search/by_all?q=*:*&counts=["createdAt"]&limit=2
{
    "total_rows": 10,
    "bookmark": "xxx",
    "rows": [
        {
            "id": "fb24a15d8fb7cdf12feadac08e7c05dc",
            "order": [
                1.0,
                0
            ],
            "fields": {
                "createdAt": "2022-10-24T11:15:24.269Z",
                "user_id": "c4bdcb54-3d0a-4b6a-a8a9-aa12e45345f3"
            }
        },
        {
            "id": "dad431735986bbf41b1fa3b1cd30cd0f",
            "order": [
                1.0,
                0
            ],
            "fields": {
                "createdAt": "2022-10-24T11:07:02.138Z",
                "user_id": "76f03307-4497-4a19-a647-8097fa288e77"
            }
        },
        {
            "id": "2d277ec3dd8c33da7642b72722aa93ed",
            "order": [
                1.0,
                0
            ],
            "fields": {
                "createdAt": "2022-10-24T11:08:24.269Z",
                "user_id": "06d67681-e2c4-4ed4-b40a-5a2c5e7e6ed9"
            }
        }
    ]
}

but it doesn't return the id sorted by date based on the createdAt and user_id keys.

What I would like is to get a list of an organized search with the index of the createdAt and user_id keys without having to indicate the value; a wildcard type search

Where am I wrong?

I have read several posts and guides but I did not understand how to do it.

Thanks for your help.

Upvotes: 1

Views: 121

Answers (1)

Daniel Mermelstein
Daniel Mermelstein

Reputation: 1385

You say you want to return a list of id, createdAt and user_id, sorted by createdAt and user_id. And that you want all the documents returned.

If that is the case, what you need to do is simply create a MapReduce view of your data that emits the createdAt and user_id fields in that order, i.e. :

function (doc) {
  emit([doc.createdAt, doc.user_id], 1);
}

You don't need to include the document id because that comes for free.

You can then query the view by visiting the URL:

https://<URL>/<database>/_design/<ddoc_name>/_view/<view_name>

You will get all the docs like this:

{"total_rows":3,"offset":0,"rows":[
{"id":"2d277ec3dd8c33da7642b72722aa93ed","key":["2022-10-24T11:08:24.269Z","06d67681-e2c4-4ed4-b40a-5a2c5e7e6ed9"],"value":1},
{"id":"fb24a15d8fb7cdf12feadac08e7c05dc","key":["2022-10-24T11:15:24.269Z","c4bdcb54-3d0a-4b6a-a8a9-aa12e45345f3"],"value":1},
{"id":"606a4d591031c14a8c48fcb4a9541ff0","key":["2022-10-26T09:16:29.472Z","4499c1c2-7507-4707-b0e4-ec83e2d2f34d"],"value":1}
]}

Upvotes: 2

Related Questions