* Changed setKeepDuplicateFieldValues to setRemoveDuplicateFieldValues

* made sure the tests deal with duplicate values on one field, but not the other

http://www.nabble.com/Re%3A-svn-commit%3A-r547493---in--lucene-solr-trunk%3A-.--src-java-org-apache-solr-common--src-java-org-apache-solr-schema--src-java-org-apache-solr-update--src-test-org-apache-solr-common--p11159518.html

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@548094 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2007-06-17 18:37:28 +00:00
parent 91c3b6d535
commit 045248e2c1
2 changed files with 22 additions and 18 deletions

View File

@ -29,7 +29,7 @@ import java.util.Map;
* match those specified in schema.xml
*
* By default, this will keep every field value added to the document. To only
* keep distinct values, use setKeepDuplicateFieldValues( "fieldname", false);
* keep distinct values, use setRemoveDuplicateFieldValues( "fieldname", true );
*
* @author ryan
* @version $Id$
@ -38,7 +38,7 @@ import java.util.Map;
public class SolrInputDocument extends SolrDocument
{
private Map<String,Float> _boost = null;
private Map<String,Boolean> _keepDuplicates = null;
private Map<String,Boolean> _removeDuplicates = null;
/**
* Return a base collection to manage the fields for a given value. If
@ -49,7 +49,7 @@ public class SolrInputDocument extends SolrDocument
@Override
protected Collection<Object> getEmptyCollection( String name )
{
if( _keepDuplicates == null || Boolean.TRUE == _keepDuplicates.get( name )) {
if( _removeDuplicates == null || Boolean.FALSE == _removeDuplicates.get( name )) {
return new ArrayList<Object>();
}
return new LinkedHashSet<Object>(); // keep the order? -- perhaps HashSet?
@ -65,8 +65,8 @@ public class SolrInputDocument extends SolrDocument
if( _boost != null ) {
_boost.clear();
}
if(_keepDuplicates != null ) {
_keepDuplicates.clear();
if(_removeDuplicates != null ) {
_removeDuplicates.clear();
}
}
@ -125,22 +125,22 @@ public class SolrInputDocument extends SolrDocument
*
* NOTE: this must be called before adding any values to the given field.
*/
public void setKeepDuplicateFieldValues( String name, boolean v )
public void setRemoveDuplicateFieldValues( String name, boolean v )
{
if( this.getFieldValues( 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( _keepDuplicates == null ) {
if( v == true ) {
// we only care about 'false' we don't need to make a map unless
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;
}
_keepDuplicates = new HashMap<String, Boolean>();
_removeDuplicates = new HashMap<String, Boolean>();
}
_keepDuplicates.put( name, v );
_removeDuplicates.put( name, v );
}
}

View File

@ -132,11 +132,15 @@ public class SolrDocumentTest extends TestCase
c0.add( "ccc" );
SolrInputDocument doc = new SolrInputDocument();
doc.setKeepDuplicateFieldValues( null, false );
doc.addField( "v", c0 );
assertEquals( 3, doc.getFieldValues("v").size() );
assertEquals( "[aaa, bbb, ccc]", doc.getFieldValues( "v" ).toString() );
doc.setRemoveDuplicateFieldValues( "f1", true );
doc.setRemoveDuplicateFieldValues( "f2", false );
doc.addField( "f1", c0 );
doc.addField( "f2", c0 );
assertEquals( 3, doc.getFieldValues("f1").size() );
assertEquals( 5, doc.getFieldValues("f2").size() );
assertEquals( "[aaa, bbb, ccc]", doc.getFieldValues( "f1" ).toString() );
assertEquals( "[aaa, bbb, aaa, aaa, ccc]", doc.getFieldValues( "f2" ).toString() );
}
public void testDuplicate()
@ -155,13 +159,13 @@ public class SolrDocumentTest extends TestCase
assertEquals( (3*5), doc.getFieldValues("f").size() );
try {
doc.setKeepDuplicateFieldValues( "f", false );
doc.setRemoveDuplicateFieldValues( "f", true );
fail( "can't change distinct for an existing field" );
}
catch( Exception ex ) {}
doc.removeFields( "f" );
doc.setKeepDuplicateFieldValues( "f", false );
doc.setRemoveDuplicateFieldValues( "f", true );
for( int i=0; i<5; i++ ) {
doc.addField( "f", fval0 );
doc.addField( "f", fval1 );