Index: /main/trunk/greenstone2/perllib/lucenebuildproc.pm
===================================================================
--- /main/trunk/greenstone2/perllib/lucenebuildproc.pm	(revision 33368)
+++ /main/trunk/greenstone2/perllib/lucenebuildproc.pm	(revision 33369)
@@ -216,11 +216,5 @@
 	    my $shortname="";
 	    my $new_field = 0; # have we found a new field name?
-	    if (defined $self->{'fieldnamemap'}->{$real_field}) {
-		$shortname = $self->{'fieldnamemap'}->{$real_field};
-	    } else {
-		$shortname = $self->create_shortname($real_field);
-		$self->{'fieldnamemap'}->{$real_field} = $shortname;
-		$self->{'fieldnamemap'}->{$shortname} = 1;
-	    }
+	    $shortname = $self->get_or_create_shortname($real_field);
 	    my @metadata_list = (); # put any metadata values in here
 	    my $section_text = ""; # put the text in here
@@ -322,12 +316,5 @@
 		&ghtml::htmlsafe($mvalue);
 		
-		if (defined $self->{'fieldnamemap'}->{$mfield}) {
-		    $shortname = $self->{'fieldnamemap'}->{$mfield};
-		}
-		else {
-		    $shortname = $self->create_shortname($mfield);
-		    $self->{'fieldnamemap'}->{$mfield} = $shortname;
-		    $self->{'fieldnamemap'}->{$shortname} = 1;
-		}
+		$shortname = $self->get_or_create_shortname($mfield);
 		$self->{'allindexfields'}->{$mfield} = 1;
 		$new_text .= "<$shortname index=\"1\">$mvalue</$shortname>\n";
@@ -376,13 +363,6 @@
 	    # ignore special field rank
 	    next if ($sfield eq "rank" || $sfield eq "none");
-	    my $sf_shortname;
-	    if (defined $self->{'sortfieldnamemap'}->{$sfield}) {
-		$sf_shortname = $self->{'sortfieldnamemap'}->{$sfield};
-	    }
-	    else {
-		$sf_shortname = $self->create_sortfield_shortname($sfield);
-		$self->{'sortfieldnamemap'}->{$sfield} = $sf_shortname;
-		$self->{'sortfieldnamemap'}->{$sf_shortname} = 1;
-	    }
+	    my $sf_shortname = $self->get_or_create_sortfield_shortname($sfield);
+	 
 	    my @metadata_list = (); # put any metadata values in here
 	    foreach my $submeta (split /,/, $sfield) {
@@ -397,25 +377,14 @@
 		push (@metadata_list, @section_metadata);
 	    }
-	    # my $new_text = "";
-	    # foreach my $item (@metadata_list) {
-		# &ghtml::htmlsafe($item);
-		# $new_text .= "$item"; # should be .="$item "; But will be commenting out and rewriting this entire thing, so it doesn't matter
-	    # }
-	    # if ($new_text =~ /\S/) {
-		# $new_text = "<$sf_shortname index=\"1\" tokenize=\"0\">$new_text</$sf_shortname>";
-		# # filter the text???
-		# $text .= "$new_text"; # add it to the main text block
-		# $self->{'actualsortfields'}->{$sfield} = 1;
-	    # }
 		
-		foreach my $item (@metadata_list) {
-			&ghtml::htmlsafe($item);
-			if ($item =~ /\S/) {
-				$item = "<$sf_shortname index=\"1\" tokenize=\"0\">$item</$sf_shortname>";
-				$text .= "$item"; # add it to the main text block
-			}
-		}
-		if(scalar @metadata_list > 0) {
-			$self->{'actualsortfields'}->{$sfield} = 1;
+	    my $new_text = "";
+	    foreach my $item (@metadata_list) {
+		&ghtml::htmlsafe($item);
+		$new_text .= "$item ";
+	    }
+	    if ($new_text =~ /\S/) {
+		$new_text = "<$sf_shortname index=\"1\" tokenize=\"0\">$new_text</$sf_shortname>";
+		$text .= "$new_text"; # add it to the main text block
+		$self->{'actualsortfields'}->{$sfield} = 1;
 	    }
 	}
@@ -658,17 +627,21 @@
 }
 
