Author: jerome Date: Mon Feb 27 14:21:55 2006 New Revision: 381465 URL: http://svn.apache.org/viewcvs?rev=381465&view=rev Log: NUTCH-204, HitDetails can now return multi-valued fields
Modified: lucene/nutch/trunk/src/java/org/apache/nutch/searcher/HitDetails.java lucene/nutch/trunk/src/test/org/apache/nutch/searcher/TestHitDetails.java Modified: lucene/nutch/trunk/src/java/org/apache/nutch/searcher/HitDetails.java URL: http://svn.apache.org/viewcvs/lucene/nutch/trunk/src/java/org/apache/nutch/searcher/HitDetails.java?rev=381465&r1=381464&r2=381465&view=diff ============================================================================== --- lucene/nutch/trunk/src/java/org/apache/nutch/searcher/HitDetails.java (original) +++ lucene/nutch/trunk/src/java/org/apache/nutch/searcher/HitDetails.java Mon Feb 27 14:21:55 2006 @@ -19,6 +19,7 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; +import java.util.ArrayList; import java.util.logging.Logger; import org.apache.hadoop.io.*; @@ -73,6 +74,19 @@ return null; } + /** Returns all the values with the specified name. */ + public String[] getValues(String field) { + ArrayList vals = new ArrayList(); + for (int i=0; i<length; i++) { + if (fields[i].equals(field)) { + vals.add(values[i]); + } + } + return (vals.size() > 0) + ? (String[]) vals.toArray(new String[vals.size()]) + : null; +} + // javadoc from Writable public void write(DataOutput out) throws IOException { out.writeInt(length); @@ -107,13 +121,20 @@ /** Display as HTML. */ public String toHtml() { + String[] vals = null; StringBuffer buffer = new StringBuffer(); buffer.append("<ul>\n"); for (int i = 0; i < length; i++) { buffer.append("<li>"); buffer.append(fields[i]); buffer.append(" = "); - buffer.append(Entities.encode(values[i])); + vals = getValues(fields[i]); + for (int j=0; j<vals.length; j++) { + buffer.append(Entities.encode(vals[j])); + if (j<(vals.length-1)) { + buffer.append(","); + } + } buffer.append("</li>\n"); } buffer.append("</ul>\n"); Modified: lucene/nutch/trunk/src/test/org/apache/nutch/searcher/TestHitDetails.java URL: http://svn.apache.org/viewcvs/lucene/nutch/trunk/src/test/org/apache/nutch/searcher/TestHitDetails.java?rev=381465&r1=381464&r2=381465&view=diff ============================================================================== --- lucene/nutch/trunk/src/test/org/apache/nutch/searcher/TestHitDetails.java (original) +++ lucene/nutch/trunk/src/test/org/apache/nutch/searcher/TestHitDetails.java Mon Feb 27 14:21:55 2006 @@ -23,9 +23,9 @@ public TestHitDetails(String name) { super(name); } public void testHitDetails() throws Exception { - final int length = 3; - final String[] fields = new String[] {"a", "b", "c" }; - final String[] values = new String[] { "foo", "bar", "baz" }; + final int length = 4; + final String[] fields = new String[] {"a", "b", "c", "a" }; + final String[] values = new String[] { "foo1", "bar", "baz", "foo2" }; HitDetails before = new HitDetails(fields, values); @@ -38,10 +38,20 @@ HitDetails after = HitDetails.read(dib); assertEquals(length, after.getLength()); - for (int i = 0; i < length; i++) { + for (int i = 0; i < 3; i++) { assertEquals(fields[i], after.getField(i)); assertEquals(values[i], after.getValue(i)); assertEquals(values[i], after.getValue(fields[i])); } + String[] vals = after.getValues("a"); + assertEquals(2, vals.length); + assertEquals("foo1", vals[0]); + assertEquals("foo2", vals[1]); + vals = after.getValues("b"); + assertEquals(1, vals.length); + assertEquals("bar", vals[0]); + vals = after.getValues("c"); + assertEquals(1, vals.length); + assertEquals("baz", vals[0]); } }