Database Search with Javascript

For this example we will use Node.js. You will need to install Node.js and NPM.

Let's create a new project folder called database-search:

Copied to clipboard!
    mkdir database-search

Now install Kuzzle SDK JS 6:

Copied to clipboard!
    npm init
    npm install kuzzle-sdk

Now the project configuration is complete, we can create an index.js file in the database-search folder to program our test.

Copied to clipboard!
    touch index.js

Instantiate Kuzzle

First, we need to instantiate a new Kuzzle object. To do this implement the following code:

Copied to clipboard!
const {
  Kuzzle,
  WebSocket
} = require('kuzzle-sdk');

// Replace 'kuzzle' with your Kuzzle server hostname. (example: 'localhost')
const kuzzle = new Kuzzle(
  new WebSocket('kuzzle')
);

Connect to Kuzzle

We now need to connect to Kuzzle:

Copied to clipboard!
const run = async () => {
  try {
    // Wait for the  connection to Kuzzle to be established
    await kuzzle.connect();
  } catch (error) {
    console.error(error.message);
  } finally {
    // Disconnecting from Kuzzle
    kuzzle.disconnect();
  }
};

run();

Create an index, a collection and documents

Now that we have established a connection to Kuzzle, we will create a new index, a new collection and two documents.

Copied to clipboard!
try {
  // Create a "nyc-open-data" index, a "yellow-taxi" collection
  // and 2 documents with different "licence" property values
  await kuzzle.index.create('nyc-open-data');
  await kuzzle.collection.create('nyc-open-data', 'yellow-taxi');
  await kuzzle.document.create(
    'nyc-open-data',
    'yellow-taxi',
    { licence: 'B' },
    null,
    { refresh: 'wait_for' } // Wait for the document to be indexed by Elasticsearch
  );
  await kuzzle.document.create(
    'nyc-open-data',
    'yellow-taxi',
    { licence: 'C' },
    null,
    { refresh: 'wait_for' } // Wait for the document to be indexed by Elasticsearch
  );

  console.log('New documents successfully created!');
} catch (error) {
  console.error(error);
}

Search for documents

Now that the documents are created and stored in Kuzzle, let's perform a search returning the documents that match our query filters.

Copied to clipboard!
try {
  // Search for documents with "licence" property that include the letter 'B'
  const results = await kuzzle.document.search(
    'nyc-open-data',
    'yellow-taxi',
    {
      query: {
        match: {
          licence: 'B'
        }
      }
    }
  );

  console.log(`There are ${results.hits.length} matching documents.`);
} catch (error) {
  console.error(error);
}

Your index.js file should now look like this:

Copied to clipboard!
// Require and instantiate kuzzle
const {
  Kuzzle,
  WebSocket
} = require('kuzzle-sdk');

const kuzzle = new Kuzzle(
  // Replace 'kuzzle' with your Kuzzle server hostname. (example: 'localhost')
  new WebSocket('kuzzle')
);

const run = async () => {
  try {
    // Wait a connection to Kuzzle to be established
    await kuzzle.connect();

  // Create a "nyc-open-data" index, a "yellow-taxi" collection
  // and 2 documents with different "licence" property values
  await kuzzle.index.create('nyc-open-data');
  await kuzzle.collection.create('nyc-open-data', 'yellow-taxi');
  await kuzzle.document.create(
    'nyc-open-data',
    'yellow-taxi',
    { licence: 'B' },
    null,
    { refresh: 'wait_for' } // Wait for the document to be indexed by Elasticsearch
  );
  await kuzzle.document.create(
    'nyc-open-data',
    'yellow-taxi',
    { licence: 'C' },
    null,
    { refresh: 'wait_for' } // Wait for the document to be indexed by Elasticsearch
  );

  // Search for documents with "licence" property that include the letter 'B'
  const results = await kuzzle.document.search(
    'nyc-open-data',
    'yellow-taxi',
    {
      query: {
        match: {
          licence: 'B'
        }
      }
    }
  );

    console.log(`There are ${results.hits.length} matching documents.`);
  } catch (error) {
    console.error(error.message);
  } finally {
    // Disconnect from Kuzzle
    kuzzle.disconnect();
  }
};

run();

Here we are, we have a simple bit of code that connects to Kuzzle, creates some documents and then prints the number of documents matching a simple search request on the terrain property.

To run it, just use node :

Copied to clipboard!
    node index.js

By running this code, the console should output the following message:

Copied to clipboard!
    There are 1 matching documents.