-sub create_sortfield_shortname {
+sub get_or_create_sortfield_shortname {
     my $self = shift(@_);
 
     my ($realname) = @_;
-
-    my $index_shortname;
-    # if we have created a shortname for an index on this field, then use it.
-    if (defined $self->{'fieldnamemap'}->{$realname}) {
-	$index_shortname = $self->{'fieldnamemap'}->{$realname};
-    } else {
-	$index_shortname = $self->create_shortname($realname);
+    
+    if (defined $self->{'sortfieldnamemap'}->{$realname}) {
+	return $self->{'sortfieldnamemap'}->{$realname};
     }
-    return "by".$index_shortname;
+
+    # get the shortname made for this index field
+
+    my $shortname = $self->get_or_create_shortname($realname);
+    $shortname = "by".$shortname;
+
+    $self->{'sortfieldnamemap'}->{$realname} = $shortname;
+    $self->{'sortfieldnamemap'}->{$shortname} = 1;
+    return $shortname;
 }
   
Index: /main/trunk/greenstone2/perllib/mgppbuildproc.pm
===================================================================
--- /main/trunk/greenstone2/perllib/mgppbuildproc.pm	(revision 33368)
+++ /main/trunk/greenstone2/perllib/mgppbuildproc.pm	(revision 33369)
@@ -298,13 +298,7 @@
 		my $shortname="";
 
-	    if (defined $self->{'fieldnamemap'}->{$real_field}) {
-		$shortname = $self->{'fieldnamemap'}->{$real_field};
-	    } else {
-		$shortname = $self->create_shortname($real_field);
-		$self->{'fieldnamemap'}->{$real_field} = $shortname;
-		$self->{'fieldnamemap'}->{$shortname} = 1;
-	    }
-
-		my @metadata_list = (); # put any meta values in here
+	        $shortname = $self->get_or_create_shortname($real_field);
+		
+	        my @metadata_list = (); # put any meta values in here
 		my $section_text = ""; # put any text in here
 		foreach my $submeta (split /,/, $real_field) {
@@ -381,11 +375,5 @@
 		next if ($mfield =~ /^gsdl/);
 		
-		if (defined $self->{'fieldnamemap'}->{$mfield}) {
-		    $shortname = $self->{'fieldnamemap'}->{$mfield};
-		} else {
-		    $shortname = $self->create_shortname($mfield);
-		    $self->{'fieldnamemap'}->{$mfield} = $shortname;
-		    $self->{'fieldnamemap'}->{$shortname} = 1;
-		}
+		$shortname = $self->get_or_create_shortname($mfield);
 		$self->{'allindexfields'}->{$mfield} = 1;
 		$new_text .= "$paratag<$shortname>$mvalue</$shortname>\n";
@@ -414,8 +402,12 @@
 #chooses the first two letters or digits for the shortname
 #now ignores non-letdig characters
-sub create_shortname {
+sub get_or_create_shortname {
     my $self = shift(@_);
     
     my ($realname) = @_;
+    if (defined $self->{'fieldnamemap'}->{$realname}) {
+	return $self->{'fieldnamemap'}->{$realname};
+    }
+
     my @realnamelist = split(",", $realname);
     map {$_=~ s/^[a-zA-Z]+\.//;} @realnamelist; #remove namespaces
@@ -423,39 +415,40 @@
 
     # try our predefined static mapping
-    my $name;
-    if (defined ($name = $static_indexfield_map{$singlename})) {
-	if (! defined $self->{'fieldnamemap'}->{$name}) {
-	    # has this shortname already been used??
-	    return $static_indexfield_map{$singlename};
+    my $shortname;
+    if (defined ($shortname = $static_indexfield_map{$singlename}) && (! defined $self->{'fieldnamemap'}->{$shortname})) {
+	# we have a shortname from the static list, and it hasn't already been used: we can use the shortname
+    } else {
+	# we can't use the quick map, so join all fields back together (without namespaces), and try sets of two characters.
+	$realname = join ("", @realnamelist);
+	#try the first two chars
+	if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
+	    $shortname = "$1$2";
+	} else {
+	    # there aren't two letdig's in the field - try arbitrary combinations
+	    $realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+	    $shortname = "AB";
+	}
+	$shortname =~ tr/a-z/A-Z/;
+	
+	#if already used, take the first and third letdigs and so on
+	my $count = 1;
+	while (defined $self->{'fieldnamemap'}->{$shortname} || defined $static_indexfield_map{$shortname}) {
+	    if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
+		$shortname = "$1$3";
+		$count++;
+		$shortname =~ tr/a-z/A-Z/;
+		
+	    }
+	    else {
+		#remove up to and incl the first letdig
+		$realname =~ s/^[^\w]*\w//;
+		$count = 0;
+	    }
 	}
     }
-    # we can't use the quick map, so join all fields back together (without namespaces), and try sets of two characters.
-    $realname = join ("", @realnamelist);
-    #try the first two chars
-    my $shortname;
-    if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
-	$shortname = "$1$2";
-    } else {
-	# there aren't two letdig's in the field - try arbitrary combinations
-	$realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-	$shortname = "AB";
-    }
-    $shortname =~ tr/a-z/A-Z/;
-
-    #if already used, take the first and third letdigs and so on
-    my $count = 1;
-    while (defined $self->{'fieldnamemap'}->{$shortname} || defined $static_indexfield_map{$shortname}) {
-	if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
-	    $shortname = "$1$3";
-	    $count++;
-	    $shortname =~ tr/a-z/A-Z/;
-	
-	}
-	else {
-	    #remove up to and incl the first letdig
-	    $realname =~ s/^[^\w]*\w//;
-	    $count = 0;
-	}
-    }
+    # we now have a new shortname.
+    # store it in fieldnamemap
+    $self->{'fieldnamemap'}->{$realname} = $shortname;
+    $self->{'fieldnamemap'}->{$shortname} = 1;
 
     return $shortname;
