[24446] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # solrbuildproc.pm -- perl wrapper for building index with Solr
|
---|
| 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
| 25 |
|
---|
| 26 | package solrbuildproc;
|
---|
| 27 |
|
---|
| 28 | # This document processor outputs a document for solr to process
|
---|
| 29 |
|
---|
| 30 | # Rather then use the XML structure developed for mgppbuilder/mgppbuildproc
|
---|
| 31 | # whose use was then extended to Lucene, Solr has its own XML syntax:
|
---|
| 32 | #
|
---|
| 33 | # http://wiki.apache.org/solr/UpdateXmlMessages
|
---|
| 34 | #
|
---|
| 35 | # Using this means we don't need to write SolrWrapper.jar, as had to be
|
---|
| 36 | # done for Lucene, translating the XML syntax piped to it into appropriate
|
---|
| 37 | # calls to the Lucene API
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | use lucenebuildproc;
|
---|
| 41 | use ghtml;
|
---|
| 42 | use strict;
|
---|
| 43 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | use IncrementalBuildUtils;
|
---|
| 47 |
|
---|
| 48 | sub BEGIN {
|
---|
| 49 | @solrbuildproc::ISA = ('lucenebuildproc');
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | sub new {
|
---|
| 54 | my $class = shift @_;
|
---|
| 55 | my $self = new lucenebuildproc (@_);
|
---|
| 56 |
|
---|
[33371] | 57 | $self->{'actualfacetfields'} = {};
|
---|
| 58 | $self->{'facetfieldnamemap'} = {};
|
---|
[24446] | 59 | return bless $self, $class;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[27815] | 62 | sub set_facetfields {
|
---|
| 63 | my $self = shift (@_);
|
---|
| 64 |
|
---|
| 65 | my ($facetfields) = @_;
|
---|
| 66 | $self->{'facetfields'} = ();
|
---|
| 67 | # lets just go through and check for text, allfields, metadata which are only valid for indexes, not for facetfields
|
---|
| 68 | foreach my $s (@$facetfields) {
|
---|
| 69 | if ($s !~ /^(text|allfields|metadata)$/) {
|
---|
| 70 | push (@{$self->{'facetfields'}}, $s);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[24446] | 74 |
|
---|
| 75 | #----
|
---|
| 76 |
|
---|
| 77 | sub index_field_mapping_edit {
|
---|
| 78 | my $self = shift (@_);
|
---|
| 79 | my ($doc_obj,$file,$edit_mode) = @_;
|
---|
| 80 |
|
---|
| 81 | # Only add/update gets to here
|
---|
| 82 | # Currently there is no need to distinguish between these edit modes
|
---|
| 83 |
|
---|
| 84 | my $outhandle = $self->{'outhandle'};
|
---|
| 85 |
|
---|
| 86 | # only study this document if it is one to be indexed
|
---|
| 87 | return if ($doc_obj->get_doc_type() ne "indexed_doc");
|
---|
| 88 |
|
---|
| 89 | my $indexed_doc = $self->is_subcollection_doc($doc_obj);
|
---|
| 90 |
|
---|
| 91 | # get the parameters for the output
|
---|
| 92 | # split on : just in case there is subcoll and lang stuff
|
---|
| 93 | my ($fields) = split (/:/, $self->{'index'});
|
---|
| 94 |
|
---|
| 95 | my $doc_section = 0; # just for this document
|
---|
| 96 |
|
---|
| 97 | # get the text for this document
|
---|
| 98 | my $section = $doc_obj->get_top_section();
|
---|
| 99 |
|
---|
| 100 | while (defined $section)
|
---|
| 101 | {
|
---|
| 102 | $doc_section++;
|
---|
| 103 |
|
---|
| 104 | # if we are doing subcollections, then some docs shouldn't be
|
---|
| 105 | # considered for indexing
|
---|
| 106 |
|
---|
| 107 | my $indexed_section
|
---|
| 108 | = $doc_obj->get_metadata_element($section, "gsdldoctype")
|
---|
| 109 | || "indexed_section";
|
---|
| 110 |
|
---|
| 111 | if (($indexed_doc == 0)
|
---|
| 112 | || ($indexed_section ne "indexed_section" && $indexed_section ne "indexed_doc")) {
|
---|
| 113 | $section = $doc_obj->get_next_section($section);
|
---|
| 114 | next;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | # has the user added a 'metadata' index?
|
---|
| 118 | my $all_metadata_specified = 0;
|
---|
| 119 |
|
---|
| 120 | # which fields have already been indexed?
|
---|
| 121 | # (same as fields, but in a map)
|
---|
| 122 | my $specified_fields = {};
|
---|
| 123 |
|
---|
| 124 | # do we have an allfields index??
|
---|
| 125 | my $allfields_index = 0;
|
---|
| 126 |
|
---|
| 127 | # collect up all the text for it in here
|
---|
| 128 | my $allfields_text = "";
|
---|
| 129 |
|
---|
| 130 | foreach my $field (split (/;/, $fields)) {
|
---|
| 131 | if ($field eq "allfields") {
|
---|
| 132 | $allfields_index = 1;
|
---|
| 133 | } elsif ($field eq "metadata") {
|
---|
| 134 | $all_metadata_specified = 1;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | foreach my $field (split (/;/, $fields)) {
|
---|
| 139 |
|
---|
| 140 | # only deal with this field if it doesn't start with top or
|
---|
| 141 | # this is the first section
|
---|
| 142 | my $real_field = $field;
|
---|
| 143 | next if (($real_field =~ s/^top//) && ($doc_section != 1));
|
---|
| 144 |
|
---|
| 145 | # process these two later
|
---|
| 146 | next if ($real_field eq "allfields" || $real_field eq "metadata");
|
---|
| 147 |
|
---|
| 148 | # individual metadata and or text specified
|
---|
| 149 | # -- could be a comma separated list
|
---|
| 150 | $specified_fields->{$real_field} = 1;
|
---|
| 151 |
|
---|
| 152 | if (!defined $self->{'indexfieldmap'}->{$real_field}) {
|
---|
[33371] | 153 | my $shortname = $self->get_or_create_shortname($real_field);
|
---|
[24446] | 154 | $self->{'indexfieldmap'}->{$real_field} = $shortname;
|
---|
| 155 | $self->{'indexfieldmap'}->{$shortname} = 1;
|
---|
| 156 | }
|
---|
| 157 | } # foreach field
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | if ($all_metadata_specified) {
|
---|
| 161 |
|
---|
| 162 | my $new_text = "";
|
---|
| 163 | my $shortname = "";
|
---|
| 164 | my $metadata = $doc_obj->get_all_metadata ($section);
|
---|
| 165 |
|
---|
| 166 | foreach my $pair (@$metadata) {
|
---|
| 167 | my ($mfield, $mvalue) = (@$pair);
|
---|
| 168 |
|
---|
| 169 | # no value
|
---|
| 170 | next unless defined $mvalue && $mvalue ne "";
|
---|
| 171 |
|
---|
| 172 | # we have already indexed this
|
---|
| 173 | next if defined ($specified_fields->{$mfield});
|
---|
| 174 |
|
---|
| 175 | # check fields here, maybe others dont want - change to use dontindex!!
|
---|
| 176 | next if ($mfield eq "Identifier" || $mfield eq "classifytype" || $mfield eq "assocfilepath");
|
---|
| 177 | next if ($mfield =~ /^gsdl/);
|
---|
| 178 |
|
---|
| 179 | if (defined $self->{'indexfieldmap'}->{$mfield}) {
|
---|
| 180 | $shortname = $self->{'indexfieldmap'}->{$mfield};
|
---|
| 181 | }
|
---|
| 182 | else {
|
---|
[33371] | 183 | $shortname = $self->get_or_create_shortname($mfield);
|
---|
[24446] | 184 | $self->{'indexfieldmap'}->{$mfield} = $shortname;
|
---|
| 185 | $self->{'indexfieldmap'}->{$shortname} = 1;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (!defined $self->{'indexfields'}->{$mfield}) {
|
---|
| 189 | $self->{'indexfields'}->{$mfield} = 1;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | if ($allfields_index) {
|
---|
| 195 | # add the index name mapping
|
---|
| 196 | $self->{'indexfieldmap'}->{"allfields"} = "ZZ";
|
---|
| 197 | $self->{'indexfieldmap'}->{"ZZ"} = 1;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | $section = $doc_obj->get_next_section($section);
|
---|
| 201 |
|
---|
| 202 | } # while defined section
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | }
|
---|
[33327] | 206 |
|
---|
| 207 | # UNUSED now by default.
|
---|
| 208 | # Georgy overrode the mgppbuildproc::create_shortname() method in commit 32441 to create the method below, to override the inherited
|
---|
| 209 | # behaviour so that create_shortname() worked appropriately for his use cases involving multiple analyzers.
|
---|
| 210 | # As a result, create_shortname() for solr no longer did a lookup into the %mgppbuildproc::static_indexfield_map for registered shortnames.
|
---|
| 211 | # For the rest, this method is a copy mgppbuildproc::create_shortname().
|
---|
| 212 | # But we want the original mgppbuildproc::create_shortname() behaviour restored, as it does the lookups into %static_indexfield_map that's necessary for us.
|
---|
| 213 | # So we've renamed this function to create_shortname_multi_solr_analyzer below so it won't get called as default beahviour any more.
|
---|
| 214 | # Rename to create_shortname() when requiring Georgy's behaviour.
|
---|
| 215 | sub create_shortname_multi_solr_analyzer {
|
---|
[32441] | 216 | my $self = shift(@_);
|
---|
[24446] | 217 |
|
---|
[32441] | 218 | my ($realname) = @_;
|
---|
| 219 | my @realnamelist = split(",", $realname);
|
---|
| 220 | map {$_=~ s/^[a-zA-Z]+\.//;} @realnamelist; #remove namespaces
|
---|
| 221 | my ($singlename) = $realnamelist[0];
|
---|
| 222 |
|
---|
| 223 | # try our predefined static mapping
|
---|
| 224 | my $name;
|
---|
| 225 | # we can't use the quick map, so join all fields back together (without namespaces), and try sets of two characters.
|
---|
| 226 | $realname = join ("", @realnamelist);
|
---|
| 227 | #try the first two chars
|
---|
| 228 | my $shortname;
|
---|
| 229 | if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
|
---|
| 230 | $shortname = "$1$2";
|
---|
| 231 | } else {
|
---|
| 232 | # there aren't two letdig's in the field - try arbitrary combinations
|
---|
| 233 | $realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
| 234 | $shortname = "AB";
|
---|
| 235 | }
|
---|
| 236 | $shortname =~ tr/a-z/A-Z/;
|
---|
| 237 |
|
---|
| 238 | #if already used, take the first and third letdigs and so on
|
---|
| 239 | my $count = 1;
|
---|
| 240 | while (defined $self->{'indexfieldmap'}->{$shortname}) {
|
---|
| 241 | if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
|
---|
| 242 | $shortname = "$1$3";
|
---|
| 243 | $count++;
|
---|
| 244 | $shortname =~ tr/a-z/A-Z/;
|
---|
| 245 |
|
---|
| 246 | }
|
---|
| 247 | else {
|
---|
| 248 | #remove up to and incl the first letdig
|
---|
| 249 | $realname =~ s/^[^\w]*\w//;
|
---|
| 250 | $count = 0;
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | return $shortname ;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
[24446] | 258 | sub index_field_mapping {
|
---|
| 259 | my $self = shift (@_);
|
---|
| 260 | my ($doc_obj,$file) = @_;
|
---|
| 261 |
|
---|
| 262 | $self->index_field_mapping_edit($doc_obj,$file,"add");
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | sub index_field_mappingreindex
|
---|
| 266 | {
|
---|
| 267 | my $self = shift (@_);
|
---|
| 268 | my ($doc_obj,$file) = @_;
|
---|
| 269 |
|
---|
| 270 | $self->index_field_mapping_edit($doc_obj,$file,"update");
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | sub index_field_mappingdelete
|
---|
| 274 | {
|
---|
| 275 | my $self = shift (@_);
|
---|
| 276 | my ($doc_obj,$file) = @_;
|
---|
| 277 |
|
---|
| 278 | return; # nothing to be done
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | #----
|
---|
| 283 |
|
---|
| 284 | sub textedit {
|
---|
| 285 | my $self = shift (@_);
|
---|
| 286 | my ($doc_obj,$file,$edit_mode) = @_;
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | if (!$self->get_indexing_text()) {
|
---|
| 290 | # In text-compress mode:
|
---|
| 291 | # => want document to be output in the simple <Doc>..</Doc> as is
|
---|
| 292 | # done by its super-class
|
---|
| 293 | return $self->SUPER::textedit(@_);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | # "update" for $edit_mode near identical to "add" as we use Solr in its
|
---|
| 297 | # default mode of replacing an existing document if the new document
|
---|
| 298 | # has the same doc id. Main area of difference between "add" and "update"
|
---|
| 299 | # is that we do not update our 'stats' for number of documents or number
|
---|
| 300 | # of bytes processed. The latter is inaccurate, but considered better
|
---|
| 301 | # than allowing the value to steadily climb.
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | my $solrhandle = $self->{'output_handle'};
|
---|
| 305 | my $outhandle = $self->{'outhandle'};
|
---|
| 306 |
|
---|
| 307 | # only output this document if it is one to be indexed
|
---|
| 308 | return if ($doc_obj->get_doc_type() ne "indexed_doc");
|
---|
| 309 |
|
---|
| 310 | # skip this document if in "compress-text" mode and asked to delete it
|
---|
| 311 | return if (!$self->get_indexing_text() && ($edit_mode eq "delete"));
|
---|
| 312 |
|
---|
| 313 | my $indexed_doc = $self->is_subcollection_doc($doc_obj);
|
---|
| 314 |
|
---|
| 315 | # this is another document
|
---|
| 316 | if ($edit_mode eq "add") {
|
---|
| 317 | $self->{'num_docs'} += 1;
|
---|
| 318 | }
|
---|
| 319 | elsif ($edit_mode eq "delete") {
|
---|
| 320 | $self->{'num_docs'} -= 1;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | # get the parameters for the output
|
---|
| 324 | # split on : just in case there is subcoll and lang stuff
|
---|
| 325 | my ($fields) = split (/:/, $self->{'index'});
|
---|
| 326 |
|
---|
| 327 | my $levels = $self->{'levels'};
|
---|
| 328 | my $ldoc_level = $levels->{'document'};
|
---|
| 329 | my $lsec_level = $levels->{'section'};
|
---|
| 330 |
|
---|
| 331 | my $gs2_docOID = $doc_obj->get_OID();
|
---|
| 332 |
|
---|
| 333 | my $start_doc;
|
---|
| 334 | my $end_doc;
|
---|
| 335 |
|
---|
| 336 | if ($edit_mode eq "add") {
|
---|
| 337 | $start_doc = " <add>\n";
|
---|
| 338 | $start_doc .= " <doc>\n";
|
---|
| 339 | $start_doc .= " <field name=\"docOID\">$gs2_docOID</field>\n";
|
---|
| 340 |
|
---|
| 341 | $end_doc = " </doc>\n";
|
---|
| 342 | $end_doc .= " </add>\n";
|
---|
| 343 | }
|
---|
| 344 | else {
|
---|
| 345 | $start_doc = " <delete>\n";
|
---|
| 346 | $start_doc .= " <id>$gs2_docOID</id>\n";
|
---|
| 347 |
|
---|
| 348 | $end_doc = " </delete>\n";
|
---|
[29945] | 349 |
|
---|
| 350 | # for delete mode, we need to specify just the docOID to delete and we're done
|
---|
| 351 | my $text = $start_doc;
|
---|
| 352 | $text .= $end_doc;
|
---|
| 353 | print $solrhandle $text;
|
---|
| 354 | return;
|
---|
[24446] | 355 | }
|
---|
| 356 |
|
---|
| 357 | # add/update, delete
|
---|
| 358 |
|
---|
| 359 | my $sec_tag_name = "";
|
---|
| 360 | if ($lsec_level)
|
---|
| 361 | {
|
---|
| 362 | $sec_tag_name = $mgppbuildproc::level_map{'section'};
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | my $doc_section = 0; # just for this document
|
---|
| 366 |
|
---|
| 367 | # only output if working with doc level
|
---|
[25846] | 368 | # my $text = undef;
|
---|
| 369 |
|
---|
| 370 | my $text = ($sec_tag_name eq "") ? $start_doc : "";
|
---|
[24446] | 371 |
|
---|
[25846] | 372 | # my $text = $start_doc if ($sec_tag_name eq "");
|
---|
| 373 |
|
---|
[24446] | 374 | # get the text for this document
|
---|
| 375 | my $section = $doc_obj->get_top_section();
|
---|
| 376 |
|
---|
| 377 | while (defined $section)
|
---|
| 378 | {
|
---|
| 379 | # update a few statistics
|
---|
| 380 | $doc_section++;
|
---|
| 381 | $self->{'num_sections'}++;
|
---|
| 382 |
|
---|
| 383 | my $sec_gs2_id = $self->{'num_sections'};
|
---|
| 384 | my $sec_gs2_docOID = $gs2_docOID;
|
---|
| 385 | $sec_gs2_docOID .= ".$section" if ($section ne "");
|
---|
| 386 |
|
---|
| 387 | my $start_sec;
|
---|
| 388 | my $end_sec;
|
---|
| 389 |
|
---|
| 390 | if ($edit_mode eq "add") {
|
---|
| 391 | $start_sec = " <add>\n";
|
---|
| 392 | $start_sec .= " <doc>\n";
|
---|
| 393 | $start_sec .= " <field name=\"docOID\">$sec_gs2_docOID</field>\n";
|
---|
[25846] | 394 |
|
---|
[24446] | 395 | $end_sec = " </doc>\n";
|
---|
| 396 | $end_sec .= " </add>\n";
|
---|
| 397 | }
|
---|
| 398 | else {
|
---|
| 399 | $start_sec = " <delete>\n";
|
---|
| 400 | $start_sec .= " <id>$sec_gs2_docOID</id>\n";
|
---|
| 401 |
|
---|
| 402 | $end_sec = " </delete>\n";
|
---|
[29945] | 403 |
|
---|
| 404 | # for delete mode, should specify only this section's docOID to delete, then move on to the next section
|
---|
| 405 | my $text = $start_sec;
|
---|
| 406 | $text .= $end_sec;
|
---|
| 407 | print $solrhandle $text;
|
---|
| 408 | $section = $doc_obj->get_next_section($section);
|
---|
| 409 | next;
|
---|
[24446] | 410 | }
|
---|
| 411 |
|
---|
| 412 |
|
---|
| 413 | # if we are doing subcollections, then some docs shouldn't be indexed.
|
---|
| 414 | # but we need to put the section tag placeholders in there so the
|
---|
| 415 | # sections match up with database
|
---|
| 416 | my $indexed_section = $doc_obj->get_metadata_element($section, "gsdldoctype") || "indexed_section";
|
---|
| 417 | if (($indexed_doc == 0) || ($indexed_section ne "indexed_section" && $indexed_section ne "indexed_doc")) {
|
---|
| 418 | if ($sec_tag_name ne "") {
|
---|
| 419 | $text .= $start_sec;
|
---|
| 420 | $text .= $end_sec;
|
---|
| 421 | }
|
---|
| 422 | $section = $doc_obj->get_next_section($section);
|
---|
| 423 | next;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | # add in start section tag if indexing at the section level
|
---|
| 427 | $text .= $start_sec if ($sec_tag_name ne "");
|
---|
| 428 |
|
---|
| 429 | if ($edit_mode eq "add") {
|
---|
| 430 | $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
|
---|
| 431 | }
|
---|
| 432 | elsif ($edit_mode eq "delete") {
|
---|
| 433 | $self->{'num_bytes'} -= $doc_obj->get_text_length ($section);
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 |
|
---|
| 437 | # has the user added a 'metadata' index?
|
---|
| 438 | my $all_metadata_specified = 0;
|
---|
| 439 | # which fields have already been indexed? (same as fields, but in a map)
|
---|
| 440 | my $specified_fields = {};
|
---|
| 441 |
|
---|
| 442 | # do we have an allfields index??
|
---|
| 443 | my $allfields_index = 0;
|
---|
| 444 | # collect up all the text for it in here
|
---|
| 445 | my $allfields_text = "";
|
---|
| 446 | foreach my $field (split (/;/, $fields)) {
|
---|
| 447 | if ($field eq "allfields") {
|
---|
| 448 | $allfields_index = 1;
|
---|
| 449 | } elsif ($field eq "metadata") {
|
---|
| 450 | $all_metadata_specified = 1;
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | foreach my $field (split (/;/, $fields)) {
|
---|
| 455 |
|
---|
| 456 | # only deal with this field if it doesn't start with top or
|
---|
| 457 | # this is the first section
|
---|
| 458 | my $real_field = $field;
|
---|
| 459 | next if (($real_field =~ s/^top//) && ($doc_section != 1));
|
---|
| 460 |
|
---|
| 461 | # process these two later
|
---|
| 462 | next if ($real_field eq "allfields" || $real_field eq "metadata");
|
---|
| 463 |
|
---|
| 464 | #individual metadata and or text specified - could be a comma separated list
|
---|
| 465 | $specified_fields->{$real_field} = 1;
|
---|
| 466 | my $shortname="";
|
---|
| 467 | my $new_field = 0; # have we found a new field name?
|
---|
| 468 | if (defined $self->{'indexfieldmap'}->{$real_field}) {
|
---|
| 469 | $shortname = $self->{'indexfieldmap'}->{$real_field};
|
---|
| 470 | }
|
---|
| 471 | else {
|
---|
[33371] | 472 | $shortname = $self->get_or_create_shortname($real_field);
|
---|
[24446] | 473 | $new_field = 1;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | my @metadata_list = (); # put any metadata values in here
|
---|
| 477 | my $section_text = ""; # put the text in here
|
---|
| 478 | foreach my $submeta (split /,/, $real_field) {
|
---|
| 479 | if ($submeta eq "text") {
|
---|
| 480 | # no point in indexing text more than once
|
---|
| 481 | if ($section_text eq "") {
|
---|
| 482 | $section_text = $doc_obj->get_text($section);
|
---|
| 483 | if ($self->{'indexing_text'}) {
|
---|
| 484 | # we always strip html
|
---|
[30050] | 485 | &ghtml::htmlsafe($section_text);
|
---|
| 486 | #$section_text = $self->preprocess_text($section_text, 1, "");
|
---|
[24446] | 487 | }
|
---|
| 488 | else {
|
---|
| 489 | # leave html stuff in, but escape the tags
|
---|
| 490 | &ghtml::htmlsafe($section_text);
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 | else {
|
---|
| 495 | $submeta =~ s/^ex\.//; #strip off ex.
|
---|
| 496 |
|
---|
| 497 | # its a metadata element
|
---|
| 498 | my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
|
---|
| 499 | if ($section ne $doc_obj->get_top_section() && $self->{'indexing_text'} && defined ($self->{'sections_index_document_metadata'})) {
|
---|
| 500 | if ($self->{'sections_index_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_index_document_metadata'} eq "unless_section_metadata_exists")) {
|
---|
| 501 | push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)});
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 | push (@metadata_list, @section_metadata);
|
---|
| 505 | }
|
---|
| 506 | } # for each field in this one index
|
---|
| 507 |
|
---|
| 508 | # now we add the text and/or metadata into new_text
|
---|
| 509 | if ($section_text ne "" || scalar(@metadata_list)) {
|
---|
| 510 | my $new_text = "";
|
---|
| 511 |
|
---|
| 512 | if ($section_text ne "") {
|
---|
[33327] | 513 |
|
---|
| 514 | if ($allfields_index) {
|
---|
| 515 | $allfields_text .= "$section_text ";
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | # Remove any leading or trailing white space
|
---|
| 519 | $section_text =~ s/\s+$//;
|
---|
| 520 | $section_text =~ s/^\s+//;
|
---|
| 521 |
|
---|
| 522 | if ($self->{'indexing_text'}) {
|
---|
| 523 | # add the tag
|
---|
| 524 | $new_text .= "<field name=\"$shortname\" >$section_text</field>\n";
|
---|
| 525 | } else {
|
---|
| 526 | $new_text .= "$section_text ";
|
---|
| 527 | }
|
---|
[24446] | 528 | }
|
---|
| 529 |
|
---|
| 530 | foreach my $item (@metadata_list) {
|
---|
| 531 | &ghtml::htmlsafe($item);
|
---|
| 532 |
|
---|
[33327] | 533 | if ($allfields_index) {
|
---|
| 534 | $allfields_text .= "$item ";
|
---|
| 535 | }
|
---|
[24446] | 536 |
|
---|
[33327] | 537 | # Remove any leading or trailing white space
|
---|
| 538 | $item =~ s/\s+$//;
|
---|
| 539 | $item =~ s/^\s+//;
|
---|
| 540 |
|
---|
| 541 | if ($self->{'indexing_text'}) {
|
---|
| 542 | # add the tag
|
---|
| 543 | $new_text .= "<field name=\"$shortname\" >$item</field>\n";
|
---|
| 544 | } else {
|
---|
| 545 | $new_text .= "$item ";
|
---|
| 546 | }
|
---|
| 547 | } # end for loop processing @metadata_list
|
---|
[25846] | 548 |
|
---|
[24446] | 549 | # filter the text
|
---|
| 550 | $new_text = $self->filter_text ($field, $new_text);
|
---|
| 551 |
|
---|
| 552 | if ($edit_mode eq "add") {
|
---|
| 553 | $self->{'num_processed_bytes'} += length ($new_text);
|
---|
| 554 | $text .= "$new_text";
|
---|
| 555 | }
|
---|
| 556 | elsif ($edit_mode eq "update") {
|
---|
| 557 | $text .= "$new_text";
|
---|
| 558 | }
|
---|
| 559 | elsif ($edit_mode eq "delete") {
|
---|
| 560 | $self->{'num_processed_bytes'} -= length ($new_text);
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 |
|
---|
| 564 | if ($self->{'indexing_text'} && $new_field) {
|
---|
| 565 | # we need to add to the list in indexfields
|
---|
| 566 |
|
---|
| 567 | $self->{'indexfieldmap'}->{$real_field} = $shortname;
|
---|
| 568 | $self->{'indexfieldmap'}->{$shortname} = 1;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | } # foreach field
|
---|
| 574 |
|
---|
| 575 |
|
---|
| 576 | if ($all_metadata_specified) {
|
---|
| 577 |
|
---|
| 578 | my $new_text = "";
|
---|
| 579 | my $shortname = "";
|
---|
| 580 | my $metadata = $doc_obj->get_all_metadata ($section);
|
---|
| 581 | foreach my $pair (@$metadata) {
|
---|
| 582 | my ($mfield, $mvalue) = (@$pair);
|
---|
| 583 |
|
---|
| 584 | # no value
|
---|
| 585 | next unless defined $mvalue && $mvalue ne "";
|
---|
| 586 |
|
---|
| 587 | # we have already indexed this
|
---|
| 588 | next if defined ($specified_fields->{$mfield});
|
---|
| 589 |
|
---|
| 590 | # check fields here, maybe others dont want - change to use dontindex!!
|
---|
| 591 | next if ($mfield eq "Identifier" || $mfield eq "classifytype" || $mfield eq "assocfilepath");
|
---|
| 592 | next if ($mfield =~ /^gsdl/);
|
---|
| 593 |
|
---|
| 594 | &ghtml::htmlsafe($mvalue);
|
---|
| 595 |
|
---|
| 596 | if (defined $self->{'indexfieldmap'}->{$mfield}) {
|
---|
| 597 | $shortname = $self->{'indexfieldmap'}->{$mfield};
|
---|
| 598 | }
|
---|
| 599 | else {
|
---|
[33371] | 600 | $shortname = $self->get_or_create_shortname($mfield);
|
---|
[24446] | 601 | $self->{'indexfieldmap'}->{$mfield} = $shortname;
|
---|
| 602 | $self->{'indexfieldmap'}->{$shortname} = 1;
|
---|
| 603 | }
|
---|
| 604 | $new_text .= "<field name=\"$shortname\">$mvalue</field>\n";
|
---|
| 605 | if ($allfields_index) {
|
---|
| 606 | $allfields_text .= "$mvalue ";
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | if (!defined $self->{'indexfields'}->{$mfield}) {
|
---|
| 610 | $self->{'indexfields'}->{$mfield} = 1;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | }
|
---|
| 614 | # filter the text
|
---|
| 615 | $new_text = $self->filter_text ("metadata", $new_text);
|
---|
| 616 |
|
---|
| 617 | if ($edit_mode eq "add") {
|
---|
| 618 | $self->{'num_processed_bytes'} += length ($new_text);
|
---|
| 619 | $text .= "$new_text";
|
---|
| 620 | }
|
---|
| 621 | elsif ($edit_mode eq "update") {
|
---|
| 622 | $text .= "$new_text";
|
---|
| 623 | }
|
---|
| 624 | elsif ($edit_mode eq "delete") {
|
---|
| 625 | $self->{'num_processed_bytes'} -= length ($new_text);
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | if ($allfields_index) {
|
---|
| 630 | # add the index name mapping
|
---|
| 631 | $self->{'indexfieldmap'}->{"allfields"} = "ZZ";
|
---|
| 632 | $self->{'indexfieldmap'}->{"ZZ"} = 1;
|
---|
| 633 |
|
---|
| 634 | my $new_text = "<field name=\"ZZ\">$allfields_text</field>\n";
|
---|
| 635 | # filter the text
|
---|
| 636 | $new_text = $self->filter_text ("allfields", $new_text);
|
---|
| 637 |
|
---|
| 638 | if ($edit_mode eq "add") {
|
---|
| 639 | $self->{'num_processed_bytes'} += length ($new_text);
|
---|
| 640 | $text .= "$new_text";
|
---|
| 641 | }
|
---|
| 642 | elsif ($edit_mode eq "update") {
|
---|
| 643 | $text .= "$new_text";
|
---|
| 644 | }
|
---|
| 645 | elsif ($edit_mode eq "delete") {
|
---|
| 646 | $self->{'num_processed_bytes'} -= length ($new_text);
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
| 649 |
|
---|
[33371] | 650 | # only add sort and facet fields for this section if we are indexing this section, we are doing section level indexing or this is the top section
|
---|
[27802] | 651 | if ($self->{'indexing_text'} && ($sec_tag_name ne "" || $doc_section == 1 )) {
|
---|
| 652 | # add sort fields if there are any
|
---|
[33371] | 653 | foreach my $sfield (@{$self->{'sortfields'}}) {
|
---|
[28062] | 654 | # ignore special field rank/none
|
---|
| 655 | next if $sfield eq "rank" || $sfield eq "none";
|
---|
[33371] | 656 | my $sf_shortname = $self->get_or_create_sortfield_shortname($sfield);
|
---|
[27802] | 657 | my @metadata_list = (); # put any metadata values in here
|
---|
| 658 | foreach my $submeta (split /,/, $sfield) {
|
---|
| 659 | $submeta =~ s/^ex\.([^.]+)$/$1/; #strip off ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
|
---|
| 660 |
|
---|
| 661 | my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
|
---|
| 662 | if ($section ne $doc_obj->get_top_section() && defined ($self->{'sections_sort_on_document_metadata'})) {
|
---|
| 663 | if ($self->{'sections_sort_on_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_sort_on_document_metadata'} eq "unless_section_metadata_exists")) {
|
---|
| 664 | push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)});
|
---|
| 665 | }
|
---|
| 666 | }
|
---|
| 667 | push (@metadata_list, @section_metadata);
|
---|
| 668 | }
|
---|
[33371] | 669 | my $new_text = "";
|
---|
| 670 | foreach my $item (@metadata_list) {
|
---|
| 671 | &ghtml::htmlsafe($item);
|
---|
| 672 | $new_text .= "$item ";
|
---|
| 673 | }
|
---|
| 674 | if ($new_text =~ /\S/) {
|
---|
| 675 | $new_text = "<field name=\"$sf_shortname\">$new_text</field>\n";
|
---|
| 676 | # filter the text???
|
---|
| 677 | $text .= "$new_text"; # add it to the main text block
|
---|
| 678 | #print "#### new_text: $new_text\n";
|
---|
[32098] | 679 |
|
---|
[33371] | 680 | $self->{'actualsortfields'}->{$sfield} = 1;
|
---|
| 681 | }
|
---|
| 682 | # print "#### TEXT: $text\n";
|
---|
| 683 |
|
---|
[32098] | 684 |
|
---|
[33371] | 685 | }# end add in sort fields
|
---|
| 686 |
|
---|
| 687 | # add facet fields if there are any
|
---|
| 688 | foreach my $sfield (@{$self->{'facetfields'}}) {
|
---|
| 689 | my $sf_shortname = $self->get_or_create_facetfield_shortname($sfield);
|
---|
| 690 | my @metadata_list = (); # put any metadata values in here
|
---|
| 691 | foreach my $submeta (split /,/, $sfield) {
|
---|
| 692 | $submeta =~ s/^ex\.([^.]+)$/$1/; #strip off ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
|
---|
| 693 |
|
---|
| 694 | my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
|
---|
| 695 | if ($section ne $doc_obj->get_top_section() && defined ($self->{'sections_sort_on_document_metadata'})) {
|
---|
| 696 | if ($self->{'sections_sort_on_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_sort_on_document_metadata'} eq "unless_section_metadata_exists")) {
|
---|
| 697 | push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)});
|
---|
[33327] | 698 | }
|
---|
[33371] | 699 | }
|
---|
| 700 | push (@metadata_list, @section_metadata);
|
---|
[27802] | 701 | }
|
---|
[33371] | 702 | my $found_facet_value = 0;
|
---|
| 703 | foreach my $item (@metadata_list) {
|
---|
| 704 | &ghtml::htmlsafe($item);
|
---|
| 705 | if ($item =~ /\S/) {
|
---|
| 706 | $item = "<field name=\"$sf_shortname\">$item</field>\n";
|
---|
| 707 | # filter the text???
|
---|
| 708 | $text .= "$item"; # add it to the main text block
|
---|
| 709 | $found_facet_value = 1;
|
---|
| 710 | #print "#### new_text: $item\n";
|
---|
| 711 | }
|
---|
[32098] | 712 | }
|
---|
[33371] | 713 | if($found_facet_value) {
|
---|
| 714 | $self->{'actualfacetfields'}->{$sfield} = 1;
|
---|
| 715 | }
|
---|
[32098] | 716 |
|
---|
[27802] | 717 | }
|
---|
[33371] | 718 | } # end add in facet fields
|
---|
[27802] | 719 |
|
---|
[24446] | 720 | # add in end tag if at top-level doc root, or indexing at the section level
|
---|
| 721 | $text .= $end_sec if ($sec_tag_name ne "");
|
---|
| 722 |
|
---|
| 723 | $section = $doc_obj->get_next_section($section);
|
---|
| 724 | } # while defined section
|
---|
| 725 |
|
---|
| 726 |
|
---|
| 727 | # only output if working with doc level
|
---|
| 728 | $text .= $end_doc if ($sec_tag_name eq "");
|
---|
| 729 |
|
---|
| 730 | ## $text .= "<commit/>\n";
|
---|
| 731 |
|
---|
[28127] | 732 | # The following code looks like it's for debugging purposes, but
|
---|
| 733 | # committed by accident. Commenting out for now ...
|
---|
| 734 |
|
---|
| 735 | # open(TEXTOUT, '>:utf8', "text.out");
|
---|
| 736 | # print TEXTOUT "$text";
|
---|
| 737 | # close TEXTOUT;
|
---|
| 738 |
|
---|
[24446] | 739 | print $solrhandle $text;
|
---|
| 740 |
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 |
|
---|
| 744 |
|
---|
| 745 |
|
---|
| 746 | sub textreindex
|
---|
| 747 | {
|
---|
| 748 | my $self = shift (@_);
|
---|
| 749 | my ($doc_obj,$file) = @_;
|
---|
| 750 |
|
---|
[29945] | 751 | # the update command does not exist in solrbuildproc
|
---|
| 752 | # reindexing consists of deleting and then adding the same file
|
---|
| 753 | #$self->textedit($doc_obj,$file,"update");
|
---|
| 754 | $self->textedit($doc_obj,$file,"delete");
|
---|
| 755 | $self->textedit($doc_obj,$file,"add");
|
---|
[24446] | 756 | }
|
---|
| 757 |
|
---|
[33371] | 758 | sub get_or_create_facetfield_shortname {
|
---|
| 759 | my $self = shift(@_);
|
---|
[24446] | 760 |
|
---|
[33371] | 761 | my ($realname) = @_;
|
---|
| 762 |
|
---|
| 763 | if (defined $self->{'facetfieldnamemap'}->{$realname}) {
|
---|
| 764 | return $self->{'facetfieldnamemap'}->{$realname};
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | # get the shortname made for this index field
|
---|
| 768 |
|
---|
| 769 | my $shortname = $self->get_or_create_shortname($realname);
|
---|
| 770 | $shortname = "fc".$shortname;
|
---|
| 771 |
|
---|
| 772 | $self->{'facetfieldnamemap'}->{$realname} = $shortname;
|
---|
| 773 | $self->{'facetfieldnamemap'}->{$shortname} = 1;
|
---|
| 774 | return $shortname;
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 |
|
---|
| 778 |
|
---|
[24446] | 779 | 1;
|
---|
| 780 |
|
---|
| 781 |
|
---|