NAME

Data::AnyXfer::Elastic::Role::Project

SYNOPSIS

with 'Data::AnyXfer::Elastic::Role::Project';

# The consuming module must implement a attribute called 'index_info'
# which returns an IndexInfo instance describing the Elasticsearch index.

$results = $self->_es_simple_search(
    body => {
        query => {
            term => {
                "first_name" => { value => "jessica" },
            }
        },
        size => 10,
    }
);

DESCRIPTION

Data::AnyXfer::Elastic::Role::Project implements common Elasticsearch helper methods into your project.

ATTRIBUTES

es

Stores a Data::AnyXfer::Elastic::Index object that is connected to the Elasticsearch index and type specified in index_info.

METHODS

index_info

index_info must be implemented by your project too define the projects Elasticsearch index. See Data::AnyXfer::Elastic::IndexInfo.

_es_simple_search( $args )

$searcher->_es_simple_search(
    body => {
        query => {
            term => {
                "first_name" => { value => "jessica" },
            }
        }
    }
);

Output:

[
    {
        email       => "jedwards3@taobao.com",
        first_name  => "Jessica",
        id          => 4,
        last_name   => "Edwards"
    },
    {
        email       => "jhart4@dot.gov",
        first_name  => "Gerald",
        id          => 5,
        last_name   => "Hart"
    }
]

_es_simple_search provides a general search mechanism to quickly search a index. The relevant index name and index_type are automatically injected into the request - and only search body arguments need to be included. These can be found in Search::Elasticsearch::Client::Direct and official Elasticsearch documentation. The method returns the extracted results body.

See "Scrolling Searches" in ..

_es_extract_results

$results = $self->_es_extract_results( $es_search_response );

Elasticsearch by default returns search results with meta data regarding shard statuses and search duration. Most of the time this is useless. This method strips this meta data and returns an array reference of search results directly.

# default radial search, defaults to 0 - 100km

$results = $self->_es_simple_radial_search( from_point => [0.12, 51.5] );

# radial search within 10 kilometres

$results = $self->_es_simple_radial_search(
    from_point => { lon => 0.12, lat => 51.5 },
    max_distance => '10km' );

# nearest cities within 100 kilometres ordered by distance

$results = $self->_es_simple_radial_search(
    from_point => [0.12, 51.5],
    body => { query => { term => { category => 'city' } } } );

# top 5 nearest cinemas within 6 kilometres, using a boost value of .5 for distance
# (it's more important that it's a cinema first, then distance second)

$results = $self->_es_simple_radial_search(
    from_point => { lon => 0.12, lat => 51.5 },
    max_distance => '6km',
    distance_boost => .5,
    body => { query => term => { description => 'cinema' } } },
    size => 5);
# default polygon search (max 2000 results)
$polygon1 = [ [$lon1, $lat1], [$lon2, $lat2], [$lon3, $lat3]];
$results = $self->_es_simple_polygon_search(polygons => [$polygon1, $polygon2]);

# default polygon search, results sorted by price (max 2000 results)
$results = $self->_es_simple_polygon_search(
    body => { sort => [ { price => "asc" } ] },
    polygons => [$polygon1, $polygon2],
);
# default point search

my $point1 = [$lon1, $lat1];

my $results = $self->_es_simple_point_search( points => $point1 );

this search finds the intersection of the provided point with
any document with a geo_shape polygon which containes that point

Scrolling Searches

Passing _es_simple_search - or any function that extends its functionality - a scroll_size returns a Data::AnyXfer::Elastic::ScrollHelper instead of a result. This can then be used for reading batched results from ES.

scroll_size

The number of results to return for each batch from each node in the ES cluster. The total size of the batch returned is scroll_size * es_nodes.

If this is set then scrolling will be used, an instance of Data::AnyXfer::Elastic::ScrollHelper will be returned instead of the normal results.

search_type

Efficient scrolling can be set by setting the search_type to scan, saving on the innefficient sorting phase.

scroll

Set the duration to keep the results alive, for processing before the next set of results is fetched. Default is 5m.

ENVIRONMENT

ES_DEBUG

$ENV{ES_DEBUG} = 1;

Set debugging printing of ES calls.

COPYRIGHT

This software is copyright (c) 2019, Anthony Lucas.

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