| 1 | package Search::Elasticsearch::Compat; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use Search::Elasticsearch 1.10; | ||
| 5 | |||
| 6 | use Scalar::Util qw(openhandle); | ||
| 7 | use Search::Elasticsearch::Util qw(parse_params); | ||
| 8 | use namespace::clean; | ||
| 9 | |||
| 10 | our $VERSION = '0.10'; | ||
| 11 | |||
| 12 | #=================================== | ||
| 13 | sub new { | ||
| 14 | #=================================== | ||
| 15 | my ( $class, $orig ) = parse_params(@_); | ||
| 16 | my %params = ( | ||
| 17 | nodes => delete $orig->{servers}, | ||
| 18 | cxn_pool => delete $orig->{no_refresh} ? 'Static' : 'Sniff', | ||
| 19 | client => 'Compat', | ||
| 20 | deflate => delete $orig->{deflate} || 0, | ||
| 21 | max_content_length => delete $orig->{max_content_length} || 0 | ||
| 22 | ); | ||
| 23 | |||
| 24 | $params{request_timeout} = delete $orig->{timeout} | ||
| 25 | if $orig->{timeout}; | ||
| 26 | |||
| 27 | # transport | ||
| 28 | my $transport = delete $orig->{transport} || 'http'; | ||
| 29 | if ( $transport =~ /^(aehttp|aecurl|thrift)/ ) { | ||
| 30 | die "Transport <$transport> is not supported"; | ||
| 31 | } | ||
| 32 | if ( $transport eq 'httplite' ) { | ||
| 33 | warn "Transport <httplite> is not supported. Using <httptiny>"; | ||
| 34 | } | ||
| 35 | $params{cxn} | ||
| 36 | = $transport eq 'httptiny' ? 'HTTPTiny' | ||
| 37 | : $transport eq 'curl' ? 'NetCurl' | ||
| 38 | : 'LWP'; | ||
| 39 | |||
| 40 | # trace_calls | ||
| 41 | if ( my $trace = delete $orig->{trace_calls} ) { | ||
| 42 | $params{trace_to} | ||
| 43 | = $trace eq '1' ? 'Stderr' | ||
| 44 | : !ref $trace ? [ 'File', $trace ] | ||
| 45 | : openhandle($trace) ? [ 'FileHandle', $trace ] | ||
| 46 | : ref($trace) eq 'CODE' ? [ 'Callback', { logging_cb => $trace } ] | ||
| 47 | : die "Unrecognised value for <trace_requests>"; | ||
| 48 | } | ||
| 49 | |||
| 50 | return Search::Elasticsearch->new( %params, %$orig ); | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | =pod | ||
| 56 | |||
| 57 | =encoding UTF-8 | ||
| 58 | |||
| 59 | =head1 NAME | ||
| 60 | |||
| 61 | Search::Elasticsearch::Compat - A compatibility layer for migrating from ElasticSearch.pm | ||
| 62 | |||
| 63 | =head1 VERSION | ||
| 64 | |||
| 65 | version 0.10 | ||
| 66 | |||
| 67 | =head1 DESCRIPTION | ||
| 68 | |||
| 69 | With the release of the official new L<Search::Elasticsearch> module, the old | ||
| 70 | L<ElasticSearch> (note the change in case) module has been deprecated. This module, | ||
| 71 | L<Search::Elasticsearch::Compat> is a compatibility layer to help migrate | ||
| 72 | existing code from the old module to the new. | ||
| 73 | |||
| 74 | The client interface (ie L</new()> plus all request methods like | ||
| 75 | L<search()|Search::Elasticsearch::Client::Compat/search()>) are completely compatible | ||
| 76 | with the old ElasticSearch.pm. The L</new()> method translates the parameters | ||
| 77 | accepted by the old module to the parameters accepted by the new module. All | ||
| 78 | tests in the old test suite pass. | ||
| 79 | |||
| 80 | However, the networking layer has been replaced by the new L<Search::Elasticsearch> | ||
| 81 | module. The available transport backends are C<http> (L<LWP> | ||
| 82 | via L<Search::Elasticsearch::Cxn::LWP>), C<httptiny> (L<HTTP::Tiny> | ||
| 83 | via L<Search::Elasticsearch::Cxn::HTTPTiny> and C<curl> (L<Net::Curl> via | ||
| 84 | L<Search::Elasticsearch::Cxn::NetCurl>). The L<AnyEvent> backends are not supported. | ||
| 85 | That may change in the future. | ||
| 86 | |||
| 87 | No further development of this compatibility layer is planned. It allows | ||
| 88 | you to use your old code without change (other than the module name), but | ||
| 89 | new code should use the new L<Search::Elasticsearch> module. | ||
| 90 | |||
| 91 | To use this module, you will need to change: | ||
| 92 | |||
| 93 | use ElasticSearch; | ||
| 94 | my $e = ElasticSearch->new(...); | ||
| 95 | |||
| 96 | to | ||
| 97 | |||
| 98 | use Search::Elasticsearch::Compat; | ||
| 99 | my $e = Search::Elasticsearch::Compat->new(...); | ||
| 100 | |||
| 101 | You can use the official client in the same code as the compatibility | ||
| 102 | layer with: | ||
| 103 | |||
| 104 | use Search::Elasticsearch; | ||
| 105 | use Search::Elasticsearch::Compat; | ||
| 106 | |||
| 107 | my $new_es = Search::Elasticsearch->new(...); | ||
| 108 | my $old_es = Search::Elasticsearch::Compat->new(...); | ||
| 109 | |||
| 110 | =head1 Creating a new Search::Elasticsearch::Compat instance | ||
| 111 | |||
| 112 | =head2 new() | ||
| 113 | |||
| 114 | $es = Search::Elasticsearch::Compat->new( | ||
| 115 | transport => 'http', | ||
| 116 | servers => '127.0.0.1:9200' # single server | ||
| 117 | | ['es1.foo.com:9200', | ||
| 118 | 'es2.foo.com:9200'], # multiple servers | ||
| 119 | trace_calls => 1 | '/path/to/log/file' | $fh | ||
| 120 | timeout => 30, | ||
| 121 | |||
| 122 | no_refresh => 0 | 1 # don't retrieve the live | ||
| 123 | # server list. Instead, use | ||
| 124 | # just the servers specified | ||
| 125 | ); | ||
| 126 | |||
| 127 | The L</new()> method translates the parameters accepted by the old module into | ||
| 128 | parameters accepted by the new L<Search::Elasticsearch> module, and | ||
| 129 | returns an L<Search::Elasticsearch::Client::Compat> instance, | ||
| 130 | which provides the same methods as were available in the old L<ElasticSearch> | ||
| 131 | module. | ||
| 132 | |||
| 133 | =head3 servers | ||
| 134 | |||
| 135 | C<servers> can be either a single server or an ARRAY ref with a list of servers. | ||
| 136 | If not specified, then it defaults to C<localhost:9200>. | ||
| 137 | |||
| 138 | These servers are used in a round-robin fashion. If any server fails to | ||
| 139 | connect, then the other servers in the list are tried, and if any | ||
| 140 | succeeds, then a list of all servers/nodes currently known to the | ||
| 141 | Elasticsearch cluster are retrieved and stored. | ||
| 142 | This list of known nodes is refreshed automatically. | ||
| 143 | |||
| 144 | =head3 no_refresh | ||
| 145 | |||
| 146 | Retrieving the list of live nodes from the cluster may not be desirable behaviour | ||
| 147 | if, for instance, you are connecting to remote servers which use internal | ||
| 148 | IP addresses, or which don't allow remote C<nodes()> requests. | ||
| 149 | |||
| 150 | If you want to disable the sniffing behaviour, set C<no_refresh> to C<1>, | ||
| 151 | in which case the transport module will round robin through the | ||
| 152 | C<servers> list only. Failed nodes will be removed from the list | ||
| 153 | (but added back in later if they respond to a ping or when all nodes have failed). | ||
| 154 | |||
| 155 | =head3 Transport Backends | ||
| 156 | |||
| 157 | There are three C<transport> backends that Search::Elasticsearch::Compat can use: | ||
| 158 | C<http> (L<LWP> via L<Search::Elasticsearch::Cxn::LWP>), C<httptiny> (L<HTTP::Tiny> | ||
| 159 | via L<Search::Elasticsearch::Cxn::HTTPTiny> and C<curl> (L<Net::Curl> via | ||
| 160 | L<Search::Elasticsearch::Cxn::NetCurl>). The L<AnyEvent> backends are not supported. | ||
| 161 | |||
| 162 | The C<httptiny> backend is faster than C<http>, but does not use persistent | ||
| 163 | connections. If you want to use it, make sure that your open filehandles limit | ||
| 164 | (C<ulimit -l>) is high, or your connections may hang because your system runs | ||
| 165 | out of sockets. The C<curl> backend is the fastest and uses persistent | ||
| 166 | connections, but must be installed separately as it relies on | ||
| 167 | C<libcurl>. | ||
| 168 | |||
| 169 | =head1 OTHER METHODS | ||
| 170 | |||
| 171 | See L<Search::Elasticsearch::Client::Compat> for documenation of methods supported | ||
| 172 | by the client. | ||
| 173 | |||
| 174 | =head1 SUPPORT | ||
| 175 | |||
| 176 | You can find documentation for this module with the perldoc command. | ||
| 177 | |||
| 178 | perldoc Search::Elasticsearch::Compat | ||
| 179 | |||
| 180 | You can also look for information at: | ||
| 181 | |||
| 182 | =over 4 | ||
| 183 | |||
| 184 | =item * GitHub | ||
| 185 | |||
| 186 | L<http://github.com/elasticsearch/elasticsearch-perl-compat> | ||
| 187 | |||
| 188 | =item * Search MetaCPAN | ||
| 189 | |||
| 190 | L<https://metacpan.org/module/Search::Elasticsearch::Compat> | ||
| 191 | |||
| 192 | =item * IRC | ||
| 193 | |||
| 194 | The L<#elasticsearch|irc://irc.freenode.net/elasticsearch> channel on | ||
| 195 | C<irc.freenode.net>. | ||
| 196 | |||
| 197 | =item * Mailing list | ||
| 198 | |||
| 199 | The main L<Elasticsearch mailing list|http://www.elasticsearch.org/community/forum/>. | ||
| 200 | |||
| 201 | =back | ||
| 202 | |||
| 203 | =head1 TEST SUITE | ||
| 204 | |||
| 205 | The full test suite requires a live Elasticsearch cluster to run. CPAN | ||
| 206 | testers doesn't support this. You can see full test results here: | ||
| 207 | L<http://travis-ci.org/#!/clintongormley/Search::Elasticsearch::Compat/builds>. | ||
| 208 | |||
| 209 | To run the full test suite locally, run it as: | ||
| 210 | |||
| 211 | perl Makefile.PL | ||
| 212 | ES_HOME=/path/to/elasticsearch make test | ||
| 213 | |||
| 214 | =head1 AUTHOR | ||
| 215 | |||
| 216 | Clinton Gormley <drtech@cpan.org> | ||
| 217 | |||
| 218 | =head1 COPYRIGHT AND LICENSE | ||
| 219 | |||
| 220 | This software is Copyright (c) 2014 by Elasticsearch BV. | ||
| 221 | |||
| 222 | This is free software, licensed under: | ||
| 223 | |||
| 224 | The Apache License, Version 2.0, January 2004 | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | __END__ |