Removing some weirdness in the SolrInputDocument interface that is not really needed for SOLR-139.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@562832 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2007-08-05 05:26:53 +00:00
parent d486144782
commit fdc4ed58b4
2 changed files with 2 additions and 92 deletions

View File

@ -19,7 +19,6 @@ package org.apache.solr.common;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Collection; import java.util.Collection;
@ -27,9 +26,6 @@ import java.util.Collection;
* Represent the field and boost information needed to construct and index * Represent the field and boost information needed to construct and index
* a Lucene Document. Like the SolrDocument, the field values should * a Lucene Document. Like the SolrDocument, the field values should
* match those specified in schema.xml * match those specified in schema.xml
*
* By default, this will keep every field value added to the document. To only
* keep distinct values, use setRemoveDuplicateFieldValues( "fieldname", true );
* *
* @version $Id$ * @version $Id$
* @since solr 1.3 * @since solr 1.3
@ -37,7 +33,6 @@ import java.util.Collection;
public class SolrInputDocument implements Iterable<SolrInputField> public class SolrInputDocument implements Iterable<SolrInputField>
{ {
private final Map<String,SolrInputField> _fields; private final Map<String,SolrInputField> _fields;
private Map<String,Boolean> _removeDuplicates = null;
private float _documentBoost = 1.0f; private float _documentBoost = 1.0f;
public SolrInputDocument() public SolrInputDocument()
@ -53,27 +48,12 @@ public class SolrInputDocument implements Iterable<SolrInputField>
if( _fields != null ) { if( _fields != null ) {
_fields.clear(); _fields.clear();
} }
if(_removeDuplicates != null ) {
_removeDuplicates.clear();
}
} }
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
// Add / Set fields // Add / Set fields
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
private boolean isDistinct( String name )
{
if( _removeDuplicates != null ) {
Boolean v = _removeDuplicates.get( name );
if( v == null ) {
v = _removeDuplicates.get( null );
}
return (v == Boolean.TRUE);
}
return false;
}
/** /**
* Add a field with implied null value for boost. * Add a field with implied null value for boost.
* *
@ -137,13 +117,7 @@ public class SolrInputDocument implements Iterable<SolrInputField>
{ {
SolrInputField field = new SolrInputField( name ); SolrInputField field = new SolrInputField( name );
_fields.put( name, field ); _fields.put( name, field );
if( isDistinct( name ) ) { field.setValue( value, boost );
field.value = new LinkedHashSet<Object>();
this.addField(name, value, boost);
}
else {
field.setValue( value, boost );
}
} }
/** /**
@ -170,34 +144,6 @@ public class SolrInputDocument implements Iterable<SolrInputField>
public SolrInputField removeField(String name) { public SolrInputField removeField(String name) {
return _fields.remove( name ); return _fields.remove( name );
} }
/**
* Should the Document be able to contain duplicate values for the same field?
*
* By default, all field values are maintained. If you only want to distinct values
* set setKeepDuplicateFieldValues( "fieldname", false );
*
* To change the default behavior, use <code>null</code> as the fieldname.
*
* NOTE: this must be called before adding any values to the given field.
*/
public void setRemoveDuplicateFieldValues( String name, boolean v )
{
if( _fields.get( name ) != null ) {
// If it was not distinct and changed to distinct, we could, but this seems like a better rule
throw new RuntimeException( "You can't change a fields distinctness after it is initialized." );
}
if( _removeDuplicates == null ) {
if( v == false ) {
// we only care about 'true' we don't need to make a map unless
// something does not want multiple values
return;
}
_removeDuplicates = new HashMap<String, Boolean>();
}
_removeDuplicates.put( name, v );
}
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
// Get the field values // Get the field values

View File

@ -113,28 +113,7 @@ public class SolrDocumentTest extends TestCase
doc.addField( "v", c0 ); doc.addField( "v", c0 );
assertEquals( arr.length, doc.getFieldValues("v").size() ); assertEquals( arr.length, doc.getFieldValues("v").size() );
} }
public void testOrderedDistinctFields()
{
List<String> c0 = new ArrayList<String>();
c0.add( "aaa" );
c0.add( "bbb" );
c0.add( "aaa" );
c0.add( "aaa" );
c0.add( "ccc" );
SolrInputDocument doc = new SolrInputDocument();
doc.setRemoveDuplicateFieldValues( "f1", true );
doc.setRemoveDuplicateFieldValues( "f2", false );
doc.addField( "f1", c0, 1.0f );
doc.addField( "f2", c0, 1.0f );
assertEquals( 3, doc.getField("f1").getValueCount() );
assertEquals( 5, doc.getField("f2").getValueCount() );
assertEquals( "[aaa, bbb, ccc]", doc.getField( "f1" ).getValues().toString() );
assertEquals( "[aaa, bbb, aaa, aaa, ccc]", doc.getField( "f2" ).getValues().toString() );
}
public void testDuplicate() public void testDuplicate()
{ {
Float fval0 = new Float( 10.01f ); Float fval0 = new Float( 10.01f );
@ -149,21 +128,6 @@ public class SolrDocumentTest extends TestCase
doc.addField( "f", fval2, 1.0f ); doc.addField( "f", fval2, 1.0f );
} }
assertEquals( (3*5), doc.getField("f").getValueCount() ); assertEquals( (3*5), doc.getField("f").getValueCount() );
try {
doc.setRemoveDuplicateFieldValues( "f", true );
fail( "can't change distinct for an existing field" );
}
catch( Exception ex ) {}
doc.removeField( "f" );
doc.setRemoveDuplicateFieldValues( "f", true );
for( int i=0; i<5; i++ ) {
doc.addField( "f", fval0, 1.0f );
doc.addField( "f", fval1, 1.0f );
doc.addField( "f", fval2, 1.0f );
}
assertEquals( (3), doc.getField("f").getValueCount() );
} }
} }