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

@mirromioorxxx/opensearch

v1.0.2

Published

The official OpenSearch client for Node.js

Downloads

9

Readme

Nodejs Integration Bundler codecov Chat PRs welcome!

OpenSearch logo

OpenSearch Node.js client

Welcome!

opensearch-js is a community-driven, open source fork of elasticsearch-js licensed under the Apache v2.0 License. For more information, see opensearch.org.

Example use

The OpenSearch JavaScript client provides a safer and easier way to interact with your OpenSearch cluster. Rather than using OpenSearch from the browser and potentially exposing your data to the public, you can build an OpenSearch client that takes care of sending requests to your cluster.

The client contains a library of APIs that let you perform different operations on your cluster and return a standard response body. The example here demonstrates some basic operations like creating an index, adding documents, and searching your data.

Setup

To add the client to your project, install it with npm:

npm install https://github.com/opensearch-project/opensearch-js

Note: this module will be added to the NPM registry in the coming weeks.

If you prefer to add the client manually or just want to examine the source code, see opensearch-js on GitHub.

Then require the client:

const { Client } = require('@opensearch-project/opensearch')

Sample code

'use strict';

var host = 'localhost';
var protocol = 'https';
var port = 9200;
var auth = 'admin:admin'; // For testing only. Don't store credentials in code.
var ca_certs_path = '/full/path/to/root-ca.pem';

// Optional client certificates if you don't want to use HTTP basic authentication.
// var client_cert_path = '/full/path/to/client.pem'
// var client_key_path = '/full/path/to/client-key.pem'

// Create a client with SSL/TLS enabled.
var { Client } = require('@opensearch-project/opensearch');
var fs = require('fs');
var client = new Client({
    node: protocol + '://' + auth + '@' + host + ':' + port,
    ssl: {
        ca: fs.readFileSync(ca_certs_path),
        // You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
        // cert: fs.readFileSync(client_cert_path),
        // key: fs.readFileSync(client_key_path)
    }
})

async function search() {

    // Create an index with non-default settings.
    var index_name = 'books'
    var settings = {
        'settings': {
            'index': {
                'number_of_shards': 4,
                'number_of_replicas': 3
            }
        }
    }

    var response = await client.indices.create({
        index: index_name,
        body: settings
    })

    console.log('Creating index:')
    console.log(response.body)

    // Add a document to the index.
    var document = {
        'title': 'The Outsider',
        'author': 'Stephen King',
        'year': '2018',
        'genre': 'Crime fiction'
    }

    var id = '1'

    var response = await client.index({
        id: id,
        index: index_name,
        body: document,
        refresh: true
    })

    console.log('Adding document:')
    console.log(response.body)

    // Search for the document.
    var query = {
        'query': {
            'match': {
                'title': {
                    'query': 'The Outsider'
                }
            }
        }
    }

    var response = await client.search({
        index: index_name,
        body: query
    })

    console.log('Search results:')
    console.log(response.body.hits)

    // Delete the document.
    var response = await client.delete({
        index: index_name,
        id: id
    })

    console.log('Deleting document:')
    console.log(response.body)

    // Delete the index.
    var response = await client.indices.delete({
        index: index_name
    })

    console.log('Deleting index:')
    console.log(response.body)
}

search().catch(console.log)

Project Resources

Code of Conduct

This project has adopted the Amazon Open Source Code of Conduct. For more information see the Code of Conduct FAQ, or contact [email protected] with any additional questions or comments.

License

This project is licensed under the Apache v2.0 License.

Copyright

Copyright OpenSearch Contributors. See NOTICE for details.