Dancer/SearchApp/IndexSchema.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 103 CC 4 SCAR 20.2
CC
4
Cov
0%
find_or_create_index
20.0
multilang_text
2.0
create_mapping
2.0
CRAP 7.5
1 package Dancer::SearchApp::IndexSchema;
2 use strict;
3 use Exporter 'import';
4 use Data::Dumper;
5 use Promises 'deferred';
6  
7 use JSON::MaybeXS;
8 my $true = JSON->true;
9 my $false = JSON->false;
10  
11 =head1 NAME
12  
13 Dancer::SearchApp::IndexSchema - schema definition for the Elasticsearch index
14  
15 Consider maybe extending this so different items (like Tweets, RSS)
16 have different fields instead of mushing all metadata into the same schema
17  
18 =cut
19  
20 use vars qw(@EXPORT_OK $VERSION @types);
21 $VERSION = '0.06';
22 @EXPORT_OK = qw(create_mapping multilang_text find_or_create_index %indices %analyzers );
23  
24 # Maybe we should move away from the type so we can rely on just the id of a document
25 @types = (qw(file mail http));
26  
27 =head2 Boost direct matches, still allow synonyms
28  
29 PUT /<index_name>/<type_name>/_mapping
30 {
31 "<type>": {
32 "properties": {
33 "title": {
34 "type": "string",
35 "fields": {
36 "exact": {
37 "type": "string",
38 "index": "not_analyzed"
39 },
40 "synonym": {
41 "type": "string",
42 "index": "analyzed",
43 "analyzer": "synonym_analyzer"
44 },
45 "stemmed": {
46 "type": "string",
47 "index": "analyzed",
48 "analyzer": "stemming_analyzer"
49 }
50 }
51 }
52 }
53 }
54 }
55  
56 And the following query should match as you wish :
57  
58 POST /<index_name>/<type_name>/_search
59 {
60 "query": {
61 "multi_match": {
62 "query": "injury",
63 "fields": [
64 "title.exact^3",
65 "title.synonym^2",
66 "title.stemmed"
67 ]
68 }
69 }
70 }
71  
72 =cut
73  
74 # Datenstruktur für ES Felder, deren Sprache wir nicht kennen
75 sub multilang_text($$) {
76 my($name, $analyzer)= @_;
77 return {
78 "type" => "text",
79  
80 # Also for the suggestion box
81 "fields" => {
82 $name => {
83 "type" => "text",
84 "analyzer" => $analyzer,
85 "index" => "analyzed",
86 "store" => $true,
87 },
88 "${name}_synonyms" => {
89 "type" => "text",
90 # This should be configurable per language/synonyms
91 "analyzer" => 'searchapp_synonyms_en',
92 "index" => "analyzed",
93 "store" => $true,
94 },
95
96 # This is misnamed - it's more the autocorrect filter
97 # usable for "did you mean XY" responses
98 "autocomplete" => {
99 "analyzer" => "analyzer_shingle",
100 "search_analyzer" => "analyzer_shingle",
101 #"index_analyzer" => "analyzer_shingle",
102 "type" => "text",
103 "store" => $true,
104 },
105 }
106 };
107 };
108  
109 =head2 C<< create_mapping >>
110  
111 Defines a Dancer::SearchApp index. This is currently the following
112 specification:
113  
114 "properties" => {
115 "url" => { type => "text" }, # file://-URL
116 "title" => multilang_text('title',$analyzer),
117 "author" => multilang_text('author', $analyzer),
118 "content" => multilang_text('content',$analyzer),
119 'mime_type' => { type => "string" }, # text/html etc.
120 "creation_date" => {
121 "type" => "date",
122 "format" => "yyyy-MM-dd HH:mm:ss",
123 },
124 },
125  
126 =cut
127  
128 sub create_mapping {
129 my( $analyzer ) = @_;
130 $analyzer ||= 'english';
131 my $mapping = {
132 "properties" => {
133 "url" => { type => "text" }, # file://-URL
134 "title" => multilang_text('title',$analyzer),
135  
136 # Automatic (title) completion to their documents
137 # https://www.elastic.co/blog/you-complete-me
138 "title_suggest" => {
139 "type" => "completion",
140 #"payloads" => $true,
141 # Also add synonym filter
142 # Also add lowercase anaylzer
143 },
144
145 "author" => multilang_text('author', $analyzer),
146 "content" => multilang_text('content',$analyzer),
147 "folder" => {
148 "type" => "text",
149 "analyzer" => $analyzer,
150 # Some day I'll know how to have a separate tokenizer per-field
151 # "tokenizer" => "path_hierarchy",
152 },
153 # This could also be considered a path_hierarchy
154 'mime_type' => { type => "text", index => 'not_analyzed' }, # text/html etc.
155 "creation_date" => {
156 "type" => "date",
157 "format" => "yyyy-MM-dd HH:mm:ss",
158 },
159 },
160 };
161 };
162  
163 =head2 C<< find_or_create_index >>
164  
165 my $found = find_or_create_index( $es, $index_name, $lang, $type );
166 $found->then( sub {
167 my( $name ) = @_;
168 print "Using index '$name'\n";
169 });
170  
171 Returns the full name for the index C<$index_name>, concatenated with the
172 language. The language is important to chose the correct stemmer. Existing
173 indices will be cached in the package global variable C<%indices>.
174  
175 =cut
176  
177 use vars qw(%pending_creation %indices %analyzers );
178 sub find_or_create_index {
179 my( $e, $index_name, $lang, $type ) = @_;
180
181 my $res = deferred;
182
183 my $full_name = "$index_name-$lang";
184 #warn "Initializing deferred for $full_name";
185 #warn join ",", sort keys %indices;
186 if( ! $indices{ $full_name }) {
187 #warn "Checking for '$full_name'";
188 $e->indices->exists( index => $full_name )
189 ->then( sub{
190 if( $_[0] ) { # exists
191 #warn "Full name: $full_name";
192 $res->resolve( $full_name );
193  
194 # index creation in progress
195 } elsif( $pending_creation{ $full_name }) {
196 #warn "push Pending";
197 push @{ $pending_creation{ $full_name } }, $res;
198  
199 # we need to create it ourselves
200 } else {
201 $pending_creation{ $full_name } = [];
202 #warn "Creating";
203 my $mapping = create_mapping($analyzers{$lang});
204 #warn Dumper $mapping;
205 my @typemap = map { $_ => $mapping } @types;
206 $e->indices->create(index=>$full_name,
207 body => {
208 settings => {
209 analysis => {
210 analyzer => {
211 "analyzer_shingle" => {
212 "tokenizer" => "standard",
213 "filter" => ["standard", "lowercase", "filter_stop", "filter_underscores", "filter_shingle"],
214 },
215 # Synonyms should be configurable per language
216 "searchapp_synonyms_en" => {
217 "tokenizer" => "standard",
218 "filter" => ["standard", "lowercase", "searchapp_synonyms_en"],
219 },
220 "searchapp_en" => {
221 "tokenizer" => "standard",
222 "filter" => ["filter_stem_${lang}"],
223 },
224 #"filter" => ['standard','lowercase',"${analyzer}_stemmer"],
225 },
226 "filter" => {
227 # Synonyms should be configurable per language
228 "searchapp_synonyms_en" => {
229 "type" => "synonym",
230 # relative to the ES config directory
231 "synonyms_path" => "synonyms/synonyms_en.txt"
232 },
233 "filter_stem_${lang}" => {
234 type => "stemmer",
235 name => $lang,
236 },
237 "filter_underscores" => {
238 "type" => "stop",
239 "stopwords" => ['_'],
240 },
241 "filter_stop" => {
242 "type" => "stop",
243 # We'll need another filter to filter out the underscores...
244 },
245 "filter_shingle" => {
246 "type" =>"shingle",
247 "max_shingle_size" => 5,
248 "min_shingle_size" => 2,
249 "output_unigrams" => $true,
250 },
251 "ngram" => {
252 "type" => "ngram",
253 "min_gram" => 2,
254 "max_gram" => 15, # long enough even for German
255 },
256 },
257 },
258
259 mapper => { dynamic => $false }, # this is "use strict;" for ES
260 "number_of_replicas" => 0,
261 },
262 "mappings" => {
263 # Hier muessen/sollten wir wir die einzelnen Typen definieren
264 # https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
265 #$type => $mapping,
266 # One schema fits all
267 @typemap,
268 },
269 })->then(sub {
270 my( $created ) = @_;
271 #warn "Full name: $full_name";
272 $res->resolve( $full_name );
273 for( @{ $pending_creation{ $full_name }}) {
274 $_->resolve( $full_name );
275 };
276 delete $pending_creation{ $full_name };
277 }, sub { warn "Couldn't create index $full_name: " . $_[0]->{text} });
278 };
279 });
280 } else {
281 #warn "Cached '$full_name'";
282 $res->resolve( $full_name );
283 };
284 return $res->promise
285 };
286  
287 1;
288 =head1 REPOSITORY
289  
290 The public repository of this module is
291 L<https://github.com/Corion/dancer-searchapp>.
292  
293 =head1 SUPPORT
294  
295 The public support forum of this module is
296 L<https://perlmonks.org/>.
297  
298 =head1 TALKS
299  
300 I've given a talk about this module at Perl conferences:
301  
302 L<German Perl Workshop 2016, German|http://corion.net/talks/dancer-searchapp/dancer-searchapp.html>
303  
304 =head1 BUG TRACKER
305  
306 Please report bugs in this module via the RT CPAN bug queue at
307 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Dancer-SearchApp>
308 or via mail to L<dancer-searchapp-Bugs@rt.cpan.org>.
309  
310 =head1 AUTHOR
311  
312 Max Maischein C<corion@cpan.org>
313  
314 =head1 COPYRIGHT (c)
315  
316 Copyright 2014-2016 by Max Maischein C<corion@cpan.org>.
317  
318 =head1 LICENSE
319  
320 This module is released under the same terms as Perl itself.
321  
322 =cut