Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
211 lines (129 sloc) 18.4 KB
id title layout sectionid permalink next nextTitle
interactive-examples
JavaScript / Node.js
docs
interactive-examples
interactive/javascript.html
go.html
Examples with Go

Interactive examples with JavaScript

Setting things up

Before we start using the appbase-js lib, we’ll need to set up an appbase.io 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.


Dataset

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": "https://host_image.link",
    "host_name": "Alyson",
    "image": "https://image.link",
    "listing_url": "https://www.airbnb.com/rooms/6644628",
    "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 appbase.io app's instance
const appbaseRef = Appbase({
    app: "housing-demo",
    credentials: "YnCgVwozV:e9ec5cb0-58d8-4bb0-badc-acaf726661f1",
    url: "https://scalr.api.appbase.io"
});

Note

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.

Note

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.

<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-Search?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.

Note

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-Index?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-get?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-getStream?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

Note

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-BulkIndex?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

Note

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-RangeQuery?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-searchStream?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-geoDistanceQuery?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

Full-text Search

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-fullText?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-dateQuery?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.


<iframe height="600px" width="100%" src="https://repl.it/@YashJoshi/Appbase-js-compoundQuery?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

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.