Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ESClient

Hierarchy

  • ESClient

Index

Methods

attachAlias

  • attachAlias(indexName: string, alias: string): void
  • attach concrete index with alias

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    client.getAlias("my_index", "my_index_alias");
    // my_index_alias was attached to my_index
    
    

    Parameters

    • indexName: string

      concrete index

    • alias: string

      the name of alias

    Returns void

attachAndDetachAlias

  • attachAndDetachAlias(attachIndexName: string, detachIndexName: string, alias: string): void
  • attachAndDetachAlias attach and detach alias in one request, used to smoothly convert indexes within one alias

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    client.detachAlias("new_index", "old_index", "my_index_alias");
    // my_index_alias was detached from old_index and attached to new_index
    
    

    Parameters

    • attachIndexName: string

      index to attach

    • detachIndexName: string

      index to detach

    • alias: string

      the name of alias

    Returns void

bulkDeleteDocs

  • bulkDeleteDocs(indexName: string, docIdList: string[]): void
  • delete multiple documents in the specified index

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let docIds = ["1001", "1002", "1003"];
    client.bulkDeleteDocs('indexName', docIds); // docs with id in the docId list will be deleted if exists
    

    Parameters

    • indexName: string

      the specify index name

    • docIdList: string[]

      array of doc ids to be deleted

    Returns void

