npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gojob/ts-elasticsearch

v1.3.3

Published

Elasticsearch decorated by TypeScript

Downloads

45

Readme

@gojob/ts-elasticsearch

travis build Coverage Status

NPM publish

To publish a new version of this package, you have to build the project before run npm publish.

Description

The purpose of this library is to provide a decorated class approch to use the elasticsearch module.

Installation

yarn add @gojob/ts-elasticsearch

Usage example

import { Field, Elasticsearch, Primary } from '@gojob/ts-elasticsearch';

@Index()
class User {
  @Primary()
  @Field({ enabled: false})
  id: string;

  @Field('text') name: string;
  @Field('integer') age: number;
}

// ...

const elasticsearch = new Elasticsearch({ host: 'http://192.168.99.100:9200' });

await elasticsearch.indices.create(User);
await elasticsearch.indices.putMapping(User);

const user = new User();
user.id = 'agent-1122';
user.name = 'Smith';
user.age = 33;

await elasticsearch.index(user);

await elasticsearch.indices.refresh(User);

const { documents } = elasticsearch.search(User, { body: { query: { match_all: {} } } });

Documentation

Decorators

decorator @Index

Keep in mind that index types are scheduled to be removed in Elasticsearch 8.

This class decorator factory declares a new Elasticsearch index.

By default, it uses the class name as index (and type), but it can be overwritten.

Index settings may be added in the settings optional parameter.

@Index({index: 'twt', settings: { number_of_shards: 3 } })
class Tweeter {}

decorator @Primary

This property decorator factory declares the class primary key which will be used as _id in Elasticsearch index. When not provided, Elasticsearch generates ids by it own.

@Index()
class User {
  @Primary()
  @Field({ enabled: false})
  id: string;

  @Field('text') name: string;
  @Field('integer') age: number;
}

decorator @Field

This property decorator factory declares a new property in the current class.

It takes either a simple type as string or an elasticsearch mapping setting.

@Index()
class User {
  @Field('keyword') id: string;
  @Field({ type: 'text', boost: 2 })name: string;
  @Field('integer') age: number;
}
Special case of object or nested (array of object)

When dealing with object or nested, you have to declare a class with some fields declared. Notice in this case, the class in not decorated with @Index.

Object example:

class Country {
  @Field('text') name: string;
}

@Index()
class City {
  @Field('text') name: string;
  @Field({ object: Country })
  country: Country;
}

Nested example:

class Follower {
  @Field({ enabled: false}) name: string;
  @Field({ enabled: false}) popularity: number;
}

@Index()
class User {
  @Primary()
  @Field({ enabled: false})
  id: string;

  @Field('text') name: string;

  @Field({ nested: Follower })
  followers: Follower[];
}

Elasticsearch class

This class provides main elasticsearch features directly usable with indexed classes.

The purpose of this library is not to override all official plugin features, but only those that are relevant for managing documents using classes.

When dealing with documents, you can either uses indexed class instances or literal object associated to their indexed class.

The examples presents in this chapter are based on this setup.

import { Field, Elasticsearch, Primary } from '@gojob/ts-elasticsearch';

const elasticsearch = new Elasticsearch({ host: 'http://192.168.99.100:9200' });

@Index()
class User {
  @Primary()
  @Field({ enabled: false})
  id: string;

  @Field('text') name: string;
  @Field('integer') age: number;
}

Elasticsearch core

Instanciate this class with an IConfigOptions or an official elasticsearch.Client instance.

IConfigOptions

IConfigOptions extends the official client configuration option.

Extended options provided by IConfigOptions:

| Name | Type | Optional | Description | |:-----------:|:---------------------------------------------------------------------------------------------------------:|:--------:|---------------------------------| | client | elasticsearch.Client | X | Official client instance to use | | indexPrefix | string | X | Prefix to set to all indices |

Core functions

The main class provides the main part of the document API functions.

  • bulkIndex: Index an arary or a stream of documents (bulk api
  • count: Count documents in the index (count api)
  • create: Add a new document to the index create api)
  • delete: Delete a document from the index delete api)
  • get: Retrieve a document by its id get api)
  • getIndices: Return all Indexed classes
  • index: Add or update a document in the index index api)
  • search: Search documents in the index search api)
  • update: Update a document update api)

Elasticsearch.indices

The indices provides the main part of the document API functions.