Scripting (Javascript) method list


(Juraj) #1

Hi. I'm trying hard find more information about scripting in Elasticsearch but i found no information about method like add method (like piece of code here).

ctx._source.myNestedArrayField.add({..new object..});

I realize make update script with try/error method and found some useful info in comments but i find no comapct information about this topic.

Please is there any source/sources about this topic?

Thanks.

For curiosity my script (in lang javascript) for add new/update existing object on nested field.

    var logger = org.elasticsearch.common.logging.ESLoggerFactory.getLogger('JS-UPDATE-INDEX');

    ids = {};
    for (var i = 0; i < post_customers.length; i++) {
      ids[post_customers[i].classification_id] = i;
    }

    if (ctx._source.post_customers != null) {
      for (var j = 0; j < ctx._source.post_customers.length; j++) {
        if (!(ctx._source.post_customers[j].classification_id in ids)) continue;

        pos = ids[ctx._source.post_customers[j].classification_id];
        delete(ids[ctx._source.post_customers[j].classification_id]);

        logger.info('Update nested object POST_ID: ' + ctx._id + ' CLASSIFICATION_ID: ' + ctx._source.post_customers[j].classification_id);
        ctx._source.post_customers[j] = post_customers[pos];
      }
    }

    if (Object.keys(ids).length > 0) {
      logger.info('Insert nested object POST_ID: ' + ctx._id + ' CLASSIFICATION_IDS: ' + Object.keys(ids).join(','));
      for (id in ids) {
        if (ctx._source.post_customers == null) {
          logger.info('Empty: inserting ' + ids[id]);
          ctx._source.post_customers = [post_customers[ids[id]]];
        }
        else {
          logger.info('NotEmpty: inserting ' + ids[id]);
          ctx._source.post_customers.add(post_customers[ids[id]]);
        }
      }
    }

Thanks again.


(system) #2