This simple Moose class only changes "use Moose;" to "use Elastic::Doc;". At the moment we're not configuring anything else. Thanks to Moose's introspection, we have enough information to setup everything we need.

The UID (unique ID) of the object tracks (amongst other things) the current version number. Elasticsearch uses this version number to avoid overwriting changes that have been made by another process (see Optimistic Currency Control).

say $user->uid->version;
# 1

The version number is incremented each time a changed object is saved.

$user->save;
say $user->uid->version;
# 2
say $user->has_changed;
# 0

QUERYING Elasticsearch

By default, everything in Elasticsearch is indexed and searchable. You can search across one index or many indices, one type or many types.

In order to query the objects stored in Elasticsearch, you need a view. Views are reusable, so you might create views like $recent_users, $approved_comments etc.

Creating a view

You can create a view from your $domain object, in which case the view will be limited to just that domain:

$view = $domain->view; # limited to index 'myapp';

To create a view which queries multiple domains, you could do:

$view = $model->view->domain(['index_1', 'alias_2']);

Or to query all domains known to your model:

$view = $model->view;

Configuring a view

When setting an attribute on a view, a cloned instance of the old view is returned, meaning that you can use one view to derive another:

Views can also be used to return highlighted results, and facets, which provide aggregated results, much like GROUP-BY functions in SQL, for instance, the most popular terms, or the number of posting per day.