|
35 | 35 | import org.apache.lucene.index.IndexWriter; |
36 | 36 | import org.apache.lucene.index.IndexWriterConfig; |
37 | 37 | import org.apache.lucene.index.IndexWriterConfig.OpenMode; |
| 38 | +import org.apache.lucene.index.IndexableField; |
38 | 39 | import org.apache.lucene.index.Term; |
39 | 40 | import org.apache.lucene.queryparser.flexible.core.QueryNodeException; |
40 | 41 | import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; |
|
60 | 61 | import org.icatproject.lucene.analyzers.IcatAnalyzer; |
61 | 62 | import org.icatproject.lucene.analyzers.IcatSynonymAnalyzer; |
62 | 63 | import org.icatproject.lucene.exceptions.LuceneException; |
63 | | -import org.junit.Ignore; |
| 64 | +import org.icatproject.utils.IcatUnits; |
64 | 65 | import org.junit.Test; |
65 | 66 |
|
66 | 67 | import jakarta.json.Json; |
@@ -410,6 +411,24 @@ public void testLocationFields() throws IOException, QueryNodeException { |
410 | 411 | checkHits(datafileSearcher, DocumentMapping.datafileParser, "+\"/dls/i00/data/2000\" +screen* +(AB00 DE00) +txt", 2L); |
411 | 412 | } |
412 | 413 |
|
| 414 | + @Test |
| 415 | + public void testParseDocument() { |
| 416 | + // Ensure that we encode units correctly when SI conversion fails |
| 417 | + Lucene lucene = new Lucene(); |
| 418 | + lucene.icatUnits = new IcatUnits(); |
| 419 | + JsonObjectBuilder builder = Json.createObjectBuilder(); |
| 420 | + builder.add("type.units", "N/A"); |
| 421 | + builder.add("numericValue", 1.); |
| 422 | + Document document = lucene.parseDocument(builder.build()); |
| 423 | + assertEquals(4, document.getFields().size()); |
| 424 | + assertEquals("N/A", document.getField("type.units").stringValue()); |
| 425 | + // Unlike strings, numbers are represented by different fields for different purposes |
| 426 | + IndexableField[] fields = document.getFields("numericValue"); |
| 427 | + assertEquals(4607182418800017408L, fields[0].numericValue().longValue()); // For faceting, the double value is encoded as a long |
| 428 | + assertEquals(1., fields[1].numericValue().doubleValue(), 0); // For querying, encoded as double |
| 429 | + assertEquals(1., fields[2].numericValue().doubleValue(), 0); // Stored value for returning documents, encoded as a double |
| 430 | + } |
| 431 | + |
413 | 432 | private void checkHits(IndexSearcher searcher, StandardQueryParser parser, String queryString, long expected) throws QueryNodeException, IOException { |
414 | 433 | queryString = SearchBucket.escapePath(queryString); |
415 | 434 | Query query = parser.parse(queryString, null); |
|
0 commit comments