java在Hibernate搜索中搜索首选项
我使用的是Hibernate Search 3.6.2,我想将特定文档排序到最前面。出于这个原因,我编写了一个定制的比较器源代码,但不知怎么的,它似乎不起作用
我希望文档中包含这些首选值,并将其排序到最前面
以下是我目前掌握的情况:
public class PreferenceFieldComparatorSource<T> extends FieldComparatorSource {
private static final long serialVersionUID = -8959374194451783596L;
private final List<?> preferred;
private final Class<?> clazz;
private final StringBridge stringBridge;
@SafeVarargs
public PreferenceFieldComparatorSource(Class<T> clazz,
StringBridge stringBridge, T... preferred) {
this(clazz, stringBridge, Arrays.asList(preferred));
}
public PreferenceFieldComparatorSource(Class<T> clazz,
StringBridge stringBridge, List<T> preferred) {
this.clazz = clazz;
this.stringBridge = stringBridge;
this.preferred = preferred;
}
@Override
public FieldComparator<String> newComparator(final String fieldName,
int numHits, int sortPos, final boolean reversed)
throws IOException {
return new FieldComparator<String>() {
private String[] values;
private String bottom;
private String[] currentReaderValues;
@Override
public int compare(int slot1, int slot2) {
return this.compare(this.values[slot1], this.values[slot2]);
}
@Override
public int compareBottom(int doc) throws IOException {
return this.compare(this.bottom, this.currentReaderValues[doc]);
}
@Override
public void copy(int slot, int doc) throws IOException {
this.values[slot] = this.currentReaderValues[doc];
}
@Override
public void setBottom(int slot) {
this.bottom = this.values[slot];
}
@Override
public void setNextReader(IndexReader reader, int docBase)
throws IOException {
System.out.println("fieldName: " + fieldName);
this.currentReaderValues = new String[reader.maxDoc()];
this.values = new String[reader.maxDoc()];
for (int i = 0; i < currentReaderValues.length; ++i) {
try {
this.currentReaderValues[i] = reader.document(docBase)
.get(fieldName);
} catch (ClassCastException e) {
throw new IllegalArgumentException(
PreferenceFieldComparatorSource.this.stringBridge
+ " doesn't convert strings into "
+ PreferenceFieldComparatorSource.this.clazz);
}
}
}
@Override
public String value(int slot) {
return this.values[slot];
}
private int compare(Object first, Object second) {
// if none of the given values is in the preferred lists
// the index is -1 but the compare function will still work
// correctly
// and will only move the preferred ones.
int firstIndex = PreferenceFieldComparatorSource.this.preferred
.indexOf(PreferenceFieldComparatorSource.this.stringBridge
.objectToString(first));
int secondIndex = PreferenceFieldComparatorSource.this.preferred
.indexOf(PreferenceFieldComparatorSource.this.stringBridge
.objectToString(second));
int result = Integer.compare(firstIndex, secondIndex);
if (reversed) {
result *= -1;
}
return result;
}
};
}
}
compare方法似乎没有被调用。我在这门课上做错了什么
编辑:
如需澄清评论中的声明:
@Override
protected boolean lessThan(final Entry hitA, final Entry hitB) {
assert hitA != hitB;
assert hitA.slot != hitB.slot;
int numComparators = comparators.length;
for (int i = 0; i < numComparators; ++i) {
final int c = reverseMul[i] * comparators[i].compare(hitA.slot,
hitB.slot);
if (c != 0) {
// Short circuit
return c > 0;
}
}
// avoid random sort order that could lead to duplicates (bug
#31241):
return hitA.doc > hitB.doc;
}
共 (0) 个答案