You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-dev@lucene.apache.org by Darren Vengroff <ve...@gmail.com> on 2006/06/08 22:09:05 UTC

Logging a lack of uniqueKey field in schema.xml

I just ran across a situation where, in building my schema.xml file, I
forgot to specify the <field /> for the field that I was using as my
<uniqueKey />.  It was totally my error, but having no idea what the error
was, I had to spend some time debugging.  Once I realized what was going on,
I made a little change to IndexSchema.java to ensure that something appears
in the log in this situation.  The previous behavior was to simply throw a
RuntimeException, which, in Tomcat at least, did not result in anything easy
to spot in any of the logs I checked.  The patch for this is below if you
want to apply it.  I know it would have saved me some time today.

Note also that the code that looks for a <defaultSearchField /> already
assumed that getIndexedField(String) could return a null, although that code
path was unreachable given the throw that this patch removed.

It makes me think that having an XML Schema for schema.xml (there's a
palindrome for you) would be a nice idea to.

Thanks,

-D


Index: C:/Documents and Settings/Darren
Vengroff/workspace/Solr/src/java/org/apache/solr/schema/IndexSchema.java
===================================================================
--- C:/Documents and Settings/Darren
Vengroff/workspace/Solr/src/java/org/apache/solr/schema/IndexSchema.java
(revision 412846)
+++ C:/Documents and Settings/Darren
Vengroff/workspace/Solr/src/java/org/apache/solr/schema/IndexSchema.java
(working copy)
@@ -178,14 +178,7 @@
   }
 
   private SchemaField getIndexedField(String fname) {
-    SchemaField f = getFields().get(fname);
-    if (f==null) {
-      throw new RuntimeException("unknown field '" + fname + "'");
-    }
-    if (!f.indexed()) {
-      throw new RuntimeException("'"+fname+"' is not an indexed field:" +
f);
-    }
-    return f;
+    return getFields().get(fname);
   }
 
 
@@ -369,10 +362,15 @@
     if (node==null) {
       log.warning("no uniqueKey specified in schema.");
     } else {
-      uniqueKeyField=getIndexedField(node.getNodeValue().trim());
-      uniqueKeyFieldName=uniqueKeyField.getName();
-      uniqueKeyFieldType=uniqueKeyField.getType();
-      log.info("unique key field: "+uniqueKeyFieldName);
+      String fieldName = node.getNodeValue().trim();
+      uniqueKeyField = getIndexedField(fieldName);
+      if(uniqueKeyField == null) {
+        log.warning("Unique key field '" + fieldName + "' undefined.");
+      } else {
+        uniqueKeyFieldName=uniqueKeyField.getName();
+        uniqueKeyFieldType=uniqueKeyField.getType();
+        log.info("unique key field: "+uniqueKeyFieldName);
+      }
     }
 
     /////////////// parse out copyField commands ///////////////



RE: Logging a lack of uniqueKey field in schema.xml

Posted by Darren Vengroff <ve...@gmail.com>.
>Must have been a late night :-)  

Must have been too late a night for me last night.  I'm now seeing the
exceptions.  Ignore that patch.

-D



Re: Logging a lack of uniqueKey field in schema.xml

Posted by Yonik Seeley <ys...@gmail.com>.
On 6/8/06, Darren Vengroff <ve...@gmail.com> wrote:
> I just ran across a situation where, in building my schema.xml file, I
> forgot to specify the <field /> for the field that I was using as my
> <uniqueKey />.  It was totally my error, but having no idea what the error
> was, I had to spend some time debugging.  Once I realized what was going on,
> I made a little change to IndexSchema.java to ensure that something appears
> in the log in this situation.  The previous behavior was to simply throw a
> RuntimeException, which, in Tomcat at least, did not result in anything easy
> to spot in any of the logs I checked.

Hmmm, I just tried removing my "id" field in Tomcat, and I did get an
exception chain ending with
Caused by: java.lang.RuntimeException: unknown field 'id'

If the schema defines a uniqueKeyField, and that field doesn't exist,
shouldn't we throw an exception rather than just logging it?  It seems
like a rather fatal error.

> Note also that the code that looks for a <defaultSearchField /> already
> assumed that getIndexedField(String) could return a null, although that code
> path was unreachable given the throw that this patch removed.

Must have been a late night :-)  I assume I meant
      defaultSearchFieldName = defName!=null ? getIndexedField(defName) : null;
rather than
      defaultSearchFieldName = getIndexedField(defName)!=null ? defName : null;


-Yonik