Interactive examples with Python

Setting things up

Before we start using the appbase.io REST API, 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:

Copy

{
    "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,
}

To interact with our app instance, we'll make HTTP REST API requests. The semantics should remain the same for any 3rd party HTTP library as well.

This is the common code which exists in all examples. Here, we simply create url based on app, a payload containing request body and set headers with credentials.

Copy

# Request URL containing app, type and id or method
url = "https://appbase-demo-ansible-abxiydt-arc.searchbase.io/{{app}}/{{type}}/{{id/method}}"

# Request body/payload
payload = "{{query_request}}"

# App credentials base64 encoded
headers = {
    'Authorization': "Basic {{credentials_base64}}",
    'Content-Type': "application/json"
    }

# Request HTTP method ( GET | POST | PUT | DELETE )
response = requests.request("POST", url, data=payload, headers=headers)

Note

If you are using your own app instance, make sure to update url with your app, type and headers with your credentials.

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 endpoint from appbase.io REST API to fire a POST request with the body containing a match_all query.

Note

Feel free to play around 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.

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 use the /id endpoint from appbase.io REST API specifying the id H1 and fire a PUT request with the body containing a simple JSON object with some housing data.


Note

If you intended to autogenerate id, do a POST request instead and don't pass any id with the URL. We'll autogenerate it for you.

Fetch Data

Let's learn how to fetch or read the data present at specific id location.

We'll use the /id endpoint from appbase.io REST API specifying the id H1 and fire a GET request.


Bulk Index Data

Let's now learn to index multiple documents in one request. For this, we'll use Bulk API to perform many index/delete operations in a single API call.


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 100 to 150.

We'll use the /_search endpoint from appbase.io REST API to fire a POST request with the body containing a range query by passing the field price and specifying range filter values.


Here's the same query executed on Mirage.

GeoDistance Query

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

We'll use the /_search endpoint from appbase.io REST API to fire a POST request with the body containing 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 the /_search endpoint from appbase.io REST API to fire a POST request with the body containing 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.

We'll use the /_search endpoint from appbase.io REST API to fire a POST request with the body containing 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.

We'll use the /_search endpoint from appbase.io REST API to fire a POST request with the body containing a bool query combining a match query for room_type and a range query for price.


Here's the same query executed on Mirage.

Further Reading

appbase.io REST 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.io REST API other than the ones we used. The full API reference with example snippets in cURL, Ruby, Python, Node, PHP, Go, jQuery can be browsed at rest.appbase.io.