NAME

Elastic::Model::SearchBuilder - An Elastic::Model specific subclass of ElasticSearch::SearchBuilder

VERSION

version 0.52

DESCRIPTION

Elastic::Model::SearchBuilder is a sub-class of ElasticSearch::SearchBuilder to add automatic handling of Elastic::Doc and Elastic::Model::UID values.

This document just explains the functionality that Elastic::Model::SearchBuilder adds.

For the full SearchBuilder docs, see ElasticSearch::SearchBuilder.

THE PROBLEM

Consider this class (where MyApp::User is also an Elastic::Doc class):

package MyApp::Comment;

use Elastic::Doc;

has 'user' => (
    is     => 'ro',
    isa    => 'MyApp::User,
);

has 'text' => (
    is     => 'ro',
    isa    => 'Str',
);

We can create a comment as follows:

$domain->create(
    comment => {
        text => 'I like Elastic::Model',
        user => $user,
    }
);

The comment object would be stored in Elasticsearch as something like this:

{
    text    => "I like Elastic::Model",
    user    => {
        uid => {
            index   => 'myapp',
            type    => 'user',
            id      => 'abcdefg',
        },
        .... any other user fields....
    }
}

In order to search for any comments by user $user, you would need to do this:

$view->type('comment')
     ->filterb(
            'user.uid.index' => $user->uid->index,
            'user.uid.type'  => $user->uid->type,
            'user.uid.id'    => $user->uid->id,
       )
     ->search;

THE SOLUTION

With Elastic::Model::SearchBuilder, you can do it as follows:

$view->type('comment')
     ->filterb( user => $user )
     ->search;

Or with the UID:

$view->type('comment')
     ->filterb( user => $user->uid )
     ->search;

FURTHER EXAMPLES

Query or Filter

This works for both queries and filters, eg:

$view->queryb ( user => $user )->search;
$view->filterb( user => $user )->search;

Doc or UID

You can use either the doc/object itself, or an Elastic::Model::UID object:

$uid = $user->uid;
$view->queryb ( user => $uid )->search;
$view->filterb( user => $uid )->search;

Negating queries:

$view->queryb ( user => { '!=' => $user })->search;
$view->filterb( user => { '!=' => $user })->search;

"IN" queries

$view->queryb ( user => \@users )->search;
$view->filterb( user => \@users )->search;

"NOT IN" queries

$view->queryb ( user => { '!=' => \@users })->search;
$view->filterb( user => { '!=' => \@users })->search;

AUTHOR

Clinton Gormley <drtech@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Clinton Gormley.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.