Skip to content

Apache Lucene open-source search software

1. DocumentBuilder only sets the boost for the first value in a multi-valued field 2. XML parser sets the document boost as the product of each boost value. 3. Added XmlUpdateRequestHandlerTest (somehow lost in the final patch of SOLR-133)

Ryan McKinley91c3b6d535e9
diff --git a/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java b/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java
index 1f7e5318acf..ce359590f89 100644
--- a/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java
+++ b/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java
@@ -354,7 +354,17 @@ public class StaxUpdateRequestHandler extends RequestHandlerBase
if (!isNull) {
doc.addField(name, text.toString() );
if(boost != null) {
- doc.setBoost( name, boost );
+ // The lucene API and solr XML field specification make it possible to set boosts
+ // on multi-value fields even though lucene indexing does not support this.
+ // To keep behavior consistent with what happens in the lucene index, we accumulate
+ // the product of all boosts specified for this field.
+ Float old = doc.getBoost( name );
+ if( old != null ) {
+ doc.setBoost( name, boost*old );
+ }
+ else {
+ doc.setBoost( name, boost );
+ }
}
}
}
diff --git a/src/java/org/apache/solr/update/DocumentBuilder.java b/src/java/org/apache/solr/update/DocumentBuilder.java
index 3e4e6c3cd8f..a84b36e121a 100644
--- a/src/java/org/apache/solr/update/DocumentBuilder.java
+++ b/src/java/org/apache/solr/update/DocumentBuilder.java
@@ -197,6 +197,12 @@ public class DocumentBuilder {
val = v.toString();
}
out.add( sfield.createField( val, boost ) );
+
+ // 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;
}
}
diff --git a/src/test/org/apache/solr/handler/XmlUpdateRequestHandlerTest.java b/src/test/org/apache/solr/handler/XmlUpdateRequestHandlerTest.java
new file mode 100644
index 00000000000..c1bcfc2baf3
--- /dev/null
+++ b/src/test/org/apache/solr/handler/XmlUpdateRequestHandlerTest.java
@@ -0,0 +1,65 @@
+package org.apache.solr.handler;
+
+import java.io.StringReader;
+import java.util.Collection;
+
+import javanet.staxutils.BaseXMLInputFactory;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+import org.apache.solr.common.SolrInputDocument;
+
+public class XmlUpdateRequestHandlerTest extends TestCase
+{
+ private XMLInputFactory inputFactory = BaseXMLInputFactory.newInstance();
+ protected StaxUpdateRequestHandler handler = new StaxUpdateRequestHandler();
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testReadDoc() throws Exception
+ {
+ String xml =
+ "<doc boost=\"5.5\">" +
+ " <field name=\"id\" boost=\"2.2\">12345</field>" +
+ " <field name=\"name\">kitten</field>" +
+ " <field name=\"cat\" boost=\"3\">aaa</field>" +
+ " <field name=\"cat\" boost=\"4\">bbb</field>" +
+ " <field name=\"cat\" boost=\"5\">bbb</field>" +
+ " <field name=\"ab\">a&amp;b</field>" +
+ "</doc>";
+
+ XMLStreamReader parser =
+ inputFactory.createXMLStreamReader( new StringReader( xml ) );
+ parser.next(); // read the START document...
+
+ SolrInputDocument doc = handler.readDoc( parser );
+
+ // Read boosts
+ assertEquals( new Float(5.5f), doc.getBoost(null) );
+ assertEquals( null, doc.getBoost( "name" ) );
+ assertEquals( new Float(2.2f), doc.getBoost( "id" ) );
+ assertEquals( null, doc.getBoost( "ab" ) );
+ // Boost is the product of each value
+ assertEquals( new Float(3*4*5), doc.getBoost( "cat" ) );
+
+ // Read values
+ assertEquals( "12345", doc.getFieldValue( "id") );
+ assertEquals( "kitten", doc.getFieldValue( "name") );
+ assertEquals( "a&b", doc.getFieldValue( "ab") ); // read something with escaped characters
+
+ Collection<Object> out = doc.getFieldValues( "cat" );
+ assertEquals( 3, out.size() );
+ assertEquals( "[aaa, bbb, bbb]", out.toString() );
+ }
+}