SOLR-1270: make sure that plain numeric types produce correct transfer syntax for JSON, Python, Ruby, and other TextWriter fields

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@800966 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2009-08-04 20:51:12 +00:00
parent f939400f13
commit e918778c48
5 changed files with 96 additions and 28 deletions

View File

@ -28,7 +28,7 @@ import java.io.IOException;
import java.util.Map;
/**
* @version $Id: LongField.java 555343 2007-07-11 17:46:25Z hossman $
* @version $Id:$
*/
public class ByteField extends FieldType {
protected void init(IndexSchema schema, Map<String, String> args) {
@ -50,7 +50,27 @@ public class ByteField extends FieldType {
}
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
writer.writeByte(name, f.stringValue());
String s = f.stringValue();
// these values may be from a legacy lucene index, which may
// not be properly formatted in some output formats, or may
// incorrectly have a zero length.
if (s.length()==0) {
// zero length value means someone mistakenly indexed the value
// instead of simply leaving it out. Write a null value instead of a numeric.
writer.writeNull(name);
return;
}
try {
byte val = Byte.parseByte(s);
writer.writeByte(name, val);
} catch (NumberFormatException e){
// can't parse - write out the contents as a string so nothing is lost and
// clients don't get a parse error.
writer.writeStr(name, s, true);
}
}
@Override

View File

@ -50,7 +50,27 @@ public class DoubleField extends FieldType {
}
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
writer.writeDouble(name, f.stringValue());
String s = f.stringValue();
// these values may be from a legacy lucene index, which may
// not be properly formatted in some output formats, or may
// incorrectly have a zero length.
if (s.length()==0) {
// zero length value means someone mistakenly indexed the value
// instead of simply leaving it out. Write a null value instead of a numeric.
writer.writeNull(name);
return;
}
try {
double val = Double.parseDouble(s);
writer.writeDouble(name, val);
} catch (NumberFormatException e){
// can't parse - write out the contents as a string so nothing is lost and
// clients don't get a parse error.
writer.writeStr(name, s, true);
}
}

View File

@ -48,22 +48,26 @@ public class IntField extends FieldType {
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
String s = f.stringValue();
int len = s.length();
// these values may be from a legacy lucene index, which may contain
// integer values padded with zeros, or a zero length value.
if (len>=2) {
char ch = s.charAt(0);
if ((ch=='0') || (ch=='-' && s.charAt(1)=='0')) {
s = Integer.toString(Integer.parseInt(s));
}
} else if (len == 0) {
// these values may be from a legacy lucene index, which may
// not be properly formatted in some output formats, or may
// incorrectly have a zero length.
if (s.length()==0) {
// zero length value means someone mistakenly indexed the value
// instead of simply leaving it out. Write a null value instead
// of an integer value in this case.
// instead of simply leaving it out. Write a null value instead of a numeric.
writer.writeNull(name);
return;
}
writer.writeInt(name, s);
try {
int val = Integer.parseInt(s);
writer.writeInt(name, val);
} catch (NumberFormatException e){
// can't parse - write out the contents as a string so nothing is lost and
// clients don't get a parse error.
writer.writeStr(name, s, true);
}
}
@Override

View File

@ -55,22 +55,26 @@ public class LongField extends FieldType {
@Override
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
String s = f.stringValue();
int len = s.length();
// these values may be from a legacy lucene index, which may contain
// integer values padded with zeros, or a zero length value.
if (len>=2) {
char ch = s.charAt(0);
if ((ch=='0') || (ch=='-' && s.charAt(1)=='0')) {
s = Long.toString(Long.parseLong(s));
}
} else if (len == 0) {
// these values may be from a legacy lucene index, which may
// not be properly formatted in some output formats, or may
// incorrectly have a zero length.
if (s.length()==0) {
// zero length value means someone mistakenly indexed the value
// instead of simply leaving it out. Write a null value instead
// of an integer value in this case.
// instead of simply leaving it out. Write a null value instead of a numeric.
writer.writeNull(name);
return;
}
writer.writeLong(name, s);
try {
long val = Long.parseLong(s);
writer.writeLong(name, val);
} catch (NumberFormatException e){
// can't parse - write out the contents as a string so nothing is lost and
// clients don't get a parse error.
writer.writeStr(name, s, true);
}
}
@Override

View File

@ -56,7 +56,27 @@ public class ShortField extends FieldType {
@Override
public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {
writer.writeShort(name, f.stringValue());
String s = f.stringValue();
// these values may be from a legacy lucene index, which may
// not be properly formatted in some output formats, or may
// incorrectly have a zero length.
if (s.length()==0) {
// zero length value means someone mistakenly indexed the value
// instead of simply leaving it out. Write a null value instead of a numeric.
writer.writeNull(name);
return;
}
try {
short val = Short.parseShort(s);
writer.writeShort(name, val);
} catch (NumberFormatException e){
// can't parse - write out the contents as a string so nothing is lost and
// clients don't get a parse error.
writer.writeStr(name, s, true);
}
}
@Override