| 1 | package Interchange::Search::Solr::UpdateIndex; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use Sub::Quote; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Interchange::Search::Solr::UpdateIndex | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Updates your Solr index. | ||
| 17 | |||
| 18 | =head2 ACCESSORS | ||
| 19 | |||
| 20 | =head3 url | ||
| 21 | |||
| 22 | Solr URL for your collection, e.g. | ||
| 23 | |||
| 24 | http://localhost:9059/solr/collection1 | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | has url => ( | ||
| 29 | is => 'rw', | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | =head3 agent | ||
| 34 | |||
| 35 | HTTP user agent object, defaults to a LWP::UserAgent instance. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | has agent => ( | ||
| 40 | is => 'rw', | ||
| 41 | lazy => 1, | ||
| 42 | default => sub {LWP::UserAgent->new;}, | ||
| 43 | ); | ||
| 44 | |||
| 45 | =head3 status_line | ||
| 46 | |||
| 47 | HTTP status line. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | has status_line => ( | ||
| 52 | is => 'rwp', | ||
| 53 | lazy => 1, | ||
| 54 | default => quote_sub q{return '';}, | ||
| 55 | ); | ||
| 56 | |||
| 57 | =head2 METHODS | ||
| 58 | |||
| 59 | =head3 update | ||
| 60 | |||
| 61 | Updates index. Returns 1 on success. | ||
| 62 | |||
| 63 | Requires C<$mode> parameter which is either I<full> | ||
| 64 | or I<delta>. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub update { | ||
| 69 | my ($self, $mode) = @_; | ||
| 70 | my ($update_url, $command, $request, $response); | ||
| 71 | |||
| 72 | # reset status line | ||
| 73 | $self->_set_status_line(''); | ||
| 74 | |||
| 75 | # construct url and request | ||
| 76 | $update_url = $self->_construct_url($mode); | ||
| 77 | $response = $self->agent->get($update_url); | ||
| 78 | |||
| 79 | # save status line | ||
| 80 | $self->_set_status_line($response->status_line); | ||
| 81 | |||
| 82 | unless ($response->is_success) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | return $response; | ||
| 87 | } | ||
| 88 | |||
| 89 | =head3 query | ||
| 90 | |||
| 91 | Queries index. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub query { | ||
| 96 | my ($self, $query) = @_; | ||
| 97 | my $url = $self->url . '/select?wt=json&'.$query; | ||
| 98 | my $response = $self->agent->get($url); | ||
| 99 | print $url."\n\r---------------\n\r"; | ||
| 100 | return $response; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _construct_url { | ||
| 104 | my ($self, $mode) = @_; | ||
| 105 | |||
| 106 | my $update_url; | ||
| 107 | |||
| 108 | |||
| 109 | if ($mode eq 'clear') { | ||
| 110 | return $self->url . '/update?stream.body=<delete><query>*:*</query></delete>&commit=true'; | ||
| 111 | } | ||
| 112 | |||
| 113 | $update_url = $self->url . '/dataimport?command='; | ||
| 114 | |||
| 115 | if ($mode eq 'full') { | ||
| 116 | $update_url .= 'full-import'; | ||
| 117 | } | ||
| 118 | elsif ($mode eq 'delta') { | ||
| 119 | $update_url .= 'delta-import'; | ||
| 120 | } | ||
| 121 | #$update_url .= '&commit=true'; | ||
| 122 | } | ||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 127 | |||
| 128 | =head1 LICENSE AND COPYRIGHT | ||
| 129 | |||
| 130 | Copyright 2013 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 131 | |||
| 132 | This program is free software; you can redistribute it and/or modify it | ||
| 133 | under the terms of either: the GNU General Public License as published | ||
| 134 | by the Free Software Foundation; or the Artistic License. | ||
| 135 | |||
| 136 | See http://dev.perl.org/licenses/ for more information. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | 1; |