line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ElasticSearchX::Autocomplete::GeoNames; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
28574
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings FATAL => 'all', NONFATAL => 'redefine'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1644
|
use ElasticSearchX::Autocomplete 0.06; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use ElasticSearchX::Autocomplete::Util qw( |
8
|
|
|
|
|
|
|
_create_accessors _params |
9
|
|
|
|
|
|
|
_debug _try_cache cache_key |
10
|
|
|
|
|
|
|
); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use Carp; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
__PACKAGE__->_create_accessors( |
17
|
|
|
|
|
|
|
['cache'], |
18
|
|
|
|
|
|
|
['debug'], |
19
|
|
|
|
|
|
|
['JSON'], |
20
|
|
|
|
|
|
|
['auto'], |
21
|
|
|
|
|
|
|
['auto_type'], |
22
|
|
|
|
|
|
|
[ 'es', q(croak "Missing required param 'es'") ], |
23
|
|
|
|
|
|
|
[ 'index', q('geonames') ], |
24
|
|
|
|
|
|
|
[ 'type', q('place') ], |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#=================================== |
28
|
|
|
|
|
|
|
sub new { |
29
|
|
|
|
|
|
|
#=================================== |
30
|
|
|
|
|
|
|
my ( $proto, $params ) = _params(@_); |
31
|
|
|
|
|
|
|
my $class = ref $proto || $proto; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $self = { _debug => 0 }; |
34
|
|
|
|
|
|
|
bless $self, $class; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my %auto_params; |
37
|
|
|
|
|
|
|
for ( keys %$params ) { |
38
|
|
|
|
|
|
|
if ( $self->can($_) ) { |
39
|
|
|
|
|
|
|
$self->$_( $params->{$_} ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else { |
42
|
|
|
|
|
|
|
$auto_params{$_} = $params->{$_}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$self->_init( \%auto_params ); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
return $self; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
#=================================== |
52
|
|
|
|
|
|
|
sub _init { |
53
|
|
|
|
|
|
|
#=================================== |
54
|
|
|
|
|
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
my $params = shift; |
56
|
|
|
|
|
|
|
my $auto = ElasticSearchX::Autocomplete->new( |
57
|
|
|
|
|
|
|
( map { $_ => $self->$_ } qw(index cache debug es ) ), |
58
|
|
|
|
|
|
|
types => { |
59
|
|
|
|
|
|
|
$self->type => { |
60
|
|
|
|
|
|
|
custom_fields => { |
61
|
|
|
|
|
|
|
parent_ids => { type => 'integer' }, |
62
|
|
|
|
|
|
|
place_id => { type => 'integer' }, |
63
|
|
|
|
|
|
|
label_short => { type => 'string', index => 'no' }, |
64
|
|
|
|
|
|
|
dup_of => { type => 'integer' }, |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
geoloc => 1, |
67
|
|
|
|
|
|
|
multi_tokens => 1, |
68
|
|
|
|
|
|
|
formatter => \&_format_results, |
69
|
|
|
|
|
|
|
%$params, |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
$self->auto($auto); |
74
|
|
|
|
|
|
|
$self->auto_type( $auto->type( $self->type ) ); |
75
|
|
|
|
|
|
|
$self->JSON( $auto->JSON ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
#=================================== |
79
|
|
|
|
|
|
|
sub suggest { |
80
|
|
|
|
|
|
|
#=================================== |
81
|
|
|
|
|
|
|
my $self = shift; |
82
|
|
|
|
|
|
|
my ( $phrase, $params ) = _params(@_); |
83
|
|
|
|
|
|
|
$params->{context} = delete $params->{lang}; |
84
|
|
|
|
|
|
|
return $self->auto_type->suggest( $phrase, $params ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
#=================================== |
88
|
|
|
|
|
|
|
sub suggest_json { |
89
|
|
|
|
|
|
|
#=================================== |
90
|
|
|
|
|
|
|
my $self = shift; |
91
|
|
|
|
|
|
|
my ( $phrase, $params ) = _params(@_); |
92
|
|
|
|
|
|
|
$params->{context} = delete $params->{lang}; |
93
|
|
|
|
|
|
|
return $self->auto_type->suggest_json( $phrase, $params ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
our $as_json; |
97
|
|
|
|
|
|
|
#=================================== |
98
|
|
|
|
|
|
|
sub get_place { |
99
|
|
|
|
|
|
|
#=================================== |
100
|
|
|
|
|
|
|
my ( $self, $params ) = _params(@_); |
101
|
|
|
|
|
|
|
my $type = $self->auto_type; |
102
|
|
|
|
|
|
|
$params->{context} = $type->clean_context( $params->{lang} ); |
103
|
|
|
|
|
|
|
$params->{index} = $type->index; |
104
|
|
|
|
|
|
|
$params->{type} = $type->name; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
return $self->_try_cache( '_get_place', $params, $as_json ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
#=================================== |
110
|
|
|
|
|
|
|
sub get_place_json { |
111
|
|
|
|
|
|
|
#=================================== |
112
|
|
|
|
|
|
|
my $self = shift; |
113
|
|
|
|
|
|
|
local $as_json = 1; |
114
|
|
|
|
|
|
|
return $self->get_place(@_); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
#=================================== |
118
|
|
|
|
|
|
|
sub _get_place { |
119
|
|
|
|
|
|
|
#=================================== |
120
|
|
|
|
|
|
|
my $self = shift; |
121
|
|
|
|
|
|
|
my $params = shift; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $id = $params->{id} or croak "No place ID passed to get_place"; |
124
|
|
|
|
|
|
|
my $lang = $params->{lang} or croak "No lang passed to get_place"; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $doc = $self->es->get( |
127
|
|
|
|
|
|
|
index => $params->{index}, |
128
|
|
|
|
|
|
|
type => $params->{type}, |
129
|
|
|
|
|
|
|
id => "${id}_${lang}", |
130
|
|
|
|
|
|
|
ignore_missing => 1 |
131
|
|
|
|
|
|
|
) or return undef; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
if (my $dup_id = $doc->{_source}{dup_of}) { |
134
|
|
|
|
|
|
|
return $self->_get_place({%$params, id=>$dup_id}); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
return _localise_place( $lang, $doc ); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
#=================================== |
140
|
|
|
|
|
|
|
sub _localise_place { |
141
|
|
|
|
|
|
|
#=================================== |
142
|
|
|
|
|
|
|
my $lang = shift; |
143
|
|
|
|
|
|
|
my $doc = shift; |
144
|
|
|
|
|
|
|
my $src = $doc->{_source}; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
return { |
147
|
|
|
|
|
|
|
id => $src->{place_id}, |
148
|
|
|
|
|
|
|
lang => $lang, |
149
|
|
|
|
|
|
|
map { $_ => $src->{$_} } |
150
|
|
|
|
|
|
|
qw(location parent_ids rank label label_short), |
151
|
|
|
|
|
|
|
}; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
#=================================== |
155
|
|
|
|
|
|
|
sub _format_results { |
156
|
|
|
|
|
|
|
#=================================== |
157
|
|
|
|
|
|
|
my $auto = shift; |
158
|
|
|
|
|
|
|
my $params = shift; |
159
|
|
|
|
|
|
|
my $results = shift; |
160
|
|
|
|
|
|
|
my $lang = substr( $params->{context}, 1 ); |
161
|
|
|
|
|
|
|
$results = [ map { _localise_place( $lang, $_ ) } @$results ]; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
#=================================== |
165
|
|
|
|
|
|
|
sub admin { |
166
|
|
|
|
|
|
|
#=================================== |
167
|
|
|
|
|
|
|
my ( $self, $params ) = _params(@_); |
168
|
|
|
|
|
|
|
require ElasticSearchX::Autocomplete::GeoNames::Admin; |
169
|
|
|
|
|
|
|
ElasticSearchX::Autocomplete::GeoNames::Admin->new( |
170
|
|
|
|
|
|
|
geonames => $self, |
171
|
|
|
|
|
|
|
debug => $self->debug, |
172
|
|
|
|
|
|
|
%$params, |
173
|
|
|
|
|
|
|
); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# ABSTRACT: Autocomplete of geolocation data from GeoNames |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1 |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
__END__ |