bulkIndexChildDocs

  • bulkIndexChildDocs(indexName: string, routing: string, records: Dict[]): void
  • import * as es from 'es'; let esclient = es.newClient('esconnect')

    let indexName1 = "my_testIndex"

    // 创建父索引question,映射子索引answer let m = { "properties": { "my_join_field": { "type": "join", // 指定这是一个父子文档 "relations": { // 设置父子关系 "question": "answer" // question是父文档,answer是子文档 } } } } esclient.createIndexWithMapping("my_testIndex", m)

    // 插入父文档数据 let doc = { "_id": "1", // 父文档ID,这里举例为1 "text": "This is a question", // 本样例的父文档只有一个属性字段text "my_join_field": { "name": "question" // 指明是question父文档 } } esclient.indexDoc('my_testIndex', doc) // 插入一条数据,普通的indexDoc插入接口 esclient.refresh('my_testIndex') // 新增数据后刷新索引后可查看

    let docs = [] for (let i = 0; i < 3; i++) { //批量创建数据, docs.push({ // "_id": (i).toString(), // 子文档ID,不写时是个随机数 "text": "This is an answer" + i.toString(), // 子文档内容,同样也只能写一个字段text "my_join_field": { "name": "answer", // 指明这是answer子文档; "parent": "1" // 指明其父文档的 Id; } }) } esclient.bulkIndexChildDocs(indexName1, "1",docs) // 批量插入,结果将是一个question对应多个answer的索引

    Parameters

    • indexName: string
    • routing: string
    • records: Dict[]

    Returns void

bulkIndexDocs

  • bulkIndexDocs(indexName: string, docs: Dict[]): void
  • insert or full-doc update multiple documents in the specified index Notice: if one doc contains field "_id", the value will be used as self-defined Doc ID, "_id" itself will not be stored in to search engine If document with self-defined Doc ID is already exist, this operation will perform a full-doc update, otherwise, insert the document with self-defined Doc ID If no "_id" specified, document will be inserted with a random Doc ID.

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let docs = [
       {
          "_id": "1001",   //this document has specific doc id
          "name": "Joe",
          "age": 25
       },
       {
          "name": "Amy",   //this document has auto-generated doc id
          "age": 35
       }
    ]
    client.bulkIndexDocs('indexName', docs)
    

    Parameters

    • indexName: string
    • docs: Dict[]

    Returns void

cat

  • cat show indexes basic information

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    let cats = client.cat();
    
    

    Returns CatInfo[]

    basic info for each index, for example:

    [
     {
          "IndexName": "foo",
          "Aliases": null,
          "DocCount": 2
      } {
          "IndexName": "bar",
          "Aliases": [
              "a"
          ],
          "DocCount": 0
      }
    ]
    

checkDocExist

  • checkDocExist(indexName: string, docID: string): boolean
  • check if document is existing

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    
    let indexName = "my_index";
    let docId = "1001";
    let hasDoc = client.checkDocExist(indexName, docId);
    

    Parameters

    • indexName: string
    • docID: string

    Returns boolean

checkIndexExist

  • checkIndexExist(indexName: string): boolean
  • check whether the specified index is existing

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    let hasIndex = client.checkIndexExist("my_index");
    // hasIndex is true if index "my_index" exists, otherwise false
    
    

    Parameters

    • indexName: string

    Returns boolean

    boolean true for exist, otherwise false

clearScroll

  • clearScroll(scrollID: string): void
  • clear scrollID

    游标查询三个接口:scrollSearch,scrollIDSearch,clearScroll 需一起用,详细用例见scrollSearch

    example
    
    client.clearScroll(result.ScrollID) //scrollID from scrollSearch
    
    

    Parameters

    • scrollID: string

    Returns void

countDocs

  • countDocs(indexName: string): number
  • count documents in an index

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    let num = client.countDocs("my_index");
    
    

    Parameters

    • indexName: string

    Returns number

    documents count

countDocsByCondition

  • countDocsByCondition(indexName: string, dsl: Dict): number
  • count documents by dsl

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let queryDSL = {
        range: {
            foo: {
                gt:  10
            }
        }
    }
    let count = client.countDocsByCondition('indexName', queryDSL)
    

    Parameters

    • indexName: string
    • dsl: Dict

      condition in ES dsl format

    Returns number

    documents count

countDocsByQuery

  • countDocsByQuery(indexName: string, query: string): number
  • count documents by light-weight query string

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let count = client.countDocsByQuery('indexName', 'value1 OR field2:value2')
    

    Parameters

    • indexName: string
    • query: string

      light-weight query string

    Returns number

    documents count

createIndex

  • createIndex(indexName: string): void
  • create a new index

    example
    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    client.createIndex("my_index");
    
    

    Parameters

    • indexName: string

    Returns void

createIndexWithMapping

  • createIndexWithMapping(indexName: string, mapping: Dict): void
  • create a new index with mapping

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    let m = {
       dynamic: "strict",
       properties: {
            name: {
                 properties: {
                     first_name: {
                        type: "text",
                     },
                     last_name: {
                        type: "text",
                        analyzer: "english"
                     }
                 }
            },
            age: {
                type: "integer"
            }
       }
    }
    client.createIndexWithMapping("my_index", m)
    

    Parameters

    • indexName: string
    • mapping: Dict

    Returns void

deleteByQuery

  • deleteByQuery(indexName: string, dsl: Dict): number
  • delete documents by query

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let query = {
         range: {
             foo: {
                 gt:  10
             }
         }
    }
    let deletedCount = client.deleteByQuery('indexName', query)
    

    Parameters

    • indexName: string
    • dsl: Dict

    Returns number

    deleted count

deleteDocByDocID

  • deleteDocByDocID(indexName: string, docID: string): void
  • delete a document by document id docID is got by search or query interface, which is named as _id. see the query interface for more details

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let docId = '1001'; // suppose that you've got the docId in some way
    client.deleteDocByDocID('indexName', docId)   //doc with id '1001' is deleted if exists
    

    Parameters

    • indexName: string

      the specify index

    • docID: string

      the specify document id

    Returns void

detachAlias

  • detachAlias(indexName: string, alias: string): void
  • detachAlias detach concrete index with alias

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    client.detachAlias("my_index", "my_index_alias");
    // my_index_alias was detached
    
    

    Parameters

    • indexName: string

      concrete index

    • alias: string

      the name of alias

    Returns void

dropIndex

  • dropIndex(indexName: string): void
  • delete an index. Notice: cannot delete alias directly

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    client.dropIndex("my_index");
    // index name 'my_index' was dropped
    
    

    Parameters

    • indexName: string

      the index to be dropped

    Returns void

getAlias

  • getAlias(indexName: string): string[]
  • input index name return associated aliases

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    let aliases = client.getAlias("my_index");
    // aliases of index named "my_index" were returned
    
    

    Parameters

    • indexName: string

    Returns string[]

    associated aliases

getAllIndexes

  • getAllIndexes(): string[]
  • get all indexes for the current tenant

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    let indexes = client.getAllIndexes(); // an array of indexes was returned
    
    

    Returns string[]

    index name list

getAttachedIndexes

  • getAttachedIndexes(alias: string): string[]
  • get attached concrete indexes by alias

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    let indexes = client.getAttachedIndexes("my_index_alias");
    // indexes attached to "my_index_alias" were returned
    
    

    Parameters

    • alias: string

      the index name which created in connector

    Returns string[]

    attached indexes

getDocByDocID

  • getDocByDocID(indexName: string, docID: string): Dict
  • get one document by document id

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let indexName = "my_index";
    let docId = "1001";
    let doc = client.getDocByDocID(indexName, docId);
    
    

    Parameters

    • indexName: string

      the specify index

    • docID: string

      the specify document id

    Returns Dict

    document the doc

getMapping

  • getMapping(indexName: string): Dict
  • getMapping get mapping for an index or alias

    example
    
    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    let m = client.getMapping("new_index");
    
    @param indexName the index Name or alias
    @return mapping for an index
    ```json
    {
      "age": {
         "type": "integer"
      },
      "name": {
         "dynamic": "strict",
         "properties": {
             "first_name": {
                  "type": "text"
             },
             "last_name": {
                  "type": "text"
              }
          }
       }
    }
    

    Parameters

    • indexName: string

    Returns Dict

indexChildDoc

  • indexChildDoc(indexName: string, routing: string, doc: Dict): string
  • import * as es from 'es'; let esclient = es.newClient('esconnect')

    let indexName1 = "my_testIndex"

    // 创建父索引question,映射子索引answer let m = { "properties": { "my_join_field": { "type": "join", // 指定这是一个父子文档 "relations": { // 设置父子关系 "question": "answer" // question是父文档,answer是子文档 } } } } esclient.createIndexWithMapping("my_testIndex", m)

    // 插入父文档数据 let doc = { "_id": "1", // 父文档ID,这里举例为1 "text": "This is a question", // 本样例的父文档只有一个属性字段text "my_join_field": { "name": "question" // 指明是question父文档 } } esclient.indexDoc('my_testIndex', doc) // 插入一条数据,普通的indexDoc插入接口 esclient.refresh('my_testIndex') // 新增数据后刷新索引后可查看

    // 插入一条子文档数据 let routeDoc = { "_id": "5", // 不写时会是个随机数 "text": "This is an answer1", "my_join_field": { "name": "answer", // 指明这是answer子文档; "parent": "1" // 指明其父文档的 Id; } } esclient.indexChildDoc('my_testIndex', "1", routeDoc) esclient.refresh('my_testIndex')

    Parameters

    • indexName: string
    • routing: string
    • doc: Dict

    Returns string

indexDoc

  • indexDoc(indexName: string, doc: Dict): string
  • insert or fully update a document in the specified index Notice: if doc contains field "_id", the value will be used as self-defined Doc ID, "_id" itself will not be stored in to search engine If document with self-defined Doc ID is already exist, this operation will perform a full-doc update, otherwise, insert the document with self-defined Doc ID If no "_id" specified, document will be inserted with a random Doc ID.

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let doc = {
        "_id": "1001",
        "name": "Joe",
        "age": 25
    }
    let docID = client.indexDoc('indexName', doc)   //docID is supposed to be "1001" in this case
    

    Parameters

    • indexName: string

      the specify index

    • doc: Dict

      the document to be indexed.

    Returns string

    inserted document id

query

  • query(queryString: string, getAll: boolean, ...indexName: string[]): Dict[]
  • provides light-weight querying. You can use * to match zero or more characters

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let docs = client.query('value1 OR field2:value2', true, 'indexName')
    

    Parameters

    • queryString: string

      传参如: "foo", "foo:bar", "foo:bar AND qux:baz".

    • getAll: boolean

      true时默认最多返回5000条数据,可改osql的elasticsearch_querydocs_size值(最多10万)返回更多, 如果还需要更多数据,建议用scrollSearch接口, false时返回10条数据。

    • Rest ...indexName: string[]

      可以传入多个索引名。如果没有传入indexName,则搜索所有索引。

    Returns Dict[]

    已创建文档数组。例如:

    [
     {
          "_id": "fGGoNGoBvz7NQ98rl2Sn",
          "_index": "user-defined-index-name",
          "field1": "apple",
          "field2": "orange"
     },
     {
          "_id": "CTZiOGoBZQ34Z4YeRxSv",
          "_index": "user-defined-index-name",
          "field1": "banana",
          "field2": "coconut"
     }
    ]
    

refresh

  • refresh(indexName: string): void
  • make documents in specified index available for search immediately 每次索引的数据发生增删改后,可以选择手动刷新索引,使变化内容立即可被搜索到(否则会等约1秒时间)

    Notice: This operation is resource-intensive. To ensure good performance, recommend to wait for periodic refresh(usually 1s) rather than performing an explicit refresh when possible. 重要: 当写测试的时候,手动刷新或许有用,但在生产环境下每次增删改一个文档就调用refresh函数会产生很大的性能开销 不用担心更新的数据无法被搜索,通常集群会在1秒钟内自动进行刷新。所以在开发时请意识到 Elasticsearch 的近实时搜索的特性,并接受它的不足 仅仅在非常需要进行实时搜索的情况下,再选择调用refresh接口

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    // It's recommend to wait for periodic refresh(usually 1s) rather than performing an explicit refresh when possible.
    client.refresh('indexToBeRefresh'); // docs within the specify index would be refreshed
    
    

    Parameters

    • indexName: string

    Returns void

reindex

  • reindex(oldIndex: string, newIndex: string): void
  • reindex copy old index to new index

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance'); // 要修改为云搜索连接器名字
    // 重建索引mapping,完成数据从旧索引到新索引的转移,且支持在新索引新增字段
    let oldIndex = "test"
    let existOldIndex = client.checkIndexExist(oldIndex) // 检查索引是否存在
    if (existOldIndex) { client.dropIndex(oldIndex) } // 存在就删除重复索引,这里测试用例写了删除索引方法,实际情况要注意
    let m1 = {
         properties: {
             name: {
                 type: "text",
             },
             age: {
                type: "integer"
             }
         }
     }
      client.createIndexWithMapping("test", m1) // 测试给test赋值m1结构
      let doc = {
     "name": "Joe",
     "age": 25
     }
      let docID = client.indexDoc('test', doc)   // 插入一条数据测试
      client.refresh('test') // 新增数据后需刷新索引
    
      let newIndex = "newTest"
      let exist = client.checkIndexExist(newIndex) // 检查索引是否存在
      if (exist) { client.dropIndex(newIndex) } // 存在就删除重复索引,这里测试用例写了删除索引方法,实际情况要注意
      // //建newIndex的字段
      let m2 = {
         properties: {
             name: {
                 type: "text",
             },
             age: {
                 type: "integer"
             },
             sex: {
                 type: "text",
             }
         }
     }
      client.createIndexWithMapping("newTest", m2)
      client.reindex(oldIndex, newIndex) // newIndex新增了sex字段,复制oldIndex的数据
      let docNew = {
         "name": "Tom",
         "age": 26,
         "sex": "男"
     }
      let docIDNew = client.indexDoc('newTest', docNew)   //新插入一条数据测试
      client.refresh(newIndex) // 需刷新,1s后才可被搜
      let res = client.query("", true, newIndex)
      console.log(res) // 期望保留name和age的数据,有一条数据有sex值,其它数据无sex值
    

    Parameters

    • oldIndex: string
    • newIndex: string

    Returns void

scrollIDSearch

  • scrollIDSearch(scrollID: string): Dict
  • search records using scrollID, use after scrollSearch

    游标查询三个接口:scrollSearch,scrollIDSearch,clearScroll 需一起用,详细用例见scrollSearch

    example
    
    for (let i = 1; i < 3; i++) {
      let result1 = client.scrollIDSearch(scrollID)  // 如果没有scrollID,会报错
      if (result1.length == 0) {
          client.clearScroll(scrollID)
          break
      }
     console.log(i, result1.length)
    }
    
    

    Parameters

    • scrollID: string

    Returns Dict

scrollSearch

  • first scrollSearch records

    游标查询三个接口:scrollSearch,scrollIDSearch,clearScroll 需一起用

    example
    
    let indexName1 = "indexName1" // 定义一个索引例子
    //let exist = client.checkIndexExist(indexName1) // 检查索引是否存在
    //if (exist) { client.dropIndex(indexName1) } // 存在就删除重复索引,这里测试用例写了删除索引方法,实际情况要注意
    //client.createIndex(indexName1) // 创建索引
    let docs = [] // 批量建数据
    for (let i = 0; i < 1001; i++) {
      docs.push({
          "foo": "banana",
          "bar": 50,
      })
    }
    client.bulkIndexDocs(indexName1, docs) // 批量插入上面创建的数据
    client.refresh(indexName1) // 增删改之后要刷新,1s后才可被搜
    
    let query = {
         range: {
             bar: {
                 gt:  10
             }
         }
    }
    
    let result = client.scrollSearch(indexName1, query) // 游标查询第一次,query是dsl语句
    let scrollID = result.ScrollID  // 结果会有scrollID,用做下一次游标查询
    console.log("countFirstSearch:", result.Res.length)
    console.log("matchedCount:", result.DocCount)
    console.log("scrollID", scrollID) // 符合的索引条数不小于osql配置的elasticsearch_scrollsearch_size默认数1000,才会有scrollID
    for (let i = 1; i < 3; i++) {
      let result1 = client.scrollIDSearch(scrollID)  // 根据第一次获取的scrollID进行下一次游标查询,如果没有scrollID会报错
      if (result1.length == 0) {
          break
      }
     console.log(i, result1.length)
    }
     client.clearScroll(result.ScrollID)  // 必须清除ScrollID,清缓存
    
    

    Parameters

    • index: string
    • dsl: Dict

    Returns ScrSearchResult

    matched list, scrollID, matched documents count

search

  • search(condition: Dict, ...indexName: string[]): Dict
  • get documents by provided JSON DSL condition

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let dsl = {
       query: {
           match: {
               foo: "bar"
            }
        },
        from: 5,
        size: 5
    }
    let searchRes = client.search(dsl, 'indexName')
    

    Parameters

    • condition: Dict

      the condition

    • Rest ...indexName: string[]

      the specify indexes, if no index is provided then all indexes will be searched

    Returns Dict

    retrieved documents and total hits. For example,

     "hits": {
     [
          {
              "_id": "Z77z1GoBFqkjW4TNVqT9",
              "_index": "0000000000kmfw1lhyz7:myindex1",
              "_score": 1,
              "_source": {
                  "age": 50,
                  "name": {
                      "first_name": "foo",
                      "last_name": "bar"
                  }
              },
              "_type": "t"
          }
    ],
      "max_score": 1,
      "total": 1
    }
    

searchIDList

  • searchIDList(indexName: string, condition: Dict): string[]
  • search by dsl condition and only return matched document id list

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    
    let condition = {
        "query": {
            "bool": {
                "must": [
                    { "match": { "title": "Demo" }},
                    { "match": { "author": "Test" }}
                ],
                "filter": [
                    { "term":  { "author": "Test" }},
                    { "range": { "saleCnt": { "gte": "4000" }}}
                ]
            }
        }
    }
    let indexName = ""my_index;
    let ids = client.searchIDList(indexName, condition); // id of docs math condition will be returned
    
    

    Parameters

    • indexName: string

      the specify index

    • condition: Dict

      the condition

    Returns string[]

    matched id list

searchPre

  • searchPre(condition: Dict, ...indexName: string[]): Dict
  • get documents by provided JSON DSL condition

    example
    
    // 与search的用法完全一样,区别是:内部默认了偏好参数preference若为"_local",防止通过修改from size查询多个批次结果时,多个批次之间存在重复或不一致数据的问题。
    // 参考文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.11/search-search.html#search-preference
    let searchRes = client.searchPre(dsl, 'indexName')
    
    

    Parameters

    • condition: Dict
    • Rest ...indexName: string[]

    Returns Dict

updateByQuery

  • updateByQuery(indexName: string, dsl: Dict, script: string): number
  • update documents by query

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let script = 'ctx._source.price = 200'
    let query = {
        range: {
            foo: {
                gt:  10
             }
        }
    }
    let updatedCount = client.updateByQuery('indexName', query, script)
    

    Parameters

    • indexName: string

      the specify index

    • dsl: Dict

      condition dsl. You can run the query type command to create a DSL.

    • script: string

      groovy script

    Returns number

    updated count

updateDocByDocID

  • updateDocByDocID(indexName: string, docID: string, updateData: Dict): void
  • partly update a document by given data

    example
    
    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let doc = {
        "name": "Jerry",
        "age": 12
    }
    let docId = '1001'; // suppose that you've got the docId in some way
    client.updateDocByDocID('indexName', docId, doc); // doc with id '1001' has updated
    

    Parameters

    • indexName: string

      the specify index

    • docID: string

      id of document to be updated

    • updateData: Dict

      the data use for update

    Returns void