本文整理汇总了Java中org.apache.lucene.search.SortField.getType方法的典型用法代码示例。如果您正苦于以下问题:Java SortField.getType方法的具体用法?Java SortField.getType怎么用?Java SortField.getType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.search.SortField
的用法示例。
在下文中一共展示了SortField.getType方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getValueSource
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
@Override
public ValueSource getValueSource(String name) {
Object o = map.get(name);
if (o == null) {
throw new IllegalArgumentException("Invalid reference '" + name + "'");
} else if (o instanceof Expression) {
return ((Expression)o).getValueSource(this);
} else if (o instanceof ValueSource) {
return ((ValueSource)o);
}
SortField field = (SortField) o;
switch(field.getType()) {
case INT:
return new IntFieldSource(field.getField(), (IntParser) field.getParser());
case LONG:
return new LongFieldSource(field.getField(), (LongParser) field.getParser());
case FLOAT:
return new FloatFieldSource(field.getField(), (FloatParser) field.getParser());
case DOUBLE:
return new DoubleFieldSource(field.getField(), (DoubleParser) field.getParser());
case SCORE:
return getScoreValueSource();
default:
throw new UnsupportedOperationException();
}
}
示例2: buildSort
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
public static Optional<SortAndFormats> buildSort(List<SortBuilder<?>> sortBuilders, QueryShardContext context) throws IOException {
List<SortField> sortFields = new ArrayList<>(sortBuilders.size());
List<DocValueFormat> sortFormats = new ArrayList<>(sortBuilders.size());
for (SortBuilder<?> builder : sortBuilders) {
SortFieldAndFormat sf = builder.build(context);
sortFields.add(sf.field);
sortFormats.add(sf.format);
}
if (!sortFields.isEmpty()) {
// optimize if we just sort on score non reversed, we don't really
// need sorting
boolean sort;
if (sortFields.size() > 1) {
sort = true;
} else {
SortField sortField = sortFields.get(0);
if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) {
sort = false;
} else {
sort = true;
}
}
if (sort) {
return Optional.of(new SortAndFormats(
new Sort(sortFields.toArray(new SortField[sortFields.size()])),
sortFormats.toArray(new DocValueFormat[sortFormats.size()])));
}
}
return Optional.empty();
}
示例3: parse
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
XContentParser.Token token = parser.currentToken();
List<SortField> sortFields = new ArrayList<>(2);
if (token == XContentParser.Token.START_ARRAY) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token == XContentParser.Token.START_OBJECT) {
addCompoundSortField(parser, context, sortFields);
} else if (token == XContentParser.Token.VALUE_STRING) {
addSortField(context, sortFields, parser.text(), false, null, null, null, null);
} else {
throw new IllegalArgumentException("malformed sort format, within the sort array, an object, or an actual string are allowed");
}
}
} else if (token == XContentParser.Token.VALUE_STRING) {
addSortField(context, sortFields, parser.text(), false, null, null, null, null);
} else if (token == XContentParser.Token.START_OBJECT) {
addCompoundSortField(parser, context, sortFields);
} else {
throw new IllegalArgumentException("malformed sort format, either start with array, object, or an actual string");
}
if (!sortFields.isEmpty()) {
// optimize if we just sort on score non reversed, we don't really need sorting
boolean sort;
if (sortFields.size() > 1) {
sort = true;
} else {
SortField sortField = sortFields.get(0);
if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) {
sort = false;
} else {
sort = true;
}
}
if (sort) {
context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
}
}
}
示例4: assertQuery
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
void assertQuery(Query query, Filter filter, Sort sort) throws Exception {
int size = TestUtil.nextInt(random(), 1, searcher.getIndexReader().maxDoc() / 5);
TopDocs expected = searcher.search(query, filter, size, sort, random().nextBoolean(), random().nextBoolean());
// make our actual sort, mutating original by replacing some of the
// sortfields with equivalent expressions
SortField original[] = sort.getSort();
SortField mutated[] = new SortField[original.length];
for (int i = 0; i < mutated.length; i++) {
if (random().nextInt(3) > 0) {
SortField s = original[i];
Expression expr = JavascriptCompiler.compile(s.getField());
SimpleBindings simpleBindings = new SimpleBindings();
simpleBindings.add(s);
boolean reverse = s.getType() == SortField.Type.SCORE || s.getReverse();
mutated[i] = expr.getSortField(simpleBindings, reverse);
} else {
mutated[i] = original[i];
}
}
Sort mutatedSort = new Sort(mutated);
TopDocs actual = searcher.search(query, filter, size, mutatedSort, random().nextBoolean(), random().nextBoolean());
CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
if (size < actual.totalHits) {
expected = searcher.searchAfter(expected.scoreDocs[size-1], query, filter, size, sort);
actual = searcher.searchAfter(actual.scoreDocs[size-1], query, filter, size, mutatedSort);
CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
}
}
示例5: getComparator
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
private Comparator<GroupDoc> getComparator(Sort sort, final boolean sortByScoreOnly, final int[] fieldIdToDocID) {
final SortField[] sortFields = sort.getSort();
return new Comparator<GroupDoc>() {
@Override
public int compare(GroupDoc d1, GroupDoc d2) {
for (SortField sf : sortFields) {
final int cmp;
if (sf.getType() == SortField.Type.SCORE) {
if (d1.score > d2.score) {
cmp = -1;
} else if (d1.score < d2.score) {
cmp = 1;
} else {
cmp = sortByScoreOnly ? fieldIdToDocID[d1.id] - fieldIdToDocID[d2.id] : 0;
}
} else if (sf.getField().equals("sort1")) {
cmp = d1.sort1.compareTo(d2.sort1);
} else if (sf.getField().equals("sort2")) {
cmp = d1.sort2.compareTo(d2.sort2);
} else if (sf.getField().equals("sort3")) {
cmp = d1.sort3.compareTo(d2.sort3);
} else {
assertEquals(sf.getField(), "id");
cmp = d1.id - d2.id;
}
if (cmp != 0) {
return sf.getReverse() ? -cmp : cmp;
}
}
// Our sort always fully tie breaks:
fail();
return 0;
}
};
}
示例6: includesScore
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
public static boolean includesScore(Sort sort) {
if (sort==null) return true;
for (SortField sf : sort.getSort()) {
if (sf.getType() == SortField.Type.SCORE) return true;
}
return false;
}
示例7: modifySortSpec
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
private SortSpec modifySortSpec(SortSpec current, boolean force, ElevationComparatorSource comparator) {
boolean modify = false;
SortField[] currentSorts = current.getSort().getSort();
List<SchemaField> currentFields = current.getSchemaFields();
ArrayList<SortField> sorts = new ArrayList<>(currentSorts.length + 1);
List<SchemaField> fields = new ArrayList<>(currentFields.size() + 1);
// Perhaps force it to always sort by score
if (force && currentSorts[0].getType() != SortField.Type.SCORE) {
sorts.add(new SortField("_elevate_", comparator, true));
fields.add(null);
modify = true;
}
for (int i = 0; i < currentSorts.length; i++) {
SortField sf = currentSorts[i];
if (sf.getType() == SortField.Type.SCORE) {
sorts.add(new SortField("_elevate_", comparator, !sf.getReverse()));
fields.add(null);
modify = true;
}
sorts.add(sf);
fields.add(currentFields.get(i));
}
if (modify) {
SortSpec newSpec = new SortSpec(new Sort(sorts.toArray(new SortField[sorts.size()])),
fields);
newSpec.setOffset(current.getOffset());
newSpec.setCount(current.getCount());
return newSpec;
}
return null;
}
示例8: getCachedComparator
import org.apache.lucene.search.SortField; //导入方法依赖的package包/类
Comparator<ShardDoc> getCachedComparator(SortField sortField, IndexSearcher searcher) {
SortField.Type type = sortField.getType();
if (type == SortField.Type.SCORE) {
return comparatorScore();
} else if (type == SortField.Type.REWRITEABLE) {
try {
sortField = sortField.rewrite(searcher);
} catch (IOException e) {
throw new SolrException(SERVER_ERROR, "Exception rewriting sort field " + sortField, e);
}
}
return comparatorFieldComparator(sortField);
}