mirror of https://github.com/apache/lucene.git
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:
parent
d486144782
commit
fdc4ed58b4
|
@ -19,7 +19,6 @@ package org.apache.solr.common;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
|
||||
|
@ -28,16 +27,12 @@ import java.util.Collection;
|
|||
* a Lucene Document. Like the SolrDocument, the field values should
|
||||
* 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$
|
||||
* @since solr 1.3
|
||||
*/
|
||||
public class SolrInputDocument implements Iterable<SolrInputField>
|
||||
{
|
||||
private final Map<String,SolrInputField> _fields;
|
||||
private Map<String,Boolean> _removeDuplicates = null;
|
||||
private float _documentBoost = 1.0f;
|
||||
|
||||
public SolrInputDocument()
|
||||
|
@ -53,27 +48,12 @@ public class SolrInputDocument implements Iterable<SolrInputField>
|
|||
if( _fields != null ) {
|
||||
_fields.clear();
|
||||
}
|
||||
if(_removeDuplicates != null ) {
|
||||
_removeDuplicates.clear();
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
// 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.
|
||||
*
|
||||
|
@ -137,14 +117,8 @@ public class SolrInputDocument implements Iterable<SolrInputField>
|
|||
{
|
||||
SolrInputField field = new SolrInputField( name );
|
||||
_fields.put( name, field );
|
||||
if( isDistinct( name ) ) {
|
||||
field.value = new LinkedHashSet<Object>();
|
||||
this.addField(name, value, boost);
|
||||
}
|
||||
else {
|
||||
field.setValue( value, boost );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all fields and boosts from the document
|
||||
|
@ -171,34 +145,6 @@ public class SolrInputDocument implements Iterable<SolrInputField>
|
|||
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
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -114,27 +114,6 @@ public class SolrDocumentTest extends TestCase
|
|||
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()
|
||||
{
|
||||
Float fval0 = new Float( 10.01f );
|
||||
|
@ -149,21 +128,6 @@ public class SolrDocumentTest extends TestCase
|
|||
doc.addField( "f", fval2, 1.0f );
|
||||
}
|
||||
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() );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue