FieldSortComparator implements Comparator<Hit> { @Override public int compare(Hit hit1, Hit hit2) { int c = compare(hit1.getSortFields(), hit2.getSortFields()); if (c == 0) { c = hit1.compareTo(hit2); } return c; } FieldSortComparator(SortField[] sortFields, WritableType[] fieldTypes); SortField[] getSortFields(); WritableType[] getFieldTypes(); @Override int compare(Hit hit1, Hit hit2); int compare(Object[] fields1, Object[] fields2);  }
@Test public void testSingleIntFieldCompare() { SortField[] sortFields = new SortField[] { new SortField("intField", SortField.INT) }; WritableType[] sortFieldTypes = new WritableType[] { WritableType.INT }; FieldSortComparator fieldSortComparator = new FieldSortComparator(sortFields, sortFieldTypes); Hit hit1 = new Hit("shard", "node", 0.0f, 1, sortFieldTypes); Hit hit2 = new Hit("shard", "node", 0.0f, 2, sortFieldTypes); hit1.setSortFields(new WritableComparable[] { new IntWritable(1) }); hit2.setSortFields(new WritableComparable[] { new IntWritable(2) }); assertEquals(0, fieldSortComparator.compare(hit1, hit1)); assertEquals(1, fieldSortComparator.compare(hit2, hit1)); assertEquals(-1, fieldSortComparator.compare(hit1, hit2)); }
@Test public void testTwoIntFieldCompare() { SortField[] sortFields = new SortField[] { new SortField("intField1", SortField.INT), new SortField("intField2", SortField.INT) }; WritableType[] sortFieldTypes = new WritableType[] { WritableType.INT, WritableType.INT }; FieldSortComparator fieldSortComparator = new FieldSortComparator(sortFields, sortFieldTypes); Hit hit1 = new Hit("shard", "node", 0.0f, 1, sortFieldTypes); Hit hit2 = new Hit("shard", "node", 0.0f, 2, sortFieldTypes); hit1.setSortFields(new WritableComparable[] { new IntWritable(1), new IntWritable(1) }); hit2.setSortFields(new WritableComparable[] { new IntWritable(1), new IntWritable(2) }); assertEquals(0, fieldSortComparator.compare(hit1, hit1)); assertEquals(1, fieldSortComparator.compare(hit2, hit1)); assertEquals(-1, fieldSortComparator.compare(hit1, hit2)); }
@Test public void testStringFieldDocIdCompare() { SortField[] sortFields = new SortField[] { new SortField("stringField", SortField.STRING), new SortField("docIdField", SortField.DOC) }; WritableType[] sortFieldTypes = new WritableType[] { WritableType.TEXT, WritableType.INT }; FieldSortComparator fieldSortComparator = new FieldSortComparator(sortFields, sortFieldTypes); Hit hit1 = new Hit("shard", "node", 0.0f, 1, sortFieldTypes); Hit hit2 = new Hit("shard", "node", 0.0f, 2, sortFieldTypes); hit1.setSortFields(new WritableComparable[] { new Text("a"), new IntWritable(1) }); hit2.setSortFields(new WritableComparable[] { new Text("a"), new IntWritable(2) }); assertEquals(0, fieldSortComparator.compare(hit1, hit1)); assertEquals(1, fieldSortComparator.compare(hit2, hit1)); assertEquals(-1, fieldSortComparator.compare(hit1, hit2)); }
@Test public void testStringFieldScoreCompare() { SortField[] sortFields = new SortField[] { new SortField("stringField", SortField.STRING), new SortField("scoreField", SortField.SCORE) }; WritableType[] sortFieldTypes = new WritableType[] { WritableType.TEXT, WritableType.FLOAT }; FieldSortComparator fieldSortComparator = new FieldSortComparator(sortFields, sortFieldTypes); Hit hit1 = new Hit("shard", "node", 0.5f, 1, sortFieldTypes); Hit hit2 = new Hit("shard", "node", 0.2f, 2, sortFieldTypes); hit1.setSortFields(new WritableComparable[] { new Text("a"), new FloatWritable(hit1.getScore()) }); hit2.setSortFields(new WritableComparable[] { new Text("a"), new FloatWritable(hit2.getScore()) }); assertEquals(0, fieldSortComparator.compare(hit1, hit1)); assertEquals(1, fieldSortComparator.compare(hit2, hit1)); assertEquals(-1, fieldSortComparator.compare(hit1, hit2)); }
@Test public void testSameFieldValuesSecondaryCompare() { SortField[] sortFields = new SortField[] { new SortField("intField", SortField.INT) }; WritableType[] sortFieldTypes = new WritableType[] { WritableType.INT }; FieldSortComparator fieldSortComparator = new FieldSortComparator(sortFields, sortFieldTypes); Hit hit1 = new Hit("shard", "node", 0.0f, 1, sortFieldTypes); Hit hit2 = new Hit("shard", "node", 0.0f, 1, sortFieldTypes); hit1.setSortFields(new WritableComparable[] { new IntWritable(1) }); hit2.setSortFields(new WritableComparable[] { new IntWritable(1) }); assertEquals(0, fieldSortComparator.compare(hit2, hit1)); assertEquals(0, fieldSortComparator.compare(hit1, hit2)); hit2.setDocId(2); assertEquals(-1, fieldSortComparator.compare(hit1, hit2)); assertEquals( 1, fieldSortComparator.compare(hit2, hit1)); }