Adding a new nested object to an existing overall object


(Imran Siddique) #1

Hi guys,
I've a scenario where I'm storing file contents and path in ES. The file mapping is simple -
File Details = >
Contents
Content hash (this is also an id of the document)
File paths
File path =>
Folder Path
File name
File extension
I add the document first time if the same doesn't exists and update the File paths next time I come across a file with same content.

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

I'm using script ctx._source.paths += path where I'm passing path as a param. The insert is working fine as part of upsert and from # of deleted document I can see update is also happening. The only problem I've is I see only the last path seen for the file. Previous is getting replaced instead of a new entry getting added to the list.

Any pointers?
Thanks


(Imran Siddique) #2

Some more data. This is how the mapping looks like -

     "FileContract": {
        "_id": {
           "path": "documentId"
        },
        "properties": {
            "content": {
              "type": "string",
              "include_in_all": false
           },
           "contentId": {
              "type": "string",
              "index": "no",
              "store": true,
              "include_in_all": false
           },
           "documentId": {
              "type": "string",
              "index": "no",
              "include_in_all": false
           },
           "paths": {
              "type": "nested",
              "properties": {
                 "fileExtension": {
                    "type": "string",
                    "index": "not_analyzed",
                    "store": true,
                    "include_in_all": false
                 },
                 "fileExtensionId": {
                    "type": "double",
                    "store": true,
                    "include_in_all": false
                 },
                 "fileName": {
                    "type": "string",
                    "index": "not_analyzed",
                    "include_in_all": false
                 },
                 "filePath": {
                    "type": "string",
                    "index": "not_analyzed",
                    "include_in_all": false
                 },
                 "filePathOriginal": {
                    "type": "string",
                    "index": "not_analyzed",
                    "store": true,
                    "include_in_all": false
                 }
              }
           }
         }

This is how I'm constructing the descriptor -

                descriptor = descriptor.Update<T, T>(doc => doc
                    .IdFrom(item)
                    .Script("ctx._source.paths = ctx._source.paths + path")
                    .Params(p => p.Add("path", (item as FileContract).Paths[0])) // This is for testing purpose only. not a production code so using string literals and directly referring first element of array
                    .Upsert(item));

(system) #3