mirror of https://github.com/apache/lucene.git
SOLR-2402 -- more verbose errors from DocumentBuilder
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1078928 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b5dbbf5e29
commit
16b1b1d6c9
|
@ -206,6 +206,15 @@ public class DocumentBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
private static String getID( SolrInputDocument doc, IndexSchema schema )
|
||||
{
|
||||
String id = "";
|
||||
SchemaField sf = schema.getUniqueKeyField();
|
||||
if( sf != null ) {
|
||||
id = "[doc="+doc.getFieldValue( sf.getName() )+"] ";
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a SolrInputDocument to a lucene Document.
|
||||
|
@ -235,91 +244,93 @@ public class DocumentBuilder {
|
|||
|
||||
// Make sure it has the correct number
|
||||
if( sfield!=null && !sfield.multiValued() && field.getValueCount() > 1 ) {
|
||||
String id = "";
|
||||
SchemaField sf = schema.getUniqueKeyField();
|
||||
if( sf != null ) {
|
||||
id = "["+doc.getFieldValue( sf.getName() )+"] ";
|
||||
}
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
|
||||
"ERROR: "+id+"multiple values encountered for non multiValued field " +
|
||||
"ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued field " +
|
||||
sfield.getName() + ": " +field.getValue() );
|
||||
}
|
||||
|
||||
|
||||
// load each field value
|
||||
boolean hasField = false;
|
||||
for( Object v : field ) {
|
||||
if( v == null ) {
|
||||
continue;
|
||||
}
|
||||
String val = null;
|
||||
hasField = true;
|
||||
boolean isBinaryField = false;
|
||||
if (sfield != null && sfield.getType() instanceof BinaryField) {
|
||||
isBinaryField = true;
|
||||
BinaryField binaryField = (BinaryField) sfield.getType();
|
||||
Fieldable f = binaryField.createField(sfield,v,boost);
|
||||
if(f != null){
|
||||
out.add(f);
|
||||
try {
|
||||
for( Object v : field ) {
|
||||
if( v == null ) {
|
||||
continue;
|
||||
}
|
||||
used = true;
|
||||
} else {
|
||||
// TODO!!! HACK -- date conversion
|
||||
if (sfield != null && v instanceof Date && sfield.getType() instanceof DateField) {
|
||||
DateField df = (DateField) sfield.getType();
|
||||
val = df.toInternal((Date) v) + 'Z';
|
||||
} else if (v != null) {
|
||||
val = v.toString();
|
||||
}
|
||||
|
||||
if (sfield != null) {
|
||||
used = true;
|
||||
addField(out, sfield, val, boost);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we should copy this field to any other fields.
|
||||
// This could happen whether it is explicit or not.
|
||||
List<CopyField> copyFields = schema.getCopyFieldsList(name);
|
||||
for (CopyField cf : copyFields) {
|
||||
SchemaField destinationField = cf.getDestination();
|
||||
// check if the copy field is a multivalued or not
|
||||
if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
|
||||
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||
"ERROR: multiple values encountered for non multiValued copy field " +
|
||||
destinationField.getName() + ": " + val);
|
||||
}
|
||||
|
||||
used = true;
|
||||
//Don't worry about poly fields here
|
||||
Fieldable [] fields = null;
|
||||
if (isBinaryField) {
|
||||
if (destinationField.getType() instanceof BinaryField) {
|
||||
BinaryField binaryField = (BinaryField) destinationField.getType();
|
||||
//TODO: safe to assume that binary fields only create one?
|
||||
fields = new Fieldable[]{binaryField.createField(destinationField, v, boost)};
|
||||
}
|
||||
} else {
|
||||
fields = destinationField.createFields(cf.getLimitedValue(val), boost);
|
||||
}
|
||||
if (fields != null) { // null fields are not added
|
||||
for (Fieldable f : fields) {
|
||||
String val = null;
|
||||
hasField = true;
|
||||
boolean isBinaryField = false;
|
||||
if (sfield != null && sfield.getType() instanceof BinaryField) {
|
||||
isBinaryField = true;
|
||||
BinaryField binaryField = (BinaryField) sfield.getType();
|
||||
Fieldable f = binaryField.createField(sfield,v,boost);
|
||||
if(f != null){
|
||||
out.add(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
used = true;
|
||||
} else {
|
||||
// TODO!!! HACK -- date conversion
|
||||
if (sfield != null && v instanceof Date && sfield.getType() instanceof DateField) {
|
||||
DateField df = (DateField) sfield.getType();
|
||||
val = df.toInternal((Date) v) + 'Z';
|
||||
} else if (v != null) {
|
||||
val = v.toString();
|
||||
}
|
||||
|
||||
// In lucene, the boost for a given field is the product of the
|
||||
// document boost and *all* boosts on values of that field.
|
||||
// For multi-valued fields, we only want to set the boost on the
|
||||
// first field.
|
||||
boost = 1.0f;
|
||||
if (sfield != null) {
|
||||
used = true;
|
||||
addField(out, sfield, val, boost);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we should copy this field to any other fields.
|
||||
// This could happen whether it is explicit or not.
|
||||
List<CopyField> copyFields = schema.getCopyFieldsList(name);
|
||||
for (CopyField cf : copyFields) {
|
||||
SchemaField destinationField = cf.getDestination();
|
||||
// check if the copy field is a multivalued or not
|
||||
if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
|
||||
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||
"ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued copy field " +
|
||||
destinationField.getName() + ": " + val);
|
||||
}
|
||||
|
||||
used = true;
|
||||
//Don't worry about poly fields here
|
||||
Fieldable [] fields = null;
|
||||
if (isBinaryField) {
|
||||
if (destinationField.getType() instanceof BinaryField) {
|
||||
BinaryField binaryField = (BinaryField) destinationField.getType();
|
||||
//TODO: safe to assume that binary fields only create one?
|
||||
fields = new Fieldable[]{binaryField.createField(destinationField, v, boost)};
|
||||
}
|
||||
} else {
|
||||
fields = destinationField.createFields(cf.getLimitedValue(val), boost);
|
||||
}
|
||||
if (fields != null) { // null fields are not added
|
||||
for (Fieldable f : fields) {
|
||||
out.add(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// In lucene, the boost for a given field is the product of the
|
||||
// document boost and *all* boosts on values of that field.
|
||||
// For multi-valued fields, we only want to set the boost on the
|
||||
// first field.
|
||||
boost = 1.0f;
|
||||
}
|
||||
}
|
||||
catch( Exception ex ) {
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
|
||||
"ERROR: "+getID(doc, schema)+"Error adding field '" +
|
||||
field.getName() + "'='" +field.getValue()+"'", ex );
|
||||
}
|
||||
|
||||
// make sure the field was used somehow...
|
||||
if( !used && hasField ) {
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"ERROR:unknown field '" +
|
||||
name + "'");
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
|
||||
"ERROR: "+getID(doc, schema)+"unknown field '" +name + "'");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -332,8 +343,7 @@ public class DocumentBuilder {
|
|||
addField(out, field, field.getDefaultValue(), 1.0f);
|
||||
}
|
||||
else {
|
||||
String id = schema.printableUniqueKey( out );
|
||||
String msg = "Document ["+id+"] missing required field: " + field.getName();
|
||||
String msg = getID(doc, schema) + "missing required field: " + field.getName();
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, msg );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,6 +66,40 @@ public class DocumentBuilderTest extends SolrTestCaseJ4 {
|
|||
assertNull( out.get( "name" ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptions()
|
||||
{
|
||||
SolrCore core = h.getCore();
|
||||
|
||||
// make sure a null value is not indexed
|
||||
SolrInputDocument doc = new SolrInputDocument();
|
||||
doc.addField( "id", "123", 1.0f );
|
||||
doc.addField( "unknown", "something", 1.0f );
|
||||
try {
|
||||
DocumentBuilder.toDocument( doc, core.getSchema() );
|
||||
fail( "added an unknown field" );
|
||||
}
|
||||
catch( Exception ex ) {
|
||||
assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
|
||||
}
|
||||
doc.remove( "unknown" );
|
||||
|
||||
|
||||
doc.addField( "weight", "not a number", 1.0f );
|
||||
try {
|
||||
DocumentBuilder.toDocument( doc, core.getSchema() );
|
||||
fail( "invalid 'float' field value" );
|
||||
}
|
||||
catch( Exception ex ) {
|
||||
assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
|
||||
assertTrue( "cause is number format", ex.getCause() instanceof NumberFormatException );
|
||||
}
|
||||
|
||||
// now make sure it is OK
|
||||
doc.setField( "weight", "1.34", 1.0f );
|
||||
DocumentBuilder.toDocument( doc, core.getSchema() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiField() throws Exception {
|
||||
SolrCore core = h.getCore();
|
||||
|
|
Loading…
Reference in New Issue