/*
 * Copyright 2011 Internet Archive
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.archive.jbs.lucene;

import java.io.*;
import java.net.*;
import java.util.*;

import org.apache.lucene.document.*;
import org.apache.lucene.index.*;

import org.archive.jbs.Document;

/**
 * Handler that computes a boost value based on any number of values
 * in the input Document, then applies that boost value to the Lucene
 * doc.  The boost value is also stored in the Lucene doc for future
 * reference, such as auditing/debugging/tracking.
 */
public class BoostHandler implements FieldHandler
{

  public void handle( org.apache.lucene.document.Document luceneDocument, Document document )
  {
    long numInlinks = getLong( document, "numInlinks", 0 );

    if ( numInlinks > 10 )
      {
        float boost = (float) Math.log10( numInlinks );
        
        // Store the boost value with the document.  This can be handy
        // for debugging/auditing, but it is not searched on, nor does
        // it actually have any operational effect.  It's just so that
        // we can know later what boost value we set on the document.
        // There's no other way to tell.
        luceneDocument.add( new Field( "boost", Float.toString(boost), Field.Store.YES, Field.Index.NO ) );

        luceneDocument.setBoost( boost );
      }
  }

  public long getLong( Document document, String key, long defaultValue )
  {
    long value = defaultValue;
    try
      {
        value = Long.parseLong( document.get( key ) );
      }
    catch ( NumberFormatException nfe )
      {
        // Skip it
      }
    return value;
  }

}

@@ -812,100 +812,16 @@
 hat 
-computes a boost value based on any number of values%0A * in the input Document, then 
 applies 
 that
@@ -816,20 +816,17 @@
 applies 
-that
+a
  boost v
@@ -843,19 +843,16 @@
 e Lucene
-%0A *
  doc.  T
@@ -855,24 +855,27 @@
 .  The boost
+%0A *
  value is al
@@ -912,19 +912,16 @@
 r future
-%0A *
  referen
@@ -928,16 +928,19 @@
 ce, such
+%0A *
  as audi
@@ -1127,33 +1127,30 @@
     
+f
 lo
-ng numInlinks
+at boost
  = get
-Long
+Float
 ( do
@@ -1162,126 +1162,56 @@
 t, %22
-numInlinks%22, 0 );%0A%0A    if ( numInlinks %3E 10 )%0A      %7B%0A        float
+boost%22, 1.0F );%0A%0A    if (
  boost 
+!
 = 
-(float) Math.log10( numInlinks
+1.0
  )
-;
 %0A      
-  
+%7B
 %0A   
@@ -1708,20 +1708,22 @@
 lic 
+f
 lo
-ng
+at
  get
-Long
+Float
 ( Do
@@ -1751,20 +1751,21 @@
 ng key, 
+f
 lo
-ng
+at
  default
@@ -1784,12 +1784,13 @@
     
+f
 lo
-ng
+at
  val
@@ -1844,22 +1844,24 @@
 e = 
-Long
+Float
 .parse
-Long
+Float
 ( do

Apply the boost as specified in "boost" parameter. Ignore boost values of 1.0 as they have no effect.