You can create a custom update processor. The passed AddUpdateCommand object 
has an accessor to the SolrInputDocument you're about to add. In the 
processAdd method you can add a new field with whatever you want.

The wiki has a good example:
http://wiki.apache.org/solr/UpdateRequestProcessor


> Hello,
> 
> I'm trying to add a field that counts the number of terms in a document to
> my schema. So far I've been computing this value at query-time. Is there
> how I could compute this once only and store the field?
> 
> final SolrIndexSearcher searcher = request.getSearcher();
>         final SolrIndexReader reader = searcher.getReader();
>         final String content = "content";
> 
>         final byte[] norms = reader.norms(content);
>         final int[] docLengths;
>         if (norms == null) {
>             docLengths = null;
>         } else {
>             docLengths = new int[norms.length];
>             int i = 0;
>             for (byte b : norms) {
> 
>                 float docNorm =
> searcher.getSimilarity().decodeNormValue(b); int docLength = 0;
>                 if (docNorm != 0) {
>                     docLength = (int) (1 / docNorm); //reciprocal
>                 }
>                 docLengths[i++] = docLength;
>             }
> ...
>  final NumericField docLenNormField = new
> NumericField(TestQueryResponseWriter.DOC_LENGHT);
>  docLenNormField.setIntValue(docLengths[id]);
>  doc.add(docLenNormField);

Reply via email to