JavaScript / Node.js

Interactive examples with JavaScript

Setting things up

Before we start using the appbase-js lib, we’ll need to set up an app.

For this tutorial, we have used a sample app containing some dummy housing records. All the examples use this app. You can also clone this app (to create your own private app) directly by clicking “Clone this app” in the data browser view of the app here.


All the records are structured in the following format:

    "name": "The White House",
    "room_type": "Private room",
    "property_type": "Townhouse",
    "price": 124,
    "has_availability": true,
    "accommodates": 2,
    "bedrooms": 1,
    "bathrooms": 1.5,
    "beds": 1,
    "bed_type": "Real Bed",
    "host_image": "",
    "host_name": "Alyson",
    "image": "",
    "listing_url": "",
    "location": {
        "lat": 47.53540733743967,
        "lon": -122.27983057017123
    "date_from": 20170426,
    "date_to": 20170421,

Before we can interact with this app via the appbase-js lib, we will import the lib and create a reference object for the app by specifying the app name and credentials in the Appbase() constructor.

const Appbase = require("appbase-js");

// Create an app's instance
const appbaseRef = new Appbase({
    app: "housing-demo",
    credentials: "YnCgVwozV:e9ec5cb0-58d8-4bb0-badc-acaf726661f1"


Make sure to update app and credentials fields if you are using your own app instance.

Fetch All

Before with start with all the fancy queries, let’s write a query to fetch all the records in the app.

We’ll use the search() method to fire a match_all query for doing this.


Feel free to play around with the query by making changes in the interactive demo. Make sure to hit the run button after every change.

You can also see this query in Mirage - a GUI for Elasticsearch queries which provides a query explorer view to interact with an app’s data. Here’s the same query executed on Mirage.


You can use any query parameters such as from and size that are supported by Query DSL.

Index Data

Next, let’s add some data to our app. We’ll make a simple JSON object with some housing data and pass it with the index() method which writes a data object at a given type and id location. In the event that data already exists at this id locaiton, it will overwrite the data with the body provided in the index() method.

Fetch Data

Let’s learn how to fetch or read the data present at specific id location. We’ll use the appbase-js get() method to get back the data (we just added above) by passing the same id.

Stream data

Let’s now subscribe to some streaming data updates based on an id location. For this, we’ll use the appbase-js getStream() method which continuously streams new updates to a specific JSON document based on the id.

We’ll also make a change on the same doc we’ve subscribed to to show how streaming updates work. To make a change in any existing data, we can use the update() method.


If you intended to replace the complete data object use index() method instead of update() which partially updates an existing doc.

Bulk Index Data

Let’s now learn to index multiple documents in one request. For this, we’ll use the appbase-js bulk() method which is based on Bulk API and applies many operations together.


It is recommended to index up to 1 MB of data (~500 documents) at a time (so if you have 50,000 documents, you can split them into chunks of 500 documents and index).

Range Query

Let’s now query the dataset to get all rooms between prices 50 to 100. We’ll use the appbase-js search() method to fire a range query by passing the field price and specifying the range filter values.

Here’s the same query executed on Mirage.

Search Stream

Let’s now subscribe to streaming updates for a query. For this, we’ll use the searchStream() method which continuously streams new results of the subscribed query on a given type. Here, we’re querying with match_all to get streaming updates about any data change.

To see this in action, we’ll also use the index() method to add a new data object, which searchStream() should stream back as an update.

GeoDistance Query

Let’s now get a list of all the rooms available within a certain distance from a specific geopoint (lat,lon) location.

Here, we’ll use the appbase-js search() method to fire a geodistance query specifying distance and location co-ordinates.

Here, we will apply a full-text search query on a field with a n-gram analyzer applied to it. Instead of indexing the exact field value, an n-gram indexes multiple grams that constitute the field value when indexing. This allows for a granular querying experience, where even partial field values can be matched back, really useful when building auto-suggestion based search. You can read more about n-grams here.

We’ll do a full-text search query on the name field by using appbase-js search() method to fire a match query with our n-gram analyzed field name and query value.

Here’s the same query executed on Mirage.

Date Query

Let’s now do a date query to get houses that are available between certain dates. As per our mapping (aka data schema), we’ll need to use date_from and date_to fields for querying by the date range.

Here, we’ll use the appbase-js search() method to fire a range query with our field name and query values.

Here’s the same query executed on Mirage.

Compound Query

Finally, let’s do a compound query that combines multiple queries together. We’ll take an example of fetching a specific room_type of rooms that are within the specified price range.

Here, we’ll use the appbase-js search() method to pass a bool query that combines a match query for room_type and a range query for price.

Here’s the same query executed on Mirage.

Further Reading

appbase-js API methods provide full Query DSL support and there are lots of use-cases that are unlocked by constructing various types of queries. Feel free to use our GUI query explorer to construct complex queries easily.

There are many more methods provided by the appbase-js API other than the ones we used. You can check out the full JavaScript API Reference here.