Utils/Elasticsearch.js

/**Copyright (c) 2009-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.**/
"use strict";const Database=require("./Database"),IndexNotFoundError=require("../Errors/IndexNotFoundError"),{Client:Client}=require("@elastic/elasticsearch"),{errors:elasticErrors}=require("@elastic/elasticsearch");
/** 
 * Class containing Elasticsearch Utils
 * @memberof mdxWebApiCore.Utils
 * @extends Database
 * */
class Elasticsearch extends Database{
/**
     * Constructor
     * @param {Object} connectionObject
     * @param {Map} configs
     */
constructor(e,t){super({name:"Elasticsearch",client:new Client(e),configs:t})}
/** 
      * @static 
      * @private
    */static#e=this.#t();
/**
      * Initializes all the index names in a map
      * @static 
      * @private
      * @returns {Map<string,string>} Returns map containing all the index names used by mdx.
     */
static#t(){let e=new Map;return e.set("calibration","calibration"),e.set("calibrationAudit","calibration-audit"),e.set("behavior","behavior-*"),e.set("clusterLabels","cluster-labels"),e.set("alerts","alerts-*"),e.set("roadNetwork","road-network"),e.set("tripwire","tripwire-*"),e.set("frames","frames-*"),e.set("mtmc","mtmc-*"),e.set("rtls","rtls-*"),e.set("amr","amr-*"),e.set("occupancyReset","occupancy-reset"),e.set("calibrationImages","calibration-images"),e.set("configs","configs"),e.set("configsAudit","configs-audit"),e}
/**
      * return elasticsearch errors
      * @public
      * @static
      * @returns {Object} Elasticsearch error is returned
      * @example
      * const mdx = require("@nvidia-mdx/web-api-core");
      * let elasticErrors = mdx.Utils.Elasticsearch.getElasticErrors();
     */static getElasticErrors(){return elasticErrors}
/**
     * Used to format elasticsearch result object
     * @public
     * @static
     * @param {Object} esResult Elasticsearch Result Object
     * @returns {Array<Object>} Array of documents obtained by searching Elasticsearch
     * @example
     * const mdx = require("@nvidia-mdx/web-api-core");
     * let resultList = mdx.Utils.Elasticsearch.searchResultFormatter(esResult);
     */static searchResultFormatter(e){let t=e.hits.hits;return t=t.map((e=>e._source)),t}
/**
     * Used to return index
     * @public
     * @static
     * @param {string} indexType 
     * @returns {string|undefined} Returns index
     * @example
     * const mdx = require("@nvidia-mdx/web-api-core");
     * let indexType = "behavior";
     * let index = mdx.Utils.Elasticsearch.getIndex(indexType);
     */static getIndex(e){return this.#e.get(e)}
/**
     * Initializes Elasticsearch ingest pipeline related to insert timestamp
     * @public
     * @static
     * @async
     * @param {Object} client
     * @param {string} targetTimestampField
     * @returns {Promise<{success:boolean}>} Returns success message if initialization is successful.
     * @example
     * const mdx = require("@nvidia-mdx/web-api-core");
     * const elastic = new mdx.Utils.Elasticsearch({node: "elasticsearch-url"},databaseConfigMap);
     * let result = await mdx.Utils.Elasticsearch.initTimestampIngestPipeline(elastic.getClient(),"timestamp");
     */static async initTimestampIngestPipeline(e,t){const s=`addInsertTimestamp-field-${t}`;return await e.ingest.putPipeline({id:s,body:{description:"Adds insert timestamp to documents",processors:[{set:{field:`_source.${t}`,value:"{{_ingest.timestamp}}"}},{date:{field:t,target_field:t,timezone:"UTC",formats:["ISO8601"]}}]}}),{success:!0}}
/**
     * returns Elasticsearch query result
     * @public
     * @static
     * @async
     * @param {Object} client
     * @param {Object} queryObject - queryObject
     * @param {boolean} [indexAbsentErr=true] - Throw an error when index doesn't exist
     * @returns {Promise<{body:?Object,indexAbsent:boolean}>} Elasticsearch result object
     * @example
     * const mdx = require("@nvidia-mdx/web-api-core");
     * const elastic = new mdx.Utils.Elasticsearch({node: "elasticsearch-url"},databaseConfigMap);
     * let result = await mdx.Utils.Elasticsearch.getSearchResults(elastic.getClient(),queryObject,false);
     */static async getSearchResults(e,t,s=!0){if((await e.indices.exists({index:t.index,allow_no_indices:!1})).body){return{body:(await e.search(t)).body,indexAbsent:!1}}if(s){let e=`Index: ${t.index} doesn't exist.`;throw new IndexNotFoundError(e)}return{body:null,indexAbsent:!0}}
/**
     * returns Elasticsearch doc count result
     * @public
     * @static
     * @async
     * @param {Object} client
     * @param {Object} queryObject - queryObject
     * @param {boolean} [indexAbsentErr=true] - Throw an error when index doesn't exist
     * @returns {Promise<{count:number,indexAbsent:boolean}>} Elasticsearch result object
     * @example
     * const mdx = require("@nvidia-mdx/web-api-core");
     * const elastic = new mdx.Utils.Elasticsearch({node: "elasticsearch-url"},databaseConfigMap);
     * let result = mdx.Utils.Elasticsearch.getDocCount(elastic.getClient(),queryObject,false);
     */static async getDocCount(e,t,s=!0){if((await e.indices.exists({index:t.index,allow_no_indices:!1})).body){return{count:(await e.count(t)).body.count,indexAbsent:!1}}if(s){let e=`Index: ${t.index} doesn't exist.`;throw new IndexNotFoundError(e)}return{count:0,indexAbsent:!0}}}module.exports=Elasticsearch;