Interface ESClient

interface ESClient {
    attachAlias(indexName, alias): void;
    attachAndDetachAlias(attachIndexName, detachIndexName, alias): void;
    bulkDeleteDocs(indexName, docIdList): void;
    bulkIndexChildDocs(indexName, routing, records): void;
    bulkIndexDocs(indexName, docs): void;
    cat(): CatInfo[];
    checkDocExist(indexName, docID): boolean;
    checkIndexExist(indexName): boolean;
    clearScroll(scrollID): void;
    countDocs(indexName): number;
    countDocsByCondition(indexName, dsl): number;
    countDocsByQuery(indexName, query): number;
    createIndex(indexName): void;
    createIndexWithMapping(indexName, mapping): void;
    deleteByQuery(indexName, dsl): number;
    deleteDocByDocID(indexName, docID): void;
    detachAlias(indexName, alias): void;
    dropIndex(indexName): void;
    getAlias(indexName): string[];
    getAllIndexes(): string[];
    getAttachedIndexes(alias): string[];
    getDocByDocID(indexName, docID): Dict;
    getMapping(indexName): Dict;
    indexChildDoc(indexName, routing, doc): string;
    indexDoc(indexName, doc): string;
    query(queryString, getAll, ...indexName): Dict[];
    refresh(indexName): void;
    reindex(oldIndex, newIndex): void;
    scrollIDSearch(scrollID): Dict;
    scrollSearch(index, dsl): ScrSearchResult;
    search(condition, ...indexName): Dict;
    searchIDList(indexName, condition): string[];
    searchPre(condition, ...indexName): Dict;
    updateByQuery(indexName, dsl, script): number;
    updateDocByDocID(indexName, docID, updateData): void;
}

Methods

  • attach concrete index with alias

    Parameters

    • indexName: string

      concrete index

    • alias: string

      the name of alias

    Returns void

    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
  • attachAndDetachAlias attach and detach alias in one request, used to smoothly convert indexes within one alias

    Parameters

    • attachIndexName: string

      index to attach

    • detachIndexName: string

      index to detach

    • alias: string

      the name of alias

    Returns void

    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
  • delete multiple documents in the specified index

    Parameters

    • indexName: string

      the specify index name

    • docIdList: string[]

      array of doc ids to be deleted

    Returns void

    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
  • 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

  • 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.

    Parameters

    • indexName: string
    • docs: Dict[]

    Returns void

    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)
  • cat show indexes basic information

    Returns CatInfo[]

    basic info for each index, for example:

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

    Example


    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    let cats = client.cat();
  • check if document is existing

    Parameters

    • indexName: string
    • docID: string

    Returns boolean

    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);
  • check whether the specified index is existing

    Parameters

    • indexName: string

    Returns boolean

    boolean true for exist, otherwise false

    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
  • clear scrollID

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

    Parameters

    • scrollID: string

    Returns void

    Example


    client.clearScroll(result.ScrollID) //scrollID from scrollSearch
  • count documents in an index

    Parameters

    • indexName: string

    Returns number

    documents count

    Example


    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    let num = client.countDocs("my_index");
  • count documents by dsl

    Parameters

    • indexName: string
    • dsl: Dict

      condition in ES dsl format

    Returns number

    documents count

    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)
  • count documents by light-weight query string

    Parameters

    • indexName: string
    • query: string

      light-weight query string

    Returns number

    documents count

    Example


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

    Parameters

    • indexName: string

    Returns void

    Example

    import * as es from 'es';
    let client = es.newClient('es_connector_instance');
    client.createIndex("my_index");
  • create a new index with mapping

    Parameters

    • indexName: string
    • mapping: Dict

    Returns void

    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)
  • delete documents by query

    Parameters

    • indexName: string
    • dsl: Dict

    Returns number

    deleted count

    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)
  • 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

    Parameters

    • indexName: string

      the specify index

    • docID: string

      the specify document id

    Returns void

    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
  • detachAlias detach concrete index with alias

    Parameters

    • indexName: string

      concrete index

    • alias: string

      the name of alias

    Returns void

    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
  • delete an index. Notice: cannot delete alias directly

    Parameters

    • indexName: string

      the index to be dropped

    Returns void

    Example


    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    client.dropIndex("my_index");
    // index name 'my_index' was dropped
  • input index name return associated aliases

    Parameters

    • indexName: string

    Returns string[]

    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
  • get all indexes for the current tenant

    Returns string[]

    index name list

    Example


    import * as es from 'es'
    let client = es.newClient('es_connector_instance')
    let indexes = client.getAllIndexes(); // an array of indexes was returned
  • get attached concrete indexes by alias

    Parameters

    • alias: string

      the index name which created in connector

    Returns string[]

    attached indexes

    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
  • get one document by document id

    Parameters

    • indexName: string

      the specify index

    • docID: string

      the specify document id

    Returns Dict

    document the doc

    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);
  • getMapping get mapping for an index or alias

    Parameters

    • indexName: string

    Returns Dict

    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"
    }
    }
    }
    }
  • 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

  • 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.

    Parameters

    • indexName: string

      the specify index

    • doc: Dict

      the document to be indexed.

    Returns string

    inserted document 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
  • provides light-weight querying. You can use * to match zero or more characters

    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"
    }
    ]

    Example


    import * as es from 'es';
    let client = es.newClient('es_connector_instance')
    let docs = client.query('value1 OR field2:value2', true, 'indexName')
  • 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接口

    Parameters

    • indexName: string

    Returns void

    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
  • reindex copy old index to new index

    Parameters

    • oldIndex: string
    • newIndex: string

    Returns void

    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值
  • search records using scrollID, use after scrollSearch

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

    Parameters

    • scrollID: string

    Returns Dict

    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)
    }
  • first scrollSearch records

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

    Parameters

    • index: string
    • dsl: Dict

    Returns ScrSearchResult

    matched list, scrollID, matched documents count

    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,清缓存
  • get documents by provided JSON DSL condition

    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
    }

    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')
  • search by dsl condition and only return matched document id list

    Parameters

    • indexName: string

      the specify index

    • condition: Dict

      the condition

    Returns string[]

    matched 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
  • get documents by provided JSON DSL condition

    Parameters

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

    Returns Dict

    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')
  • update documents by query

    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

    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)
  • partly update a document by given data

    Parameters

    • indexName: string

      the specify index

    • docID: string

      id of document to be updated

    • updateData: Dict

      the data use for update

    Returns void

    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