« blib/lib/DBIx/Class/ElasticSync/Role/ElasticResult.pm
↑ Summary
blib/lib/DBIx/Class/ElasticSync/Role/ElasticSchema.pm »
| 1 | package DBIx::Class::ElasticSync::ResultSet; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use DBIx::Class::ResultClass::HashRefInflator; | ||
| 7 | use Search::Elasticsearch::Compat::QueryParser; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use Moose; | ||
| 11 | |||
| 12 | has body => ( | ||
| 13 | is => 'rw', | ||
| 14 | isa => 'HashRef', | ||
| 15 | required => 0, | ||
| 16 | default => sub { {} } | ||
| 17 | ); | ||
| 18 | |||
| 19 | has queries => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'ArrayRef', | ||
| 22 | required => 0, | ||
| 23 | default => sub { [] } | ||
| 24 | ); | ||
| 25 | |||
| 26 | has filters => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => 'ArrayRef', | ||
| 29 | required => 0, | ||
| 30 | default => sub { [] } | ||
| 31 | ); | ||
| 32 | |||
| 33 | has response => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'HashRef', | ||
| 36 | required => 0, | ||
| 37 | default => sub { {} } | ||
| 38 | ); | ||
| 39 | |||
| 40 | has aggs => ( | ||
| 41 | is => 'rw', | ||
| 42 | isa => 'ArrayRef', | ||
| 43 | required => 0, | ||
| 44 | default => sub { [] }, | ||
| 45 | ); | ||
| 46 | |||
| 47 | =head2 size | ||
| 48 | |||
| 49 | Set maximum hit size for the Query | ||
| 50 | |||
| 51 | $elastic_rs->size(10) | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub size { | ||
| 56 | |||
| 57 | my ( $self, $size ) = @_; | ||
| 58 | $self->body->{size} = $size; | ||
| 59 | return $self; | ||
| 60 | |||
| 61 | } | ||
| 62 | |||
| 63 | =head2 from | ||
| 64 | |||
| 65 | Offset for Result | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub from { | ||
| 70 | |||
| 71 | my ( $self, $from ) = @_; | ||
| 72 | $self->body->{from} = $from; | ||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 order_by | ||
| 77 | |||
| 78 | Order the Hits in the Elastic response | ||
| 79 | |||
| 80 | $elastic_rs->order_by( { "fieldname" => "desc" } ) | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub order_by { | ||
| 85 | |||
| 86 | my ( $self, $order ) = @_; | ||
| 87 | |||
| 88 | $self->body->{sort} = $order || [ { "_score" => 'desc' } ]; | ||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 query_string | ||
| 93 | |||
| 94 | Query String for Elastic Query String Query | ||
| 95 | |||
| 96 | $elastic_rs->query_string('foo*') | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub query_string { | ||
| 101 | |||
| 102 | my ( $self, $searchstring ) = @_; | ||
| 103 | |||
| 104 | my $qp = Search::Elasticsearch::Compat::QueryParser->new(); | ||
| 105 | my $filtered_query_string = $qp->filter($searchstring); | ||
| 106 | $self->body->{query}{query_string}{query} = $filtered_query_string; | ||
| 107 | |||
| 108 | return $self; | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 query | ||
| 112 | |||
| 113 | add query to Elastic Request | ||
| 114 | |||
| 115 | $elastic_rs->query( { match_all => {} } ) | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub query { | ||
| 120 | |||
| 121 | my ( $self, $query ) = @_; | ||
| 122 | push @{ $self->queries }, $query; | ||
| 123 | return $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 agg | ||
| 127 | |||
| 128 | add aggregation to Elastic Request | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub agg { | ||
| 133 | |||
| 134 | my ( $self, $params ) = @_; | ||
| 135 | |||
| 136 | return unless $params && ref $params eq 'HASH'; | ||
| 137 | |||
| 138 | push @{ $self->aggs }, $params; | ||
| 139 | |||
| 140 | return $self; | ||
| 141 | } | ||
| 142 | |||
| 143 | =head2 aggregation | ||
| 144 | |||
| 145 | proxy function for "agg" | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub aggregation { | ||
| 150 | |||
| 151 | my ( $self, $params ) = @_; | ||
| 152 | |||
| 153 | return $self->agg($params); | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 filter | ||
| 157 | |||
| 158 | Adds filter to Elastic Request. Deprecated since Elasticsearch 2.x | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub filter { | ||
| 163 | |||
| 164 | my ( $self, $filter ) = @_; | ||
| 165 | push @{ $self->filters }, $filter; | ||
| 166 | return $self; | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | sub filter_rs { | ||
| 171 | |||
| 172 | my ( $self, $params ) = @_; | ||
| 173 | |||
| 174 | return unless ref $params eq 'HASH' && %$params; | ||
| 175 | |||
| 176 | return $self->filter( { term => $params } ); | ||
| 177 | } | ||
| 178 | |||
| 179 | sub exists { | ||
| 180 | |||
| 181 | my ( $self, $column ) = @_; | ||
| 182 | |||
| 183 | return unless $column; | ||
| 184 | |||
| 185 | return $self->filter( { exists => { field => $column } } ); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub range { | ||
| 189 | |||
| 190 | my ( $self, $column, $params ) = @_; | ||
| 191 | |||
| 192 | return unless $column; | ||
| 193 | return unless ref $params eq 'HASH' && %$params; | ||
| 194 | |||
| 195 | return $self->filter( { range => { $column => $params } } ); | ||
| 196 | } | ||
| 197 | |||
| 198 | =head2 highlighter | ||
| 199 | |||
| 200 | Add the highlighter on $field, must be mapped with term_vector => "with_positions_offsets" | ||
| 201 | |||
| 202 | "text" => { type => "string", index => "analyzed", "store" => "yes", "term_vector" => "with_positions_offsets" }, | ||
| 203 | |||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub highlighter { | ||
| 208 | |||
| 209 | my ( $self, $field ) = @_; | ||
| 210 | |||
| 211 | die "Missing field for highlighter" unless $field; | ||
| 212 | |||
| 213 | $self->body->{highlight} = { | ||
| 214 | "number_of_fragments" => 3, | ||
| 215 | "fragment_size" => 150, | ||
| 216 | "tags_schema" => "styled", | ||
| 217 | "pre_tags" => [ '<span class="marking1">', '<span class="marking2">', '<span class="marking3">' ], | ||
| 218 | "post_tags" => [ "</span>", "</span>", "</span>" ], | ||
| 219 | "fields" => { $field => { "number_of_fragments" => 5 }, } | ||
| 220 | }; | ||
| 221 | |||
| 222 | return $self; | ||
| 223 | } | ||
| 224 | |||
| 225 | =head2 all | ||
| 226 | |||
| 227 | Collects all items, based on query / aggreagtions setted | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | sub all { | ||
| 232 | |||
| 233 | my ( $self, $search_params ) = @_; | ||
| 234 | $self->body->{track_scores} = 1; | ||
| 235 | |||
| 236 | $search_params = {} unless ref $search_params eq 'HASH'; | ||
| 237 | |||
| 238 | my $queries = $self->queries; | ||
| 239 | my $filters = $self->filters; | ||
| 240 | my $aggs = $self->aggs; | ||
| 241 | |||
| 242 | if ( @$queries == 1 ) { | ||
| 243 | |||
| 244 | $self->body->{query} = $queries->[0]; | ||
| 245 | |||
| 246 | } elsif ( @$queries > 1 ) { | ||
| 247 | |||
| 248 | $self->body->{query}{and} = $queries; | ||
| 249 | |||
| 250 | } | ||
| 251 | |||
| 252 | if ( @$aggs == 1 ) { | ||
| 253 | |||
| 254 | $self->body->{aggs} = $aggs->[0]; | ||
| 255 | } elsif ( @$aggs > 1 ) { | ||
| 256 | |||
| 257 | $self->body->{aggs} = $aggs; | ||
| 258 | } | ||
| 259 | |||
| 260 | if ( @$filters == 1 ) { | ||
| 261 | |||
| 262 | $self->body->{filter} = $filters->[0]; | ||
| 263 | |||
| 264 | } elsif ( @$filters > 1 ) { | ||
| 265 | |||
| 266 | $self->body->{filter}{and} = $filters; | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | my $response = $self->schema->es->search( | ||
| 271 | index => $self->index_name, | ||
| 272 | type => $self->type, | ||
| 273 | body => $self->body, | ||
| 274 | %$search_params, | ||
| 275 | ); | ||
| 276 | |||
| 277 | $self->response($response); | ||
| 278 | |||
| 279 | return $self; | ||
| 280 | |||
| 281 | } | ||
| 282 | |||
| 283 | =head2 hits | ||
| 284 | |||
| 285 | returns the collected hits from "all" as ArrayRef | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub hits { | ||
| 290 | |||
| 291 | my ($self) = @_; | ||
| 292 | |||
| 293 | my $response = $self->response; | ||
| 294 | |||
| 295 | my $result = []; | ||
| 296 | |||
| 297 | foreach my $match ( @{ $response->{hits}{hits} } ) { | ||
| 298 | |||
| 299 | my $doc = $match->{_source}; | ||
| 300 | my $inner_hits = $match->{inner_hits}; | ||
| 301 | |||
| 302 | if ($inner_hits) { | ||
| 303 | |||
| 304 | foreach my $key ( keys %$inner_hits ) { | ||
| 305 | |||
| 306 | my $inner_results = []; | ||
| 307 | |||
| 308 | foreach my $inner_result ( @{ $match->{inner_hits}{$key}{hits}{hits} } ) { | ||
| 309 | |||
| 310 | push @$inner_results, $inner_result->{_source}; | ||
| 311 | } | ||
| 312 | |||
| 313 | $doc->{inner_hits}->{$key} = $inner_results; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | if ( $self->body->{highlight} ) { | ||
| 318 | |||
| 319 | # TODO fix fix text-assign | ||
| 320 | $doc->{highlight} = join( " … ", @{ $match->{highlight}{text} || [] } ); | ||
| 321 | |||
| 322 | } | ||
| 323 | |||
| 324 | push @$result, $doc; | ||
| 325 | } | ||
| 326 | |||
| 327 | return $result; | ||
| 328 | } | ||
| 329 | |||
| 330 | =head2 buckets | ||
| 331 | |||
| 332 | returns Aggregation buckets, by Aggregation name | ||
| 333 | |||
| 334 | $elastic_rs->buckets('agg_name') | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | sub buckets { | ||
| 339 | |||
| 340 | my ( $self, $agg_name ) = @_; | ||
| 341 | |||
| 342 | my $response = $self->response; | ||
| 343 | |||
| 344 | return unless $response->{aggregations}{$agg_name}; | ||
| 345 | |||
| 346 | my $buckets = []; | ||
| 347 | |||
| 348 | for my $match ( @{ $response->{aggregations}{$agg_name}{buckets} } ) { | ||
| 349 | |||
| 350 | push @$buckets, $match; | ||
| 351 | } | ||
| 352 | |||
| 353 | return $buckets; | ||
| 354 | } | ||
| 355 | |||
| 356 | =head2 es_index | ||
| 357 | |||
| 358 | adds documents from a DBIC Resultset to the Elastic index. It calls the Elastic | ||
| 359 | Resultset es_transform method for denormalizing Data | ||
| 360 | |||
| 361 | $elastic_rs->es_index( $dbic_rs->search_rs( { foo => bar } ) ) | ||
| 362 | |||
| 363 | =cut | ||
| 364 | |||
| 365 | #TODO add example | ||
| 366 | |||
| 367 | sub es_index { | ||
| 368 | |||
| 369 | my $self = shift; | ||
| 370 | my $dbic_rs = shift; | ||
| 371 | |||
| 372 | my $results = $dbic_rs; | ||
| 373 | $results->result_class('DBIx::Class::ResultClass::HashRefInflator'); | ||
| 374 | |||
| 375 | while ( my $row = $results->next ) { | ||
| 376 | |||
| 377 | $row = $self->es_transform( $row, $dbic_rs ); | ||
| 378 | |||
| 379 | $row->{es_id} = $self->es_id( $row, $dbic_rs ); | ||
| 380 | |||
| 381 | $self->es->index( | ||
| 382 | { | ||
| 383 | index => $self->index_name($row), | ||
| 384 | id => $row->{es_id}, | ||
| 385 | type => $self->type, | ||
| 386 | body => $row, | ||
| 387 | } | ||
| 388 | ); | ||
| 389 | } | ||
| 390 | |||
| 391 | } | ||
| 392 | |||
| 393 | sub es_create { | ||
| 394 | |||
| 395 | my $self = shift; | ||
| 396 | my $dbic_rs = shift; | ||
| 397 | |||
| 398 | my $results = $dbic_rs; | ||
| 399 | $results->result_class('DBIx::Class::ResultClass::HashRefInflator'); | ||
| 400 | |||
| 401 | while ( my $row = $results->next ) { | ||
| 402 | |||
| 403 | $row = $self->es_transform( $row, $dbic_rs ); | ||
| 404 | |||
| 405 | $row->{es_id} = $self->es_id( $row, $dbic_rs ); | ||
| 406 | |||
| 407 | my $id = $row->{es_id} ? { id => $row->{es_id} } : {}; | ||
| 408 | |||
| 409 | $self->es->create( | ||
| 410 | { | ||
| 411 | index => $self->index_name($row), | ||
| 412 | type => $self->type, | ||
| 413 | body => $row, | ||
| 414 | %$id, | ||
| 415 | } | ||
| 416 | ); | ||
| 417 | } | ||
| 418 | } | ||
| 419 | |||
| 420 | =head2 es_delete | ||
| 421 | |||
| 422 | Deletes Object from Elasticstorage based on a DBIC Resultset | ||
| 423 | |||
| 424 | =cut | ||
| 425 | |||
| 426 | sub es_delete { | ||
| 427 | |||
| 428 | my $self = shift; | ||
| 429 | my $dbic_rs = shift; | ||
| 430 | |||
| 431 | $dbic_rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); | ||
| 432 | |||
| 433 | while ( my $row = $dbic_rs->next ) { | ||
| 434 | |||
| 435 | my $id = $self->es_id( $row, $dbic_rs ); | ||
| 436 | |||
| 437 | $self->es->delete( | ||
| 438 | id => $id, | ||
| 439 | type => $self->type, | ||
| 440 | index => $self->index_name, | ||
| 441 | ); | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | =head2 es_transform | ||
| 446 | |||
| 447 | Helper Function to denormalize Data. Override it in your ElasticResultSet to transform ResultObjects. | ||
| 448 | |||
| 449 | sub es_tranform { | ||
| 450 | |||
| 451 | my ( $self, $obj ) = @_; | ||
| 452 | |||
| 453 | $obj->{another_document} = $schema->resultset('MyRS')->find( { id => 5 } )->get_columns | ||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | =cut | ||
| 458 | |||
| 459 | sub es_transform { | ||
| 460 | |||
| 461 | my ( $self, $obj ) = @_; | ||
| 462 | |||
| 463 | # overwrite transform in Elasticsearch::ResultSet's to do actions on it | ||
| 464 | return $obj; | ||
| 465 | } | ||
| 466 | |||
| 467 | =head2 es_batch_index | ||
| 468 | |||
| 469 | Indexing huge Resultsets with the Search::Elasticsearch Bulkhelper. | ||
| 470 | |||
| 471 | Use this method for bigger Resultlists to speed up Indexing | ||
| 472 | |||
| 473 | =cut | ||
| 474 | |||
| 475 | sub es_batch_index { | ||
| 476 | warn "Batch Indexing...\n"; | ||
| 477 | |||
| 478 | my $self = shift; | ||
| 479 | my $rs = shift; | ||
| 480 | |||
| 481 | my $batch_size = shift || 1000; | ||
| 482 | my $data = []; | ||
| 483 | |||
| 484 | my $results = $self->index_rs; # add prefetches | ||
| 485 | my $dbic_rs = $self->index_rs; | ||
| 486 | |||
| 487 | $results->result_class('DBIx::Class::ResultClass::HashRefInflator'); | ||
| 488 | |||
| 489 | my $counter = 0; | ||
| 490 | |||
| 491 | my $bulk = $self->es->bulk_helper; | ||
| 492 | |||
| 493 | while ( my $row = $results->next ) { | ||
| 494 | |||
| 495 | my $additional = {}; | ||
| 496 | |||
| 497 | $row = $self->es_transform( $row, $dbic_rs ); | ||
| 498 | $row->{es_id} = $self->es_id( $row, $results ); | ||
| 499 | |||
| 500 | if ( $row->{_parent} ) { | ||
| 501 | $additional->{parent} = delete $row->{_parent}; | ||
| 502 | } | ||
| 503 | |||
| 504 | my $params = { | ||
| 505 | index => $self->index_name($row), | ||
| 506 | id => $row->{es_id}, | ||
| 507 | type => $self->type, | ||
| 508 | source => $row, | ||
| 509 | %$additional, | ||
| 510 | }; | ||
| 511 | |||
| 512 | $bulk->index($params); | ||
| 513 | } | ||
| 514 | |||
| 515 | $bulk->flush; | ||
| 516 | |||
| 517 | 1; | ||
| 518 | } | ||
| 519 | |||
| 520 | =head2 es_id | ||
| 521 | |||
| 522 | Builds the document ID for the Elasticstorage based on the columns setted up in the specific ElasticResultSet | ||
| 523 | |||
| 524 | =cut | ||
| 525 | |||
| 526 | sub es_id { | ||
| 527 | |||
| 528 | my $self = shift; | ||
| 529 | my $row = shift; | ||
| 530 | |||
| 531 | my $pks = $self->es_id_columns; | ||
| 532 | |||
| 533 | return unless scalar @$pks; | ||
| 534 | |||
| 535 | my $ids = []; | ||
| 536 | |||
| 537 | for my $pk (@$pks) { | ||
| 538 | |||
| 539 | push @$ids, $row->{$pk}; | ||
| 540 | } | ||
| 541 | |||
| 542 | return join '_', @$ids; | ||
| 543 | } | ||
| 544 | |||
| 545 | =head2 | ||
| 546 | |||
| 547 | returns Search::Elasticsearch Object from Schemastorage | ||
| 548 | |||
| 549 | =cut | ||
| 550 | |||
| 551 | sub es { | ||
| 552 | |||
| 553 | return shift->schema->es; | ||
| 554 | } | ||
| 555 | |||
| 556 | __PACKAGE__->meta->make_immutable; | ||
| 557 | |||
| 558 | 1; |