SimpleSortField extends SortField { @Override public org.apache.lucene.search.SortField sortField(Schema schema) { if (field.equalsIgnoreCase("score")) { return FIELD_SCORE; } Mapper mapper = schema.mapper(field); if (mapper == null) { throw new IndexException("No mapper found for sortFields field '{}'", field); } else if (!mapper.docValues) { throw new IndexException("Field '{}' does not support sorting", field); } else { return mapper.sortField(field, reverse); } } SimpleSortField(String field, Boolean reverse); String getField(); @Override org.apache.lucene.search.SortField sortField(Schema schema); Set<String> postProcessingFields(); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); final String field; }
@Test public void testSortFieldScoreDefaults() { Schema schema = schema().mapper("field", stringMapper()).build(); SimpleSortField sortField = new SimpleSortField("score", null); org.apache.lucene.search.SortField luceneSortField = sortField.sortField(schema); assertNotNull("SortField is not created", luceneSortField); assertEquals("SortField type is wrong", FIELD_SCORE, luceneSortField); assertEquals("SortField reverse is wrong", SortField.DEFAULT_REVERSE, luceneSortField.getReverse()); }
@Test public void testSortFieldScore() { Schema schema = schema().mapper("field", stringMapper()).build(); SimpleSortField sortField = new SimpleSortField("score", false); org.apache.lucene.search.SortField luceneSortField = sortField.sortField(schema); assertNotNull("SortField is not created", luceneSortField); assertEquals("SortField type is wrong", FIELD_SCORE, luceneSortField); assertFalse("SortField reverse is wrong", luceneSortField.getReverse()); }
@Test public void testSortFieldScoreReverse() { Schema schema = schema().mapper("field", stringMapper()).build(); SimpleSortField sortField = new SimpleSortField("score", true); org.apache.lucene.search.SortField luceneSortField = sortField.sortField(schema); assertNotNull("SortField is not created", luceneSortField); assertEquals("SortField type is wrong", FIELD_SCORE, luceneSortField); assertFalse("SortField reverse is wrong", luceneSortField.getReverse()); }
@Test(expected = IndexException.class) public void testSortFieldWithoutMapper() { Schema schema = schema().build(); SimpleSortField sortField = new SimpleSortField("field", true); sortField.sortField(schema); }