Preparations

See first Dataset basics tutorial for getting setup and initializing a Dataset.

Querying

The basic thing we want to do with Datasets is query and filter them. This is very easy to do:

// must have a div with class="ex-1"
var $el = $('.ex-2');

// query with text 'UK' - this will attempt to match any field for UK
// Also limit the query to return a maximum of 2 results using the size attribute 

// query function has asynchronous behaviour and returns a promise object
// (even for the case of in memory data where querying in fact happens synchronously)
// On completion the display function will be called
dataset.query({q: 'UK', size: 2}).done(function() {
  $('.ex-2').append('Total found: ' + dataset.recordCount);
  $('.ex-2').append(' Total returned: ' + dataset.records.length);
  $('.ex-2').append(
    $('<pre />').html(
      JSON.stringify(dataset.records.toJSON(), null, 2)
    )
  );
});

This results in the following. Note how recordCount is now 3 (the total number of records matched by the query) but that records only contains 2 records as we restricted number of returned records using the size attribute.

 

Filtering

A simple unstructured query like the one provided above searches all fieldsfor the value provided.

Often you want to “filter” results more precisely, for example by specifying a specific value in a specific field. To do this we use “filters”.

var query = new recline.Model.Query();
query.addFilter({type: 'term', field: 2});
dataset.query(query);

QueryState

The last run query is stored as a Query instance in the queryState attribute of the Dataset object. Modifying queryState will also resulting in a query being run. This is useful when building views that want to display or manage the query state (see, for example, Query Editor or Filter Editor widgets).

Full Details of the Query Language

Full details of the query structure and its options can be found in the reference documentation.