LUCENE-4218: restore stringValue for numeric fields

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1380812 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-09-04 19:01:04 +00:00
parent ea8ecb15a4
commit 8ff108e41d
3 changed files with 30 additions and 1 deletions

View File

@ -138,6 +138,10 @@ Bug Fixes
* LUCENE-4333: Fixed NPE in TermGroupFacetCollector when faceting on mv fields.
(Jesse MacVicar, Martijn van Groningen)
* LUCENE-4218: Document.get(String) and Field.stringValue() again return
values for numeric fields, like Lucene 3.x and consistent with the documentation.
(Jamie, Uwe Schindler, Robert Muir)
* NRTCachingDirectory was always caching a newly flushed segment in
RAM, instead of checking the estimated size of the segment
to decide whether to cache it. (Mike McCandless)

View File

@ -267,7 +267,11 @@ public class Field implements IndexableField, StorableField {
* getBinaryValue() must be set.
*/
public String stringValue() {
return fieldsData instanceof String ? (String) fieldsData : null;
if (fieldsData instanceof String || fieldsData instanceof Number) {
return fieldsData.toString();
} else {
return null;
}
}
/**

View File

@ -324,4 +324,25 @@ public class TestDocument extends LuceneTestCase {
// expected
}
}
public void testNumericFieldAsString() throws Exception {
Document doc = new Document();
doc.add(new IntField("int", 5, Field.Store.YES));
assertEquals("5", doc.get("int"));
assertNull(doc.get("somethingElse"));
doc.add(new IntField("int", 4, Field.Store.YES));
assertArrayEquals(new String[] { "5", "4" }, doc.getValues("int"));
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
iw.addDocument(doc);
DirectoryReader ir = iw.getReader();
StoredDocument sdoc = ir.document(0);
assertEquals("5", sdoc.get("int"));
assertNull(sdoc.get("somethingElse"));
assertArrayEquals(new String[] { "5", "4" }, sdoc.getValues("int"));
ir.close();
iw.close();
dir.close();
}
}