lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From no...@apache.org
Subject svn commit: r1575886 - in /lucene/dev/trunk/solr: core/src/test/org/apache/solr/update/ solrj/src/java/org/apache/solr/common/
Date Mon, 10 Mar 2014 10:18:43 GMT
Author: noble
Date: Mon Mar 10 10:18:42 2014
New Revision: 1575886

URL: http://svn.apache.org/r1575886
Log:
SOLR-5837 Added .equals method for SolrDocument, SolrInputDocument and SolrInputField

Modified:
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java?rev=1575886&r1=1575885&r2=1575886&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java Mon
Mar 10 10:18:42 2014
@@ -23,7 +23,9 @@ import org.apache.lucene.index.AtomicRea
 import org.apache.lucene.index.NumericDocValues;
 import org.apache.lucene.index.SlowCompositeReaderWrapper;
 import org.apache.lucene.search.similarities.DefaultSimilarity;
+import org.apache.lucene.util.TestUtil;
 import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrInputDocument;
@@ -394,4 +396,106 @@ public class DocumentBuilderTest extends
     assertNull(h.validateUpdate(add(xml, new String[0])));
   }
 
+  public void testSolrDocumentEquals() {
+
+    String randomString = TestUtil.randomSimpleString(random());
+
+    SolrDocument doc1 = new SolrDocument();
+    doc1.addField("foo", randomString);
+
+    SolrDocument doc2 = new SolrDocument();
+    doc2.addField("foo", randomString);
+
+    assertTrue(doc1.equals(doc2));
+
+    doc1.addField("foo", "bar");
+
+    assertFalse(doc1.equals(doc2));
+
+    doc1 = new SolrDocument();
+    doc1.addField("bar", randomString);
+
+    assertFalse(doc1.equals(doc2));
+
+    int randomInt = random().nextInt();
+    doc1 = new SolrDocument();
+    doc1.addField("foo", randomInt);
+    doc2 = new SolrDocument();
+    doc2.addField("foo", randomInt);
+
+    assertTrue(doc1.equals(doc2));
+
+    doc2 = new SolrDocument();
+    doc2.addField("bar", randomInt);
+
+    assertFalse(doc1.equals(doc2));
+
+  }
+
+  public void testSolrInputDocumentEquality() {
+
+    String randomString = TestUtil.randomSimpleString(random());
+
+    SolrInputDocument doc1 = new SolrInputDocument();
+    doc1.addField("foo", randomString);
+    SolrInputDocument doc2 = new SolrInputDocument();
+    doc2.addField("foo", randomString);
+
+    assertTrue(doc1.equals(doc2));
+
+    doc1.setDocumentBoost(1.1f);
+    assertFalse(doc1.equals(doc2));
+
+    doc2.setDocumentBoost(1.1f);
+    assertTrue(doc1.equals(doc2));
+
+    doc2.setDocumentBoost(20f);
+    assertFalse(doc1.equals(doc2));
+
+
+    doc1 = new SolrInputDocument();
+    doc1.addField("foo", randomString);
+    doc2 = new SolrInputDocument();
+    doc2.addField("foo", randomString);
+
+    SolrInputDocument childDoc = new SolrInputDocument();
+    childDoc.addField("foo", "bar");
+
+    doc1.addChildDocument(childDoc);
+    assertFalse(doc1.equals(doc2));
+
+    doc2.addChildDocument(childDoc);
+    assertTrue(doc1.equals(doc2));
+
+    SolrInputDocument childDoc1 = new SolrInputDocument();
+    childDoc.addField(TestUtil.randomSimpleString(random()), TestUtil.randomSimpleString(random()));
+    doc2.addChildDocument(childDoc1);
+    assertFalse(doc1.equals(doc2));
+
+  }
+
+  public void testSolrInputFieldEquality() {
+    String randomString = TestUtil.randomSimpleString(random(), 10, 20);
+
+    int val = random().nextInt();
+    SolrInputField sif1 = new SolrInputField(randomString);
+    sif1.setValue(val, 1.0f);
+    SolrInputField sif2 = new SolrInputField(randomString);
+    sif2.setValue(val, 1.0f);
+
+    assertTrue(sif1.equals(sif2));
+
+    sif1.setBoost(2.1f);
+    sif2.setBoost(2.1f);
+    assertTrue(sif1.equals(sif2));
+
+    sif2.setBoost(2.0f);
+    assertFalse(sif1.equals(sif2));
+
+    sif2.setName("foo");
+    assertFalse(sif1.equals(sif2));
+
+
+  }
+
 }

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java?rev=1575886&r1=1575885&r2=1575886&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java Mon Mar
10 10:18:42 2014
@@ -213,7 +213,30 @@ public class SolrDocument implements Map
   public Iterator<Entry<String, Object>> iterator() {
     return _fields.entrySet().iterator();
   }
-  
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (!(o instanceof SolrDocument)) {
+      return false;
+    }
+
+    SolrDocument solrDocument = (SolrDocument) o;
+
+    if (!_fields.equals(solrDocument._fields)) {
+      return false;
+    }
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    return _fields.hashCode();
+  }
+
   //-----------------------------------------------------------------------------------------
   // JSTL Helpers
   //-----------------------------------------------------------------------------------------

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java?rev=1575886&r1=1575885&r2=1575886&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java Mon
Mar 10 10:18:42 2014
@@ -275,7 +275,39 @@ public class SolrInputDocument implement
   public Collection<SolrInputField> values() {
     return _fields.values();
   }
-  
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (!(o instanceof SolrInputDocument)) {
+      return false;
+    }
+
+    SolrInputDocument sdoc = (SolrInputDocument) o;
+
+    if (!_fields.equals(sdoc._fields)) {
+      return false;
+    }
+    if (Float.compare(sdoc._documentBoost, _documentBoost) != 0) {
+      return false;
+    }
+    if (_childDocuments != null ? !_childDocuments.equals(sdoc._childDocuments) : sdoc._childDocuments
!= null) {
+      return false;
+    }
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    int result = _fields.hashCode();
+    result = 31 * result + (_documentBoost != +0.0f ? Float.floatToIntBits(_documentBoost)
: 0);
+    result = 31 * result + (_childDocuments != null ? _childDocuments.hashCode() : 0);
+    return result;
+  }
+
   public void addChildDocument(SolrInputDocument child) {
    if (_childDocuments == null) {
      _childDocuments = new ArrayList<SolrInputDocument>();

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java?rev=1575886&r1=1575885&r2=1575886&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrInputField.java Mon Mar
10 10:18:42 2014
@@ -229,4 +229,38 @@ public class SolrInputField implements I
     }
     return clone;
   }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (!(o instanceof SolrInputField)) {
+      return false;
+    }
+
+    SolrInputField sif = (SolrInputField) o;
+
+    if (!name.equals(sif.name)) {
+      return false;
+    }
+
+    if (!value.equals(sif.value)) {
+      return false;
+    }
+
+    if (Float.compare(sif.boost, boost) != 0) {
+      return false;
+    }
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    int result = name.hashCode();
+    result = 31 * result + value.hashCode();
+    result = 31 * result + (boost != +0.0f ? Float.floatToIntBits(boost) : 0);
+    return result;
+  }
 }



Mime
View raw message