0.3.1 • Published 16 days ago

@sebspark/opensearch v0.3.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
16 days ago

@sebspark/opensearch

A wrapper for OpenSearch Client to assist with typed queries, indices etc

Usage

yarn add @sebspark/opensearch @opensearch-project/opensearch

Note: Data types require a property called id of type string

import { Client } from '@opensearch-project/opensearch'
import { helper } from '@sebspark/opensearch'

const client = new Client({})
const typedClient = helper(client)

type Data = {
  id: string
  user: {
    name: string
    age: number
  }
  blog: {
    posts: Array<{
      title: string
      text: string
    }>
  }
}

type UserOnly = Pick<Data, 'id' | 'user'>

// Typed index creation
async function createIndex() {
  await helper(client as Client).typedIndexCreate<Data>('data', {
    mappings: {
      properties: {
        user: {
          age: {
            type: 'integer'
          },
        },
        isTrue: {
          type: 'boolean'
        }
      }
    }
  })
}

// Typed insert
async function indexDocument() {
  await helper(client as Client).typedIndex<Data>('data', {
    id: 'foo',
    isTrue: true,
    user: {
      age: 42,
      name: 'Arthur Dent',
    }
  })
}

// Typed search
async function loadData() {
  // result: Data[]
  const { result } = await typedClient.typedSearch<Data>({
    index: 'data',
    body: {
      query: {
        term: {
          'user.name': {
            value: 'Arthur Dent',
          },
        },
      },
    },
  })
  return result
}
async function loadPartialData() {
  // result: UserOnly[]
  const { result } = await typedClient.typedSearch<Data, UserOnly>({
    index: 'data',
    body: {
      query: {
        fields: ['user.age', 'user.name'],
      },
    },
  })
  return result
}
0.3.1

16 days ago

0.3.0

2 months ago

0.2.3

3 months ago

0.2.1

4 months ago

0.2.2

4 months ago

0.2.0

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago