Elasticsearch Client for Deno Runtime


Getting Started

Connect

import { Client as ElasticsearchClient } from 'https://deno.land/x/elasticsearch@v7.16.4/mod.ts'

const client = new ElasticsearchClient({
    node: 'http://localhost:9200',
    /*
    auth: {
        username: 'elastic',
        password: 'password'
    }
    */
})

Search APIs

import type { SearchResponse } from 'https://deno.land/x/elasticsearch@v7.16.4/mod.ts'

interface Source {
    title: string
}

const res: SearchResponse<Source> = await client.search<Source>({
    target: 'test-index',
    body: {
        query: {
            match_all: {}
        }
    }
})

const mres: MSearchResponse<Source> = await client.msearch<{ title: string }>({
    // target: 'test-index',
    body: [
        { index: 'test-index' },
        { query: { match: { title: 'Deno' } } },
        { index: 'test-index' },
        { query: { match: { title: 'Node' } } },
        { index: 'other-test-index' },
        { query: { match: { title: 'Rust' } } },
    ]
})

// msearch will not throw an error on not found like search does
// so you have to check if each response has object `hits` or `error`
mres.responses.forEach((m) => {
    if ('hits' in m) {
        console.log(m.status)
    } else if ('error' in m) {
        console.log(m.error.root_cause[0].reason)
    }
})

SQL Search API

import type { SqlSearchResponse } from 'https://deno.land/x/elasticsearch@v7.16.4/mod.ts'

const res: SqlSearchResponse = await client.sql.search({
    body: {
        query: "SELECT * FROM test_index"
    },
    queryParams: {}
})

res.rows.forEach((row) => {
    console.log(row)
})

Cluster APIs

const health = await client.cluster.health({
    target: '',
    queryParams: {}
})

Cat APIs

const indices = await client.cat.indices({
    target: '*',
    queryParams: {}
})

const aliases = await client.cat.aliases({
    alias: '*',
    queryParams: {}
})

const count = await client.cat.count({
    target: '*',
    queryParams: {}
})

const allocation = await client.cat.allocation({
    nodeId: '*',
    queryParams: {}
})

const fielddata = await client.cat.fielddata({
    field: '*',
    queryParams: {}
})

Index APIs

const indices = await client.indices.get({
    target: '*',
    queryParams: {}
})

const indice = await client.indices.get({
    target: 'test-index',
    queryParams: {}
})

await client.indices.create({
    index: 'test-index',
    body: {},
    queryParams: {}
})

await client.indices.delete({
    index: 'test-index',
    queryParams: {}
})

const exists = await client.indices.exists({
    target: 'test-index',
    queryParams: {}
})

await client.indices.close({
    index: 'test-index',
    queryParams: {}
})

await client.indices.open({
    target: 'test-index',
    queryParams: {}
})

const settings = await client.indices.settings({
    target: 'test-index',
    setting: '',
    queryParams: {}
})

Document APIs

const doc = await client.documents.get<Source>({
    index: 'test-index',
    _id: '1',
    queryParams: {}
})

const docs = await client.documents.mget<Source>({
    // index: 'test-index',
    body: {
        docs: [
            {
                _id: '1',
                _index: 'test-index'
            },
            {
                _id: '2',
                _index: 'test-index'
            }
        ]
    },
    queryParams: {}
})

await client.documents.index({
    target: 'test-index',
    _id: '1',
    body: { title: 'Node' },
    queryParams: {}
})

await client.documents.upsert({
    target: 'test-index',
    _id: '1',
    body: { title: 'Deno' },
    queryParams: {}
})

await client.documents.delete({
    index: 'test-index',
    _id: '1',
    queryParams: {}
})

await client.documents.bulk({
    // target: 'test-index',
    body: [
        { index: { _index: 'test-index', _id: '2', } },
        { title: 'Node is great' },
        { index: { _index: 'test-index', _id: '3', } },
        { title: 'Deno is also great' },
        { update: { _id: '3', _index: "test-index" } },
        { doc: { title: 'Deno is much better than node' } }
    ],
    queryParams: {}
})

const exists = await client.documents.exists({
    index: 'test-index',
    _id: '1',
    queryParams: {}
})

Todo

  • Write all tests for each REST endpoint.
  • Add async search for Document APIs.
  • Add async search for SQL APIs.
  • Add other cat and cluster endpoints.
  • Separate the search operations from client class.