database - NoSQL Structure for handling labeled tags -


currently have hundreds of thousands of files so:

{     "_id": "1234567890",     "type": "file",     "name": "demo file",     "file_type": "application/pdf",     "size": "1400",     "timestamp": "1491421149",     "folder_id": "root" } 

currently, index names, , client can search files based on name of file. these files have tags need associated file have specific labels.

an example be:

{     "tags": [         { "client": "john doe" },         { "office": "virginia" },         { "ssn": "1234" }      ] } 

is adding tags array above file object ideal solution if want able search thousands of files client of john doe?

the other solution can think of having object per tag , having array of file id's associated each tag so:

{     "_id": "11111111",     "type": "tag",     "label": "client",     "items": [         "1234567890",         "1222222222",         "1333333333"     ] } 

with being lot of objects need add tags to, i'd rather efficient way possible first don't have backtrack in near future when start running issues.

any guidance appreciated.

your original design, tags array, works cloudant search: https://console.ng.bluemix.net/docs/services/cloudant/api/search.html#search.

with approach define single design document index tag in tags array. not have create different views different tags , can use lucene syntax queries: http://lucene.apache.org/core/4_3_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#overview.

so, using example, if have document looks tags:

{   "_id": "1234567890",   "type": "file",   "name": "demo file",   "file_type": "application/pdf",   "size": "1400",   "timestamp": "1491421149",   "folder_id": "root",   "tags": [     { "client": "john doe" },     { "office": "virginia" },     { "ssn": "1234" }   ] } 

you can create design document indexes each tag so:

{   "_id": "_design/searchfiles",   "views": {},   "language": "javascript",   "indexes": {     "bytag": {       "analyzer": "standard",       "index": "function (doc) {\n  if (doc.type === \"file\" && doc.tags) {\n    (var i=0; i<doc.tags.length; i++) {\n      (var name in doc.tags[i]) {\n        index(name, doc.tags[i][name]);\n      }\n    }\n  }\n}"     }   } } 

the function looks this:

function (doc) {   if (doc.type === "file" && doc.tags) {     (var i=0; i<doc.tags.length; i++) {       (var name in doc.tags[i]) {         index(name, doc.tags[i][name]);       }     }   } } 

then search this:

https://your_cloudant_account.cloudant.com/your_db/_design/searchfiles/_search/bytag ?q=client:jack+or+office:virginia &include_docs=true 

Comments