appbase-js is a universal JavaScript client library for working with the ReactiveSearch API.

INSTANTIATION

Appbase()

Returns an Appbase object (refered to as appbaseRef in all the following examples) using the url, app and username:password credentials.

Copy
var appbaseRef = Appbase({
	url: 'https://appbase-demo-ansible-abxiydt-arc.searchbase.io',
	app: 'good-books-demo',
	credentials: 'c84fb24cbe08:db2a25b5-1267-404f-b8e6-cf0754953c68',
});

Usage

Appbase(appData)

Note: Either you can use a combination of username & password or use credentials.

Returns

Object appbaseRef Appbase reference object - has index(), update(), delete(), bulk(), search() and get() methods.

WRITING DATA

index()

Writes a JSON data object at a given type and id location, or replaces if an object already exists.

Copy
appbaseRef
	.index({
		id: 'aX12c5',
		body: {
			msg: 'writing my first tweet!',
			by: 'jack',
			using: ['appbase.io', 'javascript'],
			test: true,
		},
	})
	.then(function(res) {
		console.log('successfully indexed: ', res);
	})
	.catch(function(err) {
		console.log('indexing error: ', err);
	});

Usage

appbaseRef.index(params)

  • params Object
    A JavaScript object containing the type, id and the JSON data to be indexed

    • body Object
      Data to be indexed, a valid JSON object
    • id String
      Unique ID for the JSON data. id is auto generated if not specified

update()

Partially updates an existing document at a given type and id location. The important difference with the index() method is that the latter replaces the existing data values wholesale, while update() only replaces the values that are specified in the body.doc field.

Copy
appbaseRef
	.update({
		id: 'aX12c5',
		body: {
			doc: {
				msg: 'editing my first tweet!',
				by: 'ev',
			},
		},
	})
	.then(function(res) {
		console.log('successfully updated: ', res);
	})
	.catch(function(err) {
		console.log('update document error: ', err);
	});

Usage

appbaseRef.update(params)

  • params Object
    A JavaScript object containing the type, id, and the partial JSON data to be updated

    • body.doc Object
      Partial doc JSON to be updated (all the JSON data can only reside under the body.doc field)
    • id String
      Unique ID of the JSON document to be updated. id here is mandatory and should match an existing object.

delete()

Delete a JSON data object by id.

Copy
appbaseRef
	.delete({
		id: 'aX12c5',
	})
	.then(function(res) {
		console.log('successfully deleted: ', res);
	})
	.catch(function(err) {
		console.log('deletion error: ', err);
	});

Usage

appbaseRef.delete(params)

  • params Object
    A JavaScript object containing the type and id of the JSON object to be deleted

    • id String
      Unique ID for the JSON data

bulk()

Apply many index / update / delete operations in bulk.

Copy
appbaseRef
	.bulk({
		body: [
			// action#1 description
			{ index: { _id: 2 } },
			// the JSON data to index
			{
				msg: 'writing my second tweet!',
				by: 'Ev',
				using: ['appbase.io', 'javascript'],
				test: true,
			},
			// action#2 description
			{ update: { _id: 2 } },
			// JSON data to update under the `doc` key, this only modifies the mentioned fields
			{
				doc: {
					msg: 'editing my second tweet!',
				},
			},
			// action#2 description
			{ delete: { _id: 2 } },
			// deletion doesn't any further input
		],
	})
	.then(function(res) {
		console.log('successful bulk: ', res);
	})
	.catch(function(err) {
		console.log('bulk failed: ', err);
	});

Usage

appbaseRef.bulk(params)

  • params Object
    A JavaScript object containing the body and optionally a default type to be used for actions

    • body Array
      A JavaScript array of actions to be performed written as a sequence of action#1, data#1, action#2, data#2, ... action#n, data#n

GETTING DATA

get()

Get the JSON document from a particular type and id.

Copy
appbaseRef
	.get({
		id: 'aX12c5',
	})
	.then(function(res) {
		console.log('The document data: ', res);
	})
	.catch(function(err) {
		console.log('get() method failed with: ', err);
	});

Usage

appbaseRef.get(params)

  • params Object
    A JavaScript object containing the type and id of the document to retrieve.

Returns the document at the given type and id.

getMappings()

Get the mapping scheme of an app. You can read more about mappings over here.

Copy
appbaseRef
	.getMappings()
	.then(function(res) {
		console.log('Mapping scheme is: ', res);
	})
	.catch(function(err) {
		console.log('getMappings() failed: ', err);
	});

Usage

appbaseRef.getMappings()

Returns the current app's mapping scheme as an object.

Search for matching documents in a type. It's a convenience method for Elasticsearch's /_search endpoint.

Copy
appbaseRef
	.search({
		body: {
			query: {
				match_all: {},
			},
		},
	})
	.then(function(res) {
		console.log('query result: ', res);
	})
	.catch(function(err) {
		console.log('search error: ', err);
	});

Usage

appbaseRef.search(params)

  • params Object
    A JavaScript object containing the query type and body.

Returns Promise.

msearch()

Multi Search to allow execution of several search requests with same API. It's a convenience method for Elasticsearch's /_msearch endpoint.

Copy
appbaseRef
	.msearch({
		body: [{}, { query: { match_all: {} } }, {}, { query: { match: { _id: 1 } } }],
	})
	.then(function(res) {
		console.log('query result: ', res);
	})
	.catch(function(err) {
		console.log('search error: ', err);
	});

Usage

appbaseRef.msearch(params)

  • params Object
    A JavaScript object containing the query type and body.

    • body Array
      An array specifying search requests in header followed by body order for each request.

Returns Promise.

reactiveSearch()

ReactiveSearch method allows you to execute the search requests securely with the help of newly launched ReactiveSearch API. You can read about ReactiveSearch API here.

Copy
appbaseRef
	.reactiveSearch(
		[
			{
				id: 'book_search',
				dataField: ['original_title'],
				size: 10,
				value: 'harry',
			},
		],
		{
			userId: '[email protected]',
		},
	)
	.then(function(res) {
		console.log('query result: ', res);
	})
	.catch(function(err) {
		console.log('search error: ', err);
	});

Usage

appbaseRef.reactiveSearch(params)

  • params It accepts two params:
    • query, an array of objects where each object represents a ReactiveSearch query. Read more at here
    • settings, an object consisting of the properties to control your search experience. Read more at here

Returns Promise.

Usage to fetch suggestions

Copy
appbaseRef
  .reactiveSearch(
    [
      {
        id: "suggestions-demo",
        type: "suggestion",
        enableRecentSuggestions: true,
        enablePopularSuggestions: true,
        recentSuggestionsConfig: {
          size: 5,
          min_hits: 2,
          min_char: 4,
        },
        popularSuggestionsConfig: {
          size: 5,
          showGlobal: true,
        },
        size: 5,
        value: "cable",
      },
    ],
    {
      userId: "[email protected]",
      recordAnalytics: true,
    }
  )
  .then(function (res) {
    console.log("res", res);
  })
  .catch(function (err) {
    console.log("suggestions error: ", err);
  });

Note Recent suggestions work when value is empty.