My unique key field is an MD5 hash of several other fields that represent identity of documents in my index. We've been calculating this externally and setting the key value in documents but have found recurring bugs as the number and variety of inserting consumers has grown...
So I wanted to move to calculating these at "add" time. We already have our own UpdateHandler, extending from DirectUpdateHandler, so I extended its addDoc method to do the hashing and field setting. Here's the implementation highlights: String postGuid = <MD5 Hash of several fields concatenated> // set the value - overwrite if already present { SolrInputField postGuidField = doc.getField(POST_GUID_NAME); if (postGuidField != null) { postGuidField.setValue(postGuid, DEFAULT_BOOST); } else { doc.addField(POST_GUID_NAME, postGuid); } } { // add guid field to the lucene doc too - huh. Document lucDoc = cmd.getLuceneDocument(schema); Field aiPostGuidField = lucDoc.getField(POST_GUID_NAME); if (aiPostGuidField != null) { aiPostGuidField.setValue(postGuid); } else { SchemaField aiPostGuidSchemaField = schema.getField(POST_GUID_NAME); Field postGuidField = aiPostGuidSchemaField.createField(postGuid, DEFAULT_BOOST); lucDoc.add(postGuidField); } } Question1: Is this the best place to do this? Question2: Is there a way around adding it to both the SolrDocument and the Lucene Document? Thoughts? Best regards, Jim -- View this message in context: http://www.nabble.com/Calculated-Unique-Key-Field-tp19747955p19747955.html Sent from the Solr - User mailing list archive at Nabble.